0

在交流

char write[500][255];

void func1()
{
 int i=0;

 while(i<100)
 {
  char text[255];
  sprintf(text, "abcdefgh %d - %d - %d", i, i*2, i*3);
  strcpy(write[i],text);
  i++;
 }
 func2(&write); 
}

在公元前

void func2(char *write)
{
 int i=0;

 while(i<100)
 {
  printf("%d --> %s", i, &write[i]);
  i++;
 }
}

结果是:

abcdefgh 0 - 0 - 0
bcdefgh 0 - 0 - 0
cdefgh 0 - 0 - 0
defgh 0 - 0 - 0
efgh 0 - 0 - 0
fgh 0 - 0 - 0
gh 0 - 0 - 0
...

我也收到了这个警告func2(&write);

passing argument 1 of 'func2' from incompatible pointer

我不明白为什么结果是这样的。我怎样才能摆脱这个警告。我无法获取 write[] 数组的值。我怎样才能做到这一点?

谢谢


工作代码在这里:http: //ideone.com/HTyZwH

4

1 回答 1

0

您应该将签名更改func2为:

void func2(char write[][255])

请注意,我不相信发布的代码func2实际上会产生输出。鉴于这一行:

printf("%d --> %s", i, write[i]);

假设编译器通过了您将二维char数组转换为 a 的事实,在格式字符串中char*指定%s应该导致printf将存储的字符write[i]视为指向字符串的指针并调用未定义的行为。大概是撞车了。

如果该行改为:

printf("%d --> %s", i, &write[i]);

然后我可以看到您显示的输出正在生成。

于 2013-09-11T15:14:43.357 回答