How do I increment the pointer to a structure which has variable length array?

Raven

I have problem in accessing the pointer to a structure (array passed to function) that has a variable size array.

        char name[20];
        char value[20];
   }param_t;
   typedef struct object {
        char name[20];
        int no_of_params;
        param_t params[];
   };

int main()
{
    int no_of_objs = 3, no_of_params = 5;
    object_t *objs = malloc(no_of_objs * (sizeof(object_t) + (no_of_params * sizeof(param_t)) );
     //...
     //...

     objs++;  //Increment to point to the next item <-- Problem: ignores params[] size

     // blah
     // blah
     // blah

I have allocated memory to have 3 object_t with each object_t storing 5 param_t.

Memory allocation is fine and I could set values to the members. objs[0] is perfectly fine. Now if I increment the objs (objs++) to point to next object, it actually points to previous object's param address. It completely ignores the params[]. Now if I set values for the second *objs, it actually overwrites the params[] of previous *objs.

My question is: Is this a bug in C compiler? If not, how do I traverse the objs?

Paul Ogilvie

As per the answer of Bathsheba, you could better implement it as an array of pointers to the objects:

int no_of_objs = 3, no_of_params = 5;
object_t **objs = malloc(no_of_objs * sizeof(object_t *);
for (int i=0; i<no_of_objs; i++)
    objs[i]= malloc ((sizeof(object_t) + (no_of_params * sizeof(param_t)) );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I initialize a pointer to an Array of structure

How do I increment a variable that has a word within the variable name?

In Swift: How do I decode this JSON (which has variable keys)?

How do I set a variable as value in a pointer array

how can I flatten an 2d numpy array, which has different length in the second axis?

how do you increment a variable using a pointer in C?

How do I atomically increment a variable in Swift?

How do I increment a scss variable?

How do I increment an IntegerField variable in django?

How to add data dynamically in structure which is a Pointer to an array of pointers

How should I allocate my structure(pointer to array of structure)?

How do I increment one value in an array?

JNA: How to specify a variable-length (0+) array in a Structure?

How do I pass a pointer to a local variable?

How do I call a function with pointer variable?

How do I tell if I am using VLA (Variable Length Array)?

Accessing an integer within a structure which has Pointer to structure

How do I select a table column which has the name of a variable in SQL?

How do I pass an id variable to a class which has dependency injection Symfony 3.4

How do I execute a Lisp program which has been stored in a variable?

How do I echo the value of a variable before the code which calculates that value has been executed?

How do I override the variable that was given a value to in another class which has been inherited

How do i change the content of pointer array?

how do i pass an array with a pointer into a function?

How do I find the length of an array?

How do i make an array which is a class object and has a compile time size?

How do I deserialize JSON where there is an array of ane/value properties along with a Location which has three values?

How do I initialise a std::array field inside a struct with the contents of a variable length sequence container?

How do I appropriately set the pointer to a new structure?