Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 313
Lipika Deka Avatar asked Jun 09 '11 17:06

Lipika Deka


People also ask

How do you initialize a pointer to a structure?

Initialization of The Structure Pointer struct Student { int id; char name[30]; int age; char gender[15]; }; // create struct variable struct Student student1; // define a pointer with initialization struct Student *ptr = &student1; The above piece of code is declared a pointer to structure as well as initialize it.

What is initializing a pointer?

You need to initialize a pointer by assigning it a valid address. This is normally done via the address-of operator ( & ). The address-of operator ( & ) operates on a variable, and returns the address of the variable. For example, if number is an int variable, &number returns the address of the variable number .

How a pointer to a structure is declared in C?

Structure pointer in C is declared using keyword struct followed by structure name to which pointer will point to follower by pointer name. A structure pointer can only hold the address of a variable of the same structure type used in its declaration.

How do you initialize the structure?

An initializer for a structure is a brace-enclosed comma-separated list of values, and for a union, a brace-enclosed single value. The initializer is preceded by an equal sign ( = ).


2 Answers

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.

like image 67
John Bode Avatar answered Sep 20 '22 15:09

John Bode


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;
like image 27
Blagovest Buyukliev Avatar answered Sep 20 '22 15:09

Blagovest Buyukliev