C Programming - Memory Allocation - Discussion

Discussion Forum : Memory Allocation - Point Out Errors (Q.No. 1)
1.
Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *a[3];
    a = (int*) malloc(sizeof(int)*3);
    free(a);
    return 0;
}
Error: unable to allocate memory
Error: We cannot store address of allocated memory in a
Error: unable to free memory
No error
Answer: Option
Explanation:
We should store the address in a[i]
Discussion:
17 comments Page 2 of 2.

Anand said:   1 decade ago
I don't think their is any error. And 'a' is equivalent to a[0].

Balaji said:   1 decade ago
No matter whether it is integer pointer or pointer to a pointer or, pointer to pointer to a pointer and so. All the pointers are having same type value(i.e. integer value) right? then why can't we assign the address?

Kmokhtar said:   1 decade ago
a[0] does not mean a but it means *(a+0)?

Ajit said:   1 decade ago
Even though a[0] means a, that means the above program should allocate address to a[0], then where is the error.

Sheetal said:   1 decade ago
Here a gives the address of first address element in array, i.e., a is pointer to a pointer and we are assigning it to a pointer (here int*) , so we get an error. We can assign any element of a which is an int pointer.

RAJU said:   1 decade ago
*a[3] is an array of pointers. An array a contains address of independent variable or another arrays. we cannot store address of allocated memory in a.we have to store address of allocated memory in a[i] i.e subscript we have to mention.

Pavan said:   1 decade ago
Remove the size of 'a' it will work.


Post your comments here:

Your comments will be displayed after verification.