I’m trying to declare an array of pointers of a struct some_struct in C
Can I do:
some_struct* arr[10];
instead of:
some_struct** arr=(some_struct**)malloc(10*sizeof(some_struct*));
And what’s the difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the first case, the lifetime of the array is only the scope at which it is defined in.
When it falls out of scope, it will be freed automatically so you don’t have to do any clean up.
In the second case, the array lives beyond the scope where the pointer is declared. So you will need to manually
free()it later to avoid a memory leak.