C 初学者问题:指向指针的指针/无法从函数外部访问值

发布于 2024-11-07 05:16:51 字数 964 浏览 1 评论 0原文

我正在向我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

许仙没带伞 2024-11-14 05:16:51

您可能想要:

resultSet = malloc(sizeof(void *)*(int)mysql_num_rows);

而不是:

resultSet = malloc(sizeof(void)*(int)mysql_num_rows);

因为您需要指针而不是字节。

You probably want:

resultSet = malloc(sizeof(void *)*(int)mysql_num_rows);

Instead of:

resultSet = malloc(sizeof(void)*(int)mysql_num_rows);

as you need pointers not bytes.

二手情话 2024-11-14 05:16:51

如果你想使用 resultSet 作为返回参数,你需要制作函数签名

int getItems(char * cmd, char ***resultSet)

并在函数中使用它,例如

*resultSet = malloc(sizeof(char)*(int)mysql_num_rows)

函数调用可能看起来像

char** results;
nitems = getItems(somecmd, &results); 

更好和更简单的可能是保持原样并在函数调用之前进行分配。

If you want to use resultSet as a return parameter, you need to make the function signature

int getItems(char * cmd, char ***resultSet)

and use it in the function like

*resultSet = malloc(sizeof(char)*(int)mysql_num_rows)

The function call could look like

char** results;
nitems = getItems(somecmd, &results); 

Better and simpler is probably to leave it as it is and make the allocation before the function call.

许你一世情深 2024-11-14 05:16:51

这里有两个问题:

  • 如所示,结果集的分配是错误的。
  • 其次,strlen(row[i]+1)将计算row[i]+1引用的内存位置的字符串长度。该长度将比 row[i] 的字符串长度减 1。由于您基本上是在复制以 null 结尾的字符串,因此请使用该函数: resultSet[counter] = strdup((char *)row[i]);

无需 malloc 和 strcpy。

Two problems here:

  • As indicated the allocation of resultset is wrong.
  • Secondly, strlen(row[i]+1) will calculate the string length of the memory location referenced by row[i]+1. This length will be 1 less then the string length of row[i]. Since you're basically duplicating a null terminated string, use that function: resultSet[counter] = strdup((char *)row[i]);

No need to malloc and strcpy.

血之狂魔 2024-11-14 05:16:51

据我所见,您有一个要用数据填充的 char 数组数组。您的初始分配应更改为:

resultSet = malloc(sizeof(char*)*(int)mysql_num_rows);

为了反映这一点(注意 char*)。

另外:

resultSet[counter] = malloc(sizeof(char)*strlen(row[i])+1);

这一行在技术上是正确的,但您应该将其更改为:

resultSet[counter] = malloc(sizeof(char) * (strlen(row[i])+1) );

为了反映您实际想要做的事情(这样做的原因是 C 执行算术的顺序,如果您尝试这样做,第一种方法会产生错误的结果除 char/unsigned char 之外的任何其他数据类型)。

最后,我希望您希望以某种方式返回二维数组的值。有两种方法可以做到这一点:

  1. 让函数返回一个 char**(失败时返回 NULL)。此处不需要 resultSet 作为输入参数。
  2. 整数
    getItems(char * cmd, char ***resultSet)

请注意,函数中包含 resultSet 的所有内容都必须更改为 *resultSet。然后可以使用以下命令调用该函数:

char **result;
int status = getItems(cmd, &result);

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:

resultSet = malloc(sizeof(char*)*(int)mysql_num_rows);

To reflect this (notice char*).

Also:

resultSet[counter] = malloc(sizeof(char)*strlen(row[i])+1);

This row is technically correct, but you should change it to:

resultSet[counter] = malloc(sizeof(char) * (strlen(row[i])+1) );

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:

  1. Make the function return a char** (return NULL on failure). You don't need resultSet as an input parameter here.
  2. int
    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:

char **result;
int status = getItems(cmd, &result);
不甘平庸 2024-11-14 05:16:51

您可能只需将返回值(或参数)设置为 &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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文