Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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];
0
mlmcguire
Asked:
mlmcguire
  • 2
  • 2
1 Solution
 
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
 
chensuCommented:
So it is a compiling error. You need to cast it. For example,

int *p = (int *)malloc(n * sizeof(int));
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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now