1

I'm trying to create an array of pointers in C. Each value of the array should be a pointer to a struct (let's call it struct Type*).

Should i do

struct Type* myVariable= malloc(sizeof(struct Type*)*MY_SIZE);

or

struct Type** myVariable= malloc(sizeof(struct Type*)*MY_SIZE);

The second one looks like what i should do when i want to create a two dimensional array, which are an array of pointer, and those pointers are used to create arrays of the wanted type. EDIT : But in my case the second dimension size would be only one

The first one looks like a regular array with int* as the contained values type.

How can i pass the good solution to a function (by pointer, not by value because the array may be fairly large) and use it in the fonction ?

3
  • 13
    second one..... Commented Dec 15, 2015 at 21:15
  • 3
    the first creates an array of struct Types, the second is an array struct Type pointers so pick the second, to pass it to a function the function needs to except an argument of type struct Type**, then call your function and pass in myVariable declared the second way Commented Dec 15, 2015 at 21:21
  • 1
    @Ryan Fitzpatrick : Thank you, i completely forgot the basics... Long time no C :'( Commented Dec 15, 2015 at 21:25

2 Answers 2

2

The second one the right solution. However, you'll need to allocate memory for the objects too. Also, make sure to check the value returned by malloc.

// Allocate memory for the array of pointers.
struct Type** myVariable = malloc(sizeof(struct Type*)*MY_SIZE);
if ( myVariable == NULL )
{
   // Deal with error
   exit(1);
}

for (int i = 0; i < MY_SIZE; ++i )
{
   // Allocate memory for the array of objects.
   myVariable[i] = malloc(sizeof(struct Type)*THE_SIZE_IN_THE_OTHER_DIMENSION);
   if ( myVariable[i] == NULL )
   {
      // Free everything that was allocated so far
      for (int j = 0; j < i-1; ++j )
      {
         free(myVariable[j]);
      }
      free(myVariable);

      // Exit the program.
      exit(1);
   }
}

However, if THE_SIZE_IN_THE_OTHER_DIMENSION is going to be 1, you are better off using your first approach.

struct Type* myVariable = malloc(sizeof(struct Type)*MY_SIZE);
                                     // ^^^^^^^^^^^ Drop the *
if ( myVariable == NULL )
{
   // Deal with error
   exit(1);
}
Sign up to request clarification or add additional context in comments.

13 Comments

So if there is a size 1 in the other dimension, i just want one pointer pointing to one value in each myVariable[i], i just do myVariable[i] = malloc(sizeof(struct Type)); right ? Then can i manipulate myVariable content with just myVariable[i] and not myVariable[i][j] (since j would always value 0 in my case)
@Csi, "yes" to the first question, "no" to the second question. When the second dimension is 1, you need to use myVariable[i][0] or *myVariable[i].
If the second dimension is 1, you are better off using the first approach from your question. In that case, you can use myVariable[i] to access the object.
@R Sahu The first one, struct Type* myVariable= malloc(sizeof(struct Type*)*MY_SIZE); right ? So it wasn't a random stupid idea after all
@Csi, that is correct. No, it wasn't a random stupid idea.
|
0

Neither!

Use an idiom that reduces work and errors

pointer = malloc(sizeof *pointer * Number_of_elements);

Or in OP's case "to create an array of pointers in C"

#define ARRAY_N 100
struct Type **myVariable = malloc(sizeof *myVariable * N);
if (myVariable == NULL) Handle_OutOfMemmory();

Now set those pointers to some value

#define M 50
size_t i;
for (i=0; i<N; i++) {
  myVariable[i] = malloc(sizeof *(myVariable[i]) * M);
  if (myVariable[i] == NULL) Handle_OutOfMemmory();

  for (size_t m = 0; m<M; m++) {
    // Initialize the fields of 
    myVariable[i][m].start = 0;
    myVariable[i][m].value = 0.0;
    myVariable[i][m].set = NULL;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.