简体   繁体   English

在数组segfault中存储char

[英]Storing a char in an array segfault

I'm getting a segfault in my code and I'm not sure why. 我的代码中出现了段错误,我不确定原因。 I read through a file and count the number of lines in order to dynamically allocate my arrays. 我读了一个文件并计算了行数,以便动态分配我的数组。 I then rewind the file, read the data in the file, storing the data into variables, and then storing the read variables into arrays, but I'm having trouble with chars. 然后我回放文件,读取文件中的数据,将数据存储到变量中,然后将读取的变量存储到数组中,但是我遇到了chars问题。

   ...
   char *aname = malloc(sizeof(char) * 3); 
   ... 
   // get # lines in file (count)
   ... 
   char *aname_seen = malloc(count * (sizeof(char) * 3));
   ...
   rewind(file);
   while (fgets(buff, sizeof buff, file) != NULL) 
   {
      if (sscanf(buff, "%s %d %s %s %d %lf %lf %lf %lf %lf\n", 
         atm, &serial, aname, resName, &resSeq, &x, &y, &z, 
         &occupancy, &tempFactor) == 10)
      {
         aname_seen[i] = *aname;
         printf("%d: %s vs %s\n", i, aname, aname_seen[i]);

         i++;

      } // end sscanf if-loop

   } // end while loop

I can print aname with printf("%d: %s\\n", i, aname) and get the expected output, but I'm getting Segmentation fault (core dumped) when I try printf("%d: %s vs %s\\n", i, aname, aname_seen[i]) . 我可以使用printf("%d: %s\\n", i, aname)打印aname并获得预期的输出,但是当我尝试printf("%d: %s vs %s\\n", i, aname, aname_seen[i])时,我遇到了Segmentation fault (core dumped) printf("%d: %s vs %s\\n", i, aname, aname_seen[i])

This while loop + nested if loop is the same convention I use to count the number of lines, so i will increment up to count. 这个while循环+嵌套if循环是我用来计算行数的相同约定,所以i会递增计数。 Am I incorrectly allocating aname_seen and not actually giving it count number of char*3 elements? 我是否错误地分配aname_seen并且实际上没有给它count char*3元素的数量? I'm not well versed in messing with char's. 我不太擅长搞乱char。 More of a numerical fortran buff, so I need some direction. 更多的数字fortran buff,所以我需要一些方向。

Thanks in advance! 提前致谢!

The %s format specifier is supposed to correspond to a char * argument. %s格式说明符应该对应于char *参数。 In your case, aname_seen[i] is a char , which gets promoted to an int for the purpose of passing to a variadic function ( printf ). 在你的例子中, aname_seen[i]是一个char ,它被提升为一个int ,目的是传递给一个可变函数( printf )。 An int is not a char * . int不是char *

Perhaps you meant one of these: 也许你的意思是其中之一:

printf("%d: %s vs %c\n", i, aname, aname_seen[i]);
printf("%d: %s vs %s\n", i, aname, &aname_seen[i]);

If neither of these solve your problem, please explain precisely the behaviour you expect from this expression and give us a minimal, compilable testcase. 如果这些都没有解决您的问题,请准确解释您对此表达式的期望,并为我们提供一个最小的,可编译的测试用例。 Your current testcase isn't compilable. 您当前的测试用例不可编译。

you way you defined aname_seen is a pointer to a char array 你定义的方式aname_seen是一个指向char数组的指针

char *aname_seen = malloc(count * (sizeof(char) * 3));

so aname_seen[i] is a char 所以aname_seen[i]是一个char

so the 所以

printf("%d: %s vs %s\n", i, aname, aname_seen[i]);

should be 应该

printf("%d: %s vs %c\n", i, aname, aname_seen[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM