C 初学者问题:指向指针的指针/无法从函数外部访问值
我正在向我的 MySQL 函数传递一个指向指针 (**resultSet) 的指针。
以下是我如何从函数内复制 MySQL 数据的摘录:
int
getItems(char * cmd, char **resultSet)
{
...
MYSQL initialisations and set-up
...
resultSet = malloc(sizeof(char)*(int)mysql_num_rows);
while((row = mysql_fetch_row(result)))
{
for (i=0 ; i < mysql_num_fields(result); i++)
{
printf("%i: \t", i);
resultSet[counter] = malloc(sizeof(char)*strlen(row[i])+1);
strcpy(resultSet[counter], row[i]);
printf("%s\n", resultSet[counter]);
}
printf("---------------------\n");
counter++;
}
...
MYSQL cleaning up
...
return 0;
}
使用 From 调用它在 main 中,
getItems(cmd, resultSet);
在我的 getItems 函数中
printf("%s\n", resultSet[0]);
这似乎有效。
但是,如果我尝试从函数外部访问它,则会出现分段错误。这是为什么呢?
I'm passing a pointer to a pointer (**resultSet) to my MySQL function.
Here's an excerpt on how I copy the MySQL data from within the function:
int
getItems(char * cmd, char **resultSet)
{
...
MYSQL initialisations and set-up
...
resultSet = malloc(sizeof(char)*(int)mysql_num_rows);
while((row = mysql_fetch_row(result)))
{
for (i=0 ; i < mysql_num_fields(result); i++)
{
printf("%i: \t", i);
resultSet[counter] = malloc(sizeof(char)*strlen(row[i])+1);
strcpy(resultSet[counter], row[i]);
printf("%s\n", resultSet[counter]);
}
printf("---------------------\n");
counter++;
}
...
MYSQL cleaning up
...
return 0;
}
Calling it in main with
getItems(cmd, resultSet);
From within my getItems function this
printf("%s\n", resultSet[0]);
seems to work.
However, if I try to access it from outside my function I get a segmentation fault. Why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能想要:
而不是:
因为您需要指针而不是字节。
You probably want:
Instead of:
as you need pointers not bytes.
如果你想使用 resultSet 作为返回参数,你需要制作函数签名
并在函数中使用它,例如
函数调用可能看起来像
更好和更简单的可能是保持原样并在函数调用之前进行分配。
If you want to use resultSet as a return parameter, you need to make the function signature
and use it in the function like
The function call could look like
Better and simpler is probably to leave it as it is and make the allocation before the function call.
这里有两个问题:
resultSet[counter] = strdup((char *)row[i]);
无需 malloc 和 strcpy。
Two problems here:
resultSet[counter] = strdup((char *)row[i]);
No need to malloc and strcpy.
据我所见,您有一个要用数据填充的 char 数组数组。您的初始分配应更改为:
为了反映这一点(注意 char*)。
另外:
这一行在技术上是正确的,但您应该将其更改为:
为了反映您实际想要做的事情(这样做的原因是 C 执行算术的顺序,如果您尝试这样做,第一种方法会产生错误的结果除 char/unsigned char 之外的任何其他数据类型)。
最后,我希望您希望以某种方式返回二维数组的值。有两种方法可以做到这一点:
getItems(char * cmd, char ***resultSet)
请注意,函数中包含 resultSet 的所有内容都必须更改为 *resultSet。然后可以使用以下命令调用该函数:
As far as I can see, you have an array of char arrays that you want to fill with your data. Your initial allocation should be changed to:
To reflect this (notice char*).
Also:
This row is technically correct, but you should change it to:
To reflect what you actually want to do (the reason for this is the order that C performs arithmetics, and the first approach would yield wrong results if you tried to do it on any other datatype than char/unsigned char).
Finally, I expect that you want to return the value of the 2d-array somehow. There are two ways to do this:
getItems(char * cmd, char ***resultSet)
Notice that everything with resultSet in the function would have to change to *resultSet. The function can then be called with:
您可能只需将返回值(或参数)设置为 &resultSet。但如果您显示了函数的参数以及函数的调用,我们就可以确定。
You probably just need to set the return value (or the parameter) to &resultSet. But we could know for sure if you showed the parameters to the function and the calling of the function.