0

i'm learning about structures in c and there are few tips confusing me so if we have a struct like this

struct Person{
char* name;
int age;
int weight;
int height;
};

now it is a compound datatype that i can deal with like other data types.. when i make pointer to this structure

struct Person pointer=malloc(sizeof(Person)); 

now c point to an address in the memory with the size if the struct my question is

A)is really c know that Person consists of 4 variable with names name,age,weight,height and make copy to this entity to new place which pointer points to ?

B)what is the difference in this two syntax ?

struct Person *pointer=(Person*)malloc(sizeof(Person));
struct Person *pointer=malloc(sizeof(Person));

C)i'm not familiar with this function deceleration syntax so can any one clear up it to me (name of function is an pointer) ?

struct Person *Person_create(char* name, int age, int height, int   weight){...}

2 Answers2

0

Question A:

is really c know that Person consists of 4 variable with names name,age,weight,height and make copy to this entity to new place which pointer points to ?

Answer:

The answer to the first part of the question is 'yes' except I will rephrase it as A C compiler knows...

The second part of the question is not clear to me. I am not sure what you are asking.

Question B:

what is the difference in this two syntax ?

struct Person *pointer=(Person*)malloc(sizeof(Person));
struct Person *pointer=malloc(sizeof(Person));

Answer:

If you use

 #include <stdlib.h>

at the top of your .c file, both are the same. If you don't that line, then

 struct Person *pointer=(Person*)malloc(sizeof(Person));

has the potential to hid a bug. See Do I cast the result of malloc? for further details.

Question C:

i'm not familiar with this function deceleration syntax so can any one clear up it to me (name of function is an pointer) ?

struct Person *Person_create(char* name, int age, int height, int   weight){...}

Answer:

The above syntax is correct if you want to allocate an object of type struct Person from heap using malloc family of functions and return a pointer to the allocated object.

You also have the option of return an object defined using stack memory.

 struct Person Person_create(char* name, int age, int height, int   weight){...}

Which one you choose depends entirely on you. You have to make sure that when you use heap memory for the object, you must remember to call free() on the pointer. Otherwise, your program will leak memory.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • my second part of question A is pointer->age=30; after allocating process by malloc to the required memory how is c compiler act in the new allocated space to store variable called age with value 20 although age is an variable of struct person ? – Omar Khaled Feb 09 '15 at 04:07
  • `malloc` only returns you a set of bytes. It does not set any values on those bytes. You have to fill it up with the necessary data. `calloc`, on the other hand, returns you a set of bytes initialized to `0`. – R Sahu Feb 09 '15 at 04:12
0

A) No, malloc() returns an array of bytes which know nothing about what they are supposed to contain. In particular, pointer points to an uninitialized Person which could contain anything until you initialize the fields (or memset(pointer, 0, sizeof(Person))).

B) The difference is that the one with the cast ((Person*)) is considered bad practice in C. Use the shorter one, because it is shorter and doesn't hide potential errors.

C) struct Person *Person_create is not "the name of the function is a pointer," rather it is "a function which returns a pointer." Some people would write it as struct Person* Person_create instead.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • i got ur answer of my B question well but for A and C A) now malloc just returns array of bytes notintialised ..ok when i make pointer->age=20; how c act in this case although in new allocated block there is no blocks called age and for C question we use this pointer to use the function so where is the function name and coule you provide me more about this syntax ? – Omar Khaled Feb 09 '15 at 03:36
  • Most things in C are just blocks of bytes, at runtime no type information is needed. So using a block of bytes as an integer is normal in C, you don't need to "convert" the block to an integer before storing a number in it. – John Zwinck Feb 09 '15 at 04:21