简体   繁体   English

如何使用指向结构元素的指针?

[英]How can I use pointers to structure elements?

I have the following "test code": 我有以下“测试代码”:

#include <stdlib.h>
#include <stdio.h>
...

struct data{
  char *name;
  void *value;
};

struct g_arg{
  char *info1;
  char *info2;
  int info3;
};


int main()
{
  char *some_info = "information to store in g";

  struct g_arg g;

  struct data d[] = {
    {"info1=", &g.info1},
    {"info2=", &g.info2},
    {"info3=", &g.info3},
    {NULL, NULL},
  };

  ...

}

I want to insert a value in the g struct elements, passing through d struct. 我想通过d结构在g结构元素中插入一个值。 For instance, I'd like to make something like this: 例如,我想做这样的事情:

d[0].value = some_info;

After the execution of this instruction, g.info1 should contains the string some_info. 执行该指令后,g.info1应该包含字符串some_info。

This is only an example code. 这只是一个示例代码。 I have to make something similar in a complex program. 我必须在复杂的程序中做类似的事情。 I searched for other questions like this, but I don't find a solution. 我搜索了其他类似问题,但没有找到解决方案。


Thank you to all of you guys. 谢谢大家。 I make some progress, but the problem is not solved yet. 我取得了一些进展,但问题尚未解决。 I'll try to explain a little better. 我会尝试解释得更好。 Now, if I do this: 现在,如果我这样做:

printf( "content of g.info1: %s", (char*)(d[0].value) );

I can read the content of g.info , by accessing to d structure. 我可以通过访问d结构来读取g.info的内容。 But I need to write inside g.info , by "writing" something in d[i].value . 但是我需要通过在d[i].value “编写”某些内容来在g.info编写内容。

your g_arg pointers info1 and info2 need to be tied to some memory to be able to assign to them latter. 您的g_arg指针info1和info2需要绑定到一些内存,以便以后可以分配给它们。

Since those are pointers, you cannot assign one string to another, so use some thing like strcpy. 由于这些是指针,因此您无法将一个字符串分配给另一个字符串,因此请使用诸如strcpy之类的东西。

Also if you know the sizes, you can make info1 and info2 as char arrays instead of having to dynamically allocate memory for them. 另外,如果知道大小,则可以将info1和info2设为char数组,而不必为它们动态分配内存。

Point 1. allocate memory to g.info1 using malloc() [or family]. 点1.使用malloc() [或family]将内存分配给g.info1

Point 2 . 点2。 use strcpy() to copy the data to d[0].value 使用strcpy()将数据复制到d[0].value

Point 3. You don't need to use &g.info1 , g.info1 will suffice. &g.info1 3。您不需要使用&g.info1g.info1就足够了。

In your code, g.info1 has type char * , so &g.info1 has type char ** . 在您的代码中, g.info1类型为char * ,所以&g.info1类型为char ** You can certainly assign that to the void * element value of a struct data , but it is a pointer to a (maybe) string, not a string itself. 您当然可以将其分配给struct datavoid *元素value ,但这是指向 (也许)字符串的指针 ,而不是字符串本身。

Moreover, the result is a pointer to a stack-allocated pointer. 此外,结果是指向堆栈分配指针的指针。 If this pointer value survives past the end of the function then it will cease to be valid, and any attempt to dereference it will produce undefined behavior. 如果该指针值在函数结束后仍然存在,则它将不再有效,并且任何对其进行取消引用的尝试都将产生未定义的行为。

If you want to copy string values into elements of array d instead of assigning pointers to them as you now do, then you must first have strings to copy. 如果要将字符串值复制到数组d元素中,而不是像现在那样将指针分配给它们,则必须首先要复制字符串。 In that case, you can use strdup() to allocate space and copy the string in one step: 在这种情况下,可以使用strdup()分配空间并一步复制字符串:

struct g_arg g = { "info 1", "info 2", 42 };

struct data d[] = {
    {"info1=", NULL},
    {"info2=", NULL},
    {"info3=", NULL},
    {NULL, NULL},
};
d[0].value = strdup(g.info1);
d[1].value = strdup(g.info2);
d[2].value = malloc(sizeof(int));
*((int *) d[2].value) = g.info3;

Note that in this case, all the resulting pointers recorded in the value members of elements of d refer to dynamically allocated memory. 请注意,在这种情况下,记录在d元素的value成员中的所有结果指针均指的是动态分配的内存。 This allows them to remain valid past the function's exit, but it requires the program to later free() them to avoid leaking memory. 这使它们在函数退出后仍保持有效,但是它要求程序稍后free()它们以避免内存泄漏。

wrt to d[0].value = some_info; wrt到d[0].value = some_info; this would in theory work if some_info was already an object of type struct g_arg whose member info1 was already set to a string. 如果some_info已经是struct g_arg类型的对象(其成员info1已设置为字符串),则从理论上讲这是struct g_arg的。

But it seems that you want some_info to be a string ( char * , C style); 但是似乎您希望some_info是一个字符串( char * ,C风格);

So you'll need to reference the info1 member of d[0].value . 因此,您需要引用d[0].valueinfo1成员。 So you'll need to cast the void* to the appropriate data type (struct g_arg *)(d[0].value) and then access the member info1 , like so: ((struct g_arg *)(d[0].value))->info1 . 因此,您需要将void* (struct g_arg *)(d[0].value)转换为适当的数据类型(struct g_arg *)(d[0].value) ,然后像这样访问成员info1((struct g_arg *)(d[0].value))->info1

Since info1 is a char * you should use strdup() to copy the string into an appropriately allocated buffer or manually allocate the memory for the string before copying. 由于info1是一个char *,因此应使用strdup()将字符串复制到适当分配的缓冲区中,或者在复制之前为该字符串手动分配内存。 You end up with this: 您最终得到以下结果:

((struct g_arg *)(d[0].value))->info1 = strdup(some_info);

Remember to free() the memory when you're done. 完成后,请记住free()内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM