为什么这个printf语句或缺少的语句会改变for循环的效果?

时间:2022-03-08 09:40:04

First section of code:

第一部分代码:

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main(void)
{
    string name = GetString();
    int n = strlen(name);
    int j = 0;
    int c = 0;
    char initials[j];    
    char input[c];
    char space[] = {' '};

    for (int i = 0, c = 0; i < n; i++, c++)
    {        
        input[c] = name[i];

        printf("%c, %c\n", name[i], input[c]);     
    } 

Problem area:

    printf("%d\n", n);
    for (int i = 0, c = 0; i < n; i++, c++)
    {                 
        if (input[c] != space[0])
        {
            initials[j] = input[c];
            j++;
            break;
        }
        printf("loop test\n");
    }

    j = 0;

    printf("%c\n", initials[j]);      
}

If my input is:

如果我的输入是:

     hello

Then my output is what I want (loop test == number of spaces before input):

然后我的输出是我想要的(循环测试==输入前的空格数):

loop test
loop test
loop test
loop test
loop test
h

Unless, I remove:

除非,我删除:

printf("%d\n", n);

Then if my input starts with >= 4 spaces, my output is:

然后如果我的输入以> = 4个空格开头,我的输出是:

loop test
loop test
loop test
loop test
// blank line
// blank line         

the two comments are actual blank lines in the output

这两个注释是输出中的实际空行

*Sorry for some of the erroneous printf statements, I was trying to identify the bug.

*对于一些错误的printf语句,我很抱歉,我试图找出错误。

2 个解决方案

#1


One major problem is here:

一个主要问题是:

int c = 0;
 ...
char input[c];

input[] is made a zero length array. Then the code merrily writes beyond the end of it which amounts to random writing on other parts of the stack frame.

input []是一个零长度数组。然后代码快速写入超出它的结尾,这相当于在堆栈帧的其他部分上随机写入。

The fix is to properly size the array before writing into it.

修复是在写入数组之前正确调整数组的大小。

There is also

还有

int j = 0;
 ...
char initials[j];    

#2


You probably want something more like:

你可能想要更像的东西:

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main(void)
{
  string name = GetString();
  int n = strlen(name);
  int j = 0;
  int c = 0;
  char *initials = calloc(n,1);    
  char *input = calloc(n,1);

  for (int i = 0, c = 0; i < n; i++, c++)
  {        
    input[c] = name[i];

    printf("%c, %c\n", name[i], input[c]);     
  } 

  printf("%d\n", n);
  for (int c = 0; c < n; c++) // you weren't using i in the loop
  {                 
    if (input[c] != ' ')
    {
        initials[j] = input[c];
        j++;
        break;
    }
    printf("loop test\n");
  }

  j = 0;

  printf("%c\n", initials[j]);      

  free(initials);
  free(input);
}

#1


One major problem is here:

一个主要问题是:

int c = 0;
 ...
char input[c];

input[] is made a zero length array. Then the code merrily writes beyond the end of it which amounts to random writing on other parts of the stack frame.

input []是一个零长度数组。然后代码快速写入超出它的结尾,这相当于在堆栈帧的其他部分上随机写入。

The fix is to properly size the array before writing into it.

修复是在写入数组之前正确调整数组的大小。

There is also

还有

int j = 0;
 ...
char initials[j];    

#2


You probably want something more like:

你可能想要更像的东西:

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main(void)
{
  string name = GetString();
  int n = strlen(name);
  int j = 0;
  int c = 0;
  char *initials = calloc(n,1);    
  char *input = calloc(n,1);

  for (int i = 0, c = 0; i < n; i++, c++)
  {        
    input[c] = name[i];

    printf("%c, %c\n", name[i], input[c]);     
  } 

  printf("%d\n", n);
  for (int c = 0; c < n; c++) // you weren't using i in the loop
  {                 
    if (input[c] != ' ')
    {
        initials[j] = input[c];
        j++;
        break;
    }
    printf("loop test\n");
  }

  j = 0;

  printf("%c\n", initials[j]);      

  free(initials);
  free(input);
}