【问题标题】:Character arrays passing into functions [closed]传递给函数的字符数组[关闭]
【发布时间】:2019-04-29 03:22:18
【问题描述】:

我目前在将字符数组从我的主函数传递到某个计算数组中元素数量的函数时遇到问题。

我使用 getchar() 函数要求 2 个单独的字符串字符。

为了澄清,这是我的代码的 sn-p:

我尝试过使用 scanf to &myArray[0] 作为替代方法

#include <stdio.h>
#include <stdlib.h>
#define ARRAY_LEN 20


int match(char s[], char t[] )
{

    int i = 0;
    int j = 0;

    printf("begin: ivaL: %d, jval: %d\n", i, j);
    while(s[i] != '\0')
        i++;

    while(t[j] != '\0')
        j++;

    printf("i val: %d, jval: %d ", i, j); /*J value is messed up, it prints 20+ when ive only typed 5 characters or so*/


}

int main()
{
    int cur = 0;
    char char1[ARRAY_LEN];
    char char2[ARRAY_LEN];
    char c;
    char f;
    printf("Enter char: ");
    while((c=getchar())!=EOF && c!='\n')
    {
        char1[cur++] = c;
    }
    cur = 0;
    printf("\n2nd Char: ");
    while((f=getchar())!=EOF && f!='\n')
    {
        char2[cur++] = f;
        putchar(f);
    }
    putchar('\n')
    printf("Cur value:  %d\n", cur); //here cur accurately prints the correct number of while loops done when counting the characters

    match(char1, char2); /*here i tried to fetch the number of elements in char2 before i passed it to match function and in here char2 is showing correct value, something happens when i pass char2 to the match function*/



}

【问题讨论】:

  • 为什么不c=getchar())!=EOF &amp;&amp; c!='\n' -> (c=getchar())!='\n'
  • 你不是 null 终止两个字符串。默认不添加空字符
  • 我正在阅读“C 编程语言”并遵循相同的风格,如果我省略了 EOF 检查,但没有任何改变。
  • @sjsam 谢谢!这解决了我的问题。 :) 从来没有真正想过这个
  • 当您没有在第一个 while 循环之后立即明确地执行 char1[cur]='\0' 时,您不能执行 s[i] != '\0'。第二个也是如此。

标签: c getchar


【解决方案1】:

您的 match() 函数会遍历您的 char 数组,直到找到空终止符,但您实际上从未在数组中的任何位置放置空终止符。

printf("Enter char: ");    
while((c=getchar()) !='\n'){
    char1[cur++] = c;
}
char1[cur] = '\0';
cur = 0;
printf("\n2nd Char: ");    
while((f=getchar()) !='\n'){
    char2[cur++] = f;
}
char2[cur] = '\0';

【讨论】:

    【解决方案2】:

    您有语法错误需要解决:

    putchar('\n') // Missing semicolon.
    

    读取一组字符后默认不添加空字符。

    while((c=getchar())!='\n')
        {
            char1[cur++] = c;
        }
    char1[cur] = '\0'; // Adding a null terminator to make the identifier a C Style string.
    

    第二个也是如此。

    除此之外,您还有其他问题。

    int match(char s[], char t[] )
    

    应该返回一个整数。你可以考虑做类似的事情:

    return i==j;
    

    捕获所有编译器警告(比如在 gcc 中使用 -Wall

    我可以像下面这样重写那段代码::

    #include <stdio.h>
    
    #define ARRAY_LEN 30
    
    int match(char * restrict str1,char * restrict str2)
    // If str1 and str2 is is the sole agencies dealing with the strings
    // then 'restrict' helps compiler with some optimizations.
    {
    size_t count1=0,count2=0;
    while(str1[count1++] != '\0')
      ;;
    while(str2[count2++] != '\0')
      ;;
    return count1==count2;
    // Ideally  count1-1 == count2 -1
    // But does that make any difference?
    }
    
    int main(int argc,char * argv[])
    {
      char str1[ARRAY_LEN];
      char str2[ARRAY_LEN]; // No harm doing so.
      signed x; // If you want to check against EOF
      int count=0;
      while((x=getchar()) != '\n' && x!=EOF )
        // You need to implement bounds check.
        {
          if(count < (ARRAY_LEN - 1))
          {
            str1[count++]=x;
          }
          else
          {
            // C input is buffered
            // so you need to clear the buffer if the string first entered was larger than 30 characters
            while((x=getchar()) != '\n' && x!=EOF )
              ;;
            break;
          }
        }
      // C input is buffered
      // so you need to clear the buffer if the string first entered was larger than 30 characters
    
    
      str1[count] = '\0' ; // Null terminating
      count = 0; // Reset count
    
    while((x=getchar()) != '\n' && x!=EOF )
        // You need to implement bounds check.
        {
          if(count < (ARRAY_LEN - 1))
          {
            str2[count++]=x;
          }
          else
          {
            // C input is buffered
            // so you need to clear the buffer if the string first entered was larger than 30 characters
            while((x=getchar()) != '\n' && x!=EOF )
              ;;
            break;
          }
        }
      str2[count] = '\0' ; // Null terminating
      printf("Arrays are of %s length\n",match(str1,str2)?"same":"different");
      return 0;
    }
    

    编辑: EOF 宏定义为 -1。为了适应 x 需要是一个有符号整数。阅读与this 答案内联的答案。

    【讨论】:

    • EOF 不是字符,因此循环条件具有依赖于实现的行为。
    • @PierceGriffiths 没错,我已经在问题comment 中提到了这一点。改了谢谢。
    猜你喜欢
    • 2016-02-19
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    相关资源
    最近更新 更多