0

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!

7
  • 1
    You have allocated structs before this? You have initialized each pointer in the "array" to NULL? If you're using malloc it will not initialize the memory it allocate for you. Commented Apr 23, 2014 at 6:05
  • 2
    How do you initialize structs and no_of_pointers? Commented Apr 23, 2014 at 6:06
  • 3
    Note that struct is a keyword and cannot be used as a variable name. This means we aren't looking at your real code, which means there may be other extra errors in the code. It's best, in general, to show an extract from your real code rather than to misparaphrase it. Commented Apr 23, 2014 at 6:07
  • Sorry! I was just cutting out unimportant stuff to get to the point. Imagine no_of_pointers has been previously set and there's a preprocessor thingy saying struct struct { etc. etc. In this case, struct is a type of struct - sorry, a bit confusing. Commented Apr 23, 2014 at 6:09
  • 1
    You should make You might a Minimal, Complete, and Verifiable example and show us. Commented Apr 23, 2014 at 6:15

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You can use the reallocarray() function to resize structs, such that a new variable where sizeof(x) == sizeof(struct) may be stored there. See my answer for more information
0

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.

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.