c - 使用指针数组有问题吗?

标签 c pointers

代码:

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

最佳答案

如果您希望它在循环之外持续,请使用strcpystrtok 可以重用相同的指针。

关于c - 使用指针数组有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5125676/

相关文章:

c++ - 在两个数据结构中持有相同的唯一指针

c - 进入嵌入式

c - 我会为这个功能做什么样的功能原型(prototype)?

c - 如果未定义函数,C 中是否有办法让编译器/链接器给出错误?

Delphi - 推迟对输出参数的赋值

c - 将二维数组传递给 C 函数

c - C中通过指针修改函数中数组的值

c - 为什么这个 print 语句可以阻止 C 程序崩溃?

c# - ILGenerator:如何使用非托管指针? (我得到一个 VerificationException)

c - 使用 out params 查找 float 组中的最小和最大元素