user3562565
user3562565

Reputation: 11

How to append to a pointer array in c

I have an array of pointers to structs and I'm trying to find a way to fill the first NULL pointer in an array with a new pointer to a struct. i.e. I want to add a new element onto the end of an array. I tried a for loop like this:

struct **structs;

int i;
for(i = 0; i < no_of_pointers; i++) {
        if (structs[i] == NULL) {
              structs[i] = &struct;
        }
}

In theory, this would go through the array and when it finds a null pointer it would initialise it. I realise now that it would initialise all null pointers, not just the first, but when I run it it doesn't even do that. I've tried a while loop with the condition while(structs[i] != NULL) and that just goes on forever, making me think that the issue is with how I'm using NULL. What is the correct way to add a new element to an array of this kind? Is there some function like append(structs, struct) that I don't know of? Thanks!

Upvotes: 0

Views: 13145

Answers (2)

GooseDeveloper
GooseDeveloper

Reputation: 137

According to man malloc:

       void *malloc(size_t size);
       void free(void *ptr);
       void *calloc(size_t nmemb, size_t size);
       void *realloc(void *ptr, size_t size);
       void *reallocarray(void *ptr, size_t nmemb, size_t size);

...

    The reallocarray() function  changes  the  size  of  the  memory  block
    pointed  to  by  ptr to be large enough for an array of nmemb elements,
    each of which is size bytes.  It is equivalent to the call

           realloc(ptr, nmemb * size);

Try implementing a system like this

struct **structs;

int new_struct() {
    static int i = 0; // index of last allocated struct

    i++;
    struct *structp = malloc(sizeof(struct)); // new structure
    // initialize structp here

    reallocarray(structs, i, sizeof(struct));
    structs[i] = structp;

    return i; // use structs[index] to get
}

Then you may invoke new_struct(), which resizes the structs array and appends structp to it. The important part is that

  • a) create_struct returns the index of the newly created struct, and
  • b) it stores a static int i, which keeps track of the size of the structs.

Upvotes: 0

Lee Duhem
Lee Duhem

Reputation: 15121

The length of an array in C is fixed, you cannot change it after you defined an array, which means you cannot add an element to the end of an array. However, unless you defined a constant array, you could assign new values to elements of an array. According to your question description, I believe this is what you want.

Also note that, as other already pointed it out in comments, struct is a keyword of C, therefore

  1. you cannot use it as a type name (as you did in struct **structs)

  2. you also cannot use it as a variable name (as you did in structs[i] = &struct;)

Here is one way to do it:

  1. define an array properly

    struct struct_foo **structp;
    
    structp = malloc (no_of_elements * sizeof(*structp));
    if (structp == NULL) {
            /* error handle */
    }
    

    Note, at here the elements of structp is not initialized, you need to initialize them properly. That is what we are going to do in step 2.

  2. do something with structp, maybe initialize all its elements to NULL or some no-NULL value

  3. find the first no-NULL element in structp, and assign it a new value

    struct struct_foo foo;
    
    for (i = 0; i < no_of_elements; i++) {
            if (structp[i] == NULL) {
                    structp[i] = &foo;
                    break;
            }
    }
    

    Note that this foo also is uninitialized, you may want to initialize it first, or you could initialize it later.

Upvotes: 3

Related Questions