【发布时间】:2013-11-24 04:55:32
【问题描述】:
我在 C 语言中遇到了指针问题。我需要将每行的第一个元素放入一个数组中。
重要部分:
char shipname[10];
char **shipTable = NULL;
while ( fgets( line,100,myfile) != NULL ) {
sscanf(line, "%s %lf %lf %lf %lf", shipname, &lat, &lng, &dir, &speed);
shipTable = realloc( shipTable, numofShips*sizeof(char*) );
shipTable[numofShips-1]=malloc((10)*sizeof(char));
(shipTable)[numofShips-1] = shipname;
//char *shipname=malloc((10)*sizeof(char));
numofShips++;
}
当我打印出我的 shipTable 时,每个元素都是相同的,我尝试了我想出的 & 和 * 的每种组合。
【问题讨论】:
标签: c string pointers realloc arrays