Wednesday, 15 April 2015

pointers - How does dynamically allocating a struct in C work? -


Recently there was an assignment where I had to allocate memory to a structure for dynamic. I used this method:

  myStruct * struct1 = malloc (sizeof * struct1);   

It just worked fine. However, I do not understand how. I think that the struct1 indicator has not been started at that point and therefore should not have anything to do with why malloc (sizeof * struct1) how to assign Does memory of a valid quantity return?

sizeof operator does not evaluate operands in C. It looks just like the type, for example:

  #include & lt; Stdio.h & gt; Int main (zero) {int i = 0; Printf ("% zu \ n", Size I ++); Printf ("% d \ n", i); Return 0; }   

If you run the above program, you will see that i is still 0.

Then, in your example, * structure1 is not evaluated, it is used only for type information.

No comments:

Post a Comment