Linux 初始化指向结构的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6296880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:25:18  来源:igfitidea点击:

Initializing a pointer to a structure

clinuxinitializationstructure

提问by Lipika Deka

another linked question is Segmentation fault while using strcpy()?

另一个链接问题是使用 strcpy() 时出现分段错误?

I have a structure:

我有一个结构:

struct thread_data{    
    char *incall[10];
    int syscall arg_no;    
    int client_socket;
 }; 

How do I initialize a pointer to a structure of type above as well as initialize the pointer to the 10 strings (incall[]) inside the structure.

如何初始化指向上述类型结构的指针以及初始化指向结构内 10 个字符串 (incall[]) 的指针。

Do I first initialize the strings and then the structure.

我是否首先初始化字符串,然后初始化结构。

Thanks.

谢谢。

An edit: I guess I used the wrong word and should have said allocate. Actually I am passing this structure as an argument to threads. No. of threads is not fixed and the data structure sent as argument must be unique for each thread and "thread safe" i.e cannot be changed by other threads.

编辑:我想我用错了词,应该说分配。实际上,我将此结构作为参数传递给线程。线程数不是固定的,作为参数发送的数据结构对于每个线程必须是唯一的,并且“线程安全”即不能被其他线程更改。

采纳答案by John Bode

Here's the answer to the question I thinkyou're asking:

以下是我认为您要问的问题的答案:

/**
 * Allocate the struct.
 */
struct thread_data *td = malloc(sizeof *td);

/**
 * Compute the number of elements in td->incall (assuming you don't
 * want to just hardcode 10 in the following loop)
 */
size_t elements = sizeof td->incall / sizeof td->incall[0];

/**
 * Allocate each member of the incall array
 */
for (i = 0; i < elements; i++)
{
  td->incall[i] = malloc(HOWEVER_BIG_THIS_NEEDS_TO_BE);
}

Now you can assign strings to td->incalllike so:

现在您可以td->incall像这样分配字符串:

strcpy(td->incall[0], "First string");
strcpy(td->incall[1], "Second string");

Ideally, you want to check the result of each mallocto make sure it was successful before moving on to the next thing.

理想情况下,您希望malloc在继续下一件事之前检查每个的结果以确保它成功。

回答by Julio Guerra

It depends if you need your variable to be temporary or not:

这取决于您是否需要临时变量:

struct thread_data data; // allocated on the stack
// initialize your data.* field by field.

struct thread_data* data = malloc(sizeof (struct thread_data)); // allocated on the heap
// initialize your data->* field by field.

In both cases, you have to allocate your structure first to be able to access its fields.

在这两种情况下,您都必须首先分配您的结构才能访问其字段。

回答by Heath Hunnicutt

You can write something just like this:

你可以这样写:

#define ARRAY_DIMENSION(a) (sizeof(a)/sizeof((a)[0]))

void init_func(void)
{
    struct thread_data arg_to_thread;
    int i;
    char buffer[100];

    buffer[0] = '
struct thread_data *td;
int i;
// allocate memory for the structure
td = malloc( sizeof( struct thread_data ));
// then allocate/initialize the char* pointers.  It isn't clear
// what you want in them ... pointers to existing data?  Pre-allocated
// buffer?  This just allocates a fixed size buffer,
for ( i = 0; i < sizeof( td->incall ) / sizeof( td->incall[0] ); i++ )
   td->incall[i] = malloc( 42 );
'; for ( i = 0; i < ARRAY_DIMENSION(arg_to_thread.incall); i ++ ) { /* Do something to properly fill in 'buffer' */ arg_to_thread.incall[i] = strdup(buffer); } }

回答by Chobeat

i think malloc(sizeof(struct thread_data));should work, does it not?

我认为 malloc(sizeof(struct thread_data));应该工作,不是吗?

回答by Mark Wilkins

Here is another possibility. It is unclear to me what you want the values initialized to, so this just grabs a number out of the air, which is almost certainly wrong.

这是另一种可能性。我不清楚你想要初始化的值是什么,所以这只是从空中获取一个数字,这几乎肯定是错误的。

struct thread_data a = {
  .incall = {"a", "b", "c", "d", "e"},
  .arg_no = 5,
  .client_socket = 3
};

回答by Blagovest Buyukliev

The corresponding structinitialiser can look like this:

相应的struct初始化程序可能如下所示:

struct thread_data *b = &a;

Then you can assign the address of this to a pointer:

然后你可以将 this 的地址分配给一个指针:

##代码##