简体   繁体   English

在C中打印字符串数组的第一个元素时出现分段错误

[英]Segmentation fault while printing first element of string array in c

I am scanning lines from a textfile and putting elements into specific arrays, sscanf is working fine and inserting variables into arrays as well BUT printing out first element of string array result in segmentation fault(printing works for first elements from other arrays and for rest elements in string array) 我正在扫描文本文件中的行并将元素放入特定的数组中,sscanf工作正常并将变量插入数组中,但打印出字符串数组的第一个元素会导致分段错误(打印适用于其他数组中的第一个元素以及其余元素在字符串数组中)

Here is my code: 这是我的代码:

char **shipTable = NULL;
char *notimportant;
double *dirTable;
double *numTable;
double *speedTable;
double *latTable;
double *lngTable;

void scanShips(){
   char fname[30];
   char line[150];
   char shipname[10];
   double lat;
   double lng;
   double speed;
   double dir;
   int numofShips;
   numofShips=1;
   FILE *myfile;



   printf("give file name containing ships /n");
   scanf("%s",fname);
   myfile = fopen(fname,"rt");
   fgets(line,80,myfile);
   sscanf(line, "%d %d %d %d %d %d", &day, &month, &year, &h, &min, &sec);

   while ( fgets( line,100,myfile) != 0 ) {
       sscanf(line, "%s %lf %lf %lf %lf", shipname, &lat, &lng, &dir, &speed);
       printf("%s",shipname);
       printf("\n");

       shipTable = realloc( shipTable, numofShips*sizeof(char*) );
       latTable = realloc( latTable, numofShips*sizeof(double) );
       lngTable = realloc( lngTable, numofShips*sizeof(double) );
       dirTable = realloc( dirTable, numofShips*sizeof(double) );
       speedTable = realloc( speedTable, numofShips*sizeof(double) );
       shipTable[numofShips-1]=malloc((10)*sizeof(char));


       strcpy (shipTable[numofShips-1],shipname);
       dirTable[numofShips-1]=dir;
       speedTable[numofShips-1]=speed;
       latTable[numofShips-1]=lat;
       lngTable[numofShips-1]=lng;

       numofShips++;
   //printf("%d",numofShips);

    }
    fclose ( myfile);

//note:
printf("%s",shipTable[0]);//<---Segmentation fault
printf("%s",shipTable[1]);//<---Perfectly fine, as well as rest fo the array
printf("%f",dirTable[0]);//<---Perfectly fine, as well as rest of "double" arrays

Example file : 示例文件:
13 11 2011 13 04 00 13 11 2011 13 04 00
GW1927 52.408 -4.117 1.000 0.000 GW1927 52.408 -4.117 1.000 0.000
GS452 51.750 -4.300 5.000 10.000 GS452 51.750 -4.300 5.000 10.000
EI597 52.100 -6.000 90.000 12.000 EI597 52.100 -6.000 90.000 12.000
EI600 52.000 -5.900 10.000 15.000 EI600 52.000 -5.900 10.000 15.000
EI601 54.000 -5.900 10.000 15.000 EI601 54.000 -5.900 10.000 15.000

Shipname length will never be longer than 9 chars. 船名的长度绝不能超过9个字符。

Your program is incomplete. 您的程序不完整。 Once I make it complete (by adding necessary #include s, defining day , month , etc.), it compiles and works just fine. 一旦完成(通过添加必要的#include ,定义daymonth等),它便可以编译并正常工作。

Conclusion: you are not telling us the whole story, and the bug is in the code that you didn't show. 结论:您并没有告诉我们整个故事,并且该错误存在于您未显示的代码中。

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

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