代碼:使用指針數組的問題?
char *m[10];
char * s;
int lcounter=0;
int sd=0;
char mem_buf [ 500 ];
while (fgets (mem_buf, sizeof mem_buf, infile) != NULL)
{
m[lcounter] =(char *) malloc(10*sizeof(char));
item = strtok(mem_buf,delims);
m[lcounter]=item;
printf("\n value inside==== :%s:",m[lcounter]);
lcounter=lcounter+1;
}
for(i=0;i<lcounter;i++)
{
printf("\n value outside==== :%s:",m[sd]);
sd++;
}
輸入:
goo|
bbb|
ccc|
當我執行此我得到以下的輸出:
value inside==== : goo
value inside==== : bbb
value inside==== : ccc
value outside====:ccc
value outside====:ccc
value outside====:ccc
但我需要這樣的:
value outside====:goo
value outside====:bbb
value outside====:ccc
@stefan您的評論沒有任何意義。罪魁禍首是'm [lcounter] = item'。 – chrisaycock 2011-02-26 07:50:43
@chrisaycock:你是對的:)我會刪除它,我錯過了strtok;) – stefan 2011-02-26 07:52:12