allocate memory

How do I initialise p in the declaration statement :
in my code I have p= malloc(n*4);/*allocate memory*/
It is not running properly.
also:
hold = p[j];
mlmcguireAsked:
Who is Participating?
 
chensuCommented:
So it is a compiling error. You need to cast it. For example,

int *p = (int *)malloc(n * sizeof(int));
0
 
chensuCommented:
What do you mean by "not running properly"?
0
 
mlmcguireAuthor Commented:
its telling me that it cannot convert int to something. I was working on it 2days ago.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> (n * sizeof(int))

mlmcguire, not that this is a better way of doing it than your original ( n * 4 ) -> because the size of the primitive data types in C will depend upon the platform on which your program is running.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> not that

note* that....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.