Lipika Deka
Lipika Deka

Reputation: 3884

Initializing a pointer to a structure

another linked question is Segmentation fault while using 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.

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.

Upvotes: 9

Views: 85895

Answers (6)

John Bode
John Bode

Reputation: 123458

Here's the answer to the question I think you'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->incall like so:

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

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

Upvotes: 19

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43498

The corresponding struct initialiser can look like this:

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

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

struct thread_data *b = &a;

Upvotes: 10

Mark Wilkins
Mark Wilkins

Reputation: 41232

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 *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 );

Upvotes: 1

Chobeat
Chobeat

Reputation: 3535

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

Upvotes: 1

Heath Hunnicutt
Heath Hunnicutt

Reputation: 19457

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] = '\0';

    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);
    } 
}

Upvotes: 1

Julio Guerra
Julio Guerra

Reputation: 5661

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.

Upvotes: 2

Related Questions