what is the difference between these 2declarations

Hi,

I am allocating memory as follows in C

what are the differences between these 2 declaration

int a[]=malloc(100*sizeof(int));
int *a=malloc(100*sizeof(int));

Please help me on this.
Nothing, the [] operator is simply an overload for the pointer operator. Here is a quick example to show what I mean:
1
2
3
4
5
6
7
8
int *a = malloc(100 * sizeof(int));
for (int i = 0; i < 100; ++i)
    a[i] = i;
printf("%d\n", a[5]);
// This is the same thing, using pointer arithmetic:
printf("%d\n", *(a + 5));

// Therefore, the "x[]" operator is simply "*(x+0)" = "*x" 
Last edited on
@Ajaycpp
True, they can often be used interchangeably, but not all the time.

(Actually I'm not sure your first line up there is legal, but let's stick with pointers versus arrays)

A pointer generally holds the address of one object in memory, while an array is an aggregate of the same type of objects. The address of the first object in the array also serves as the address of the entire array (just like the address of the first byte in a 4-byte word would be the address of the whole word), this lets us use the name of the array as a synonym for a pointer to its first object. The "arrayness" of the array introduces some differences however, for example a common paradigm for finding the
size of an array goes like so:
1
2
int arr[] = {1,2,3,4,5,6};
int arrSize = sizeof(arr)/sizeof(arr[0]);

If you were treating arr as a pointer, then you'd get different values, for example
sizeof(arr) is not the same as sizeof(&arr[0])
sizeof will return different values for both examples below:
1
2
int a[]= {1,2,3,4,5,6,7,8,9,10}; // sizeof(a) = 8 * sizeof(int)
int *b=(int *)malloc(10*sizeof(int)); // sizeof(b) = sizeof (int *) 

Also, a pointer is a variable but an array name is not. So using the example above, you could do: b = a; or b = &a[0]; but not a = b; - This is also why your first line up there won't work.

There most certainly is a difference.

But... don't your professors want you guys to try to figure this stuff out?
http://www.cplusplus.com/forum/beginner/120056/

If you want to succeed in programming, you need to learn to search for answers yourself.
https://www.google.com/search?q=static+vs+dynamic+array

I'm not trying to pick on you, specifically... only I'm grouchy right now and threads about stuff people can learn by just reading their textbooks are getting kind of old...
Hi,

Thanks to all for giving valuable info.

one more thing how the malloc() will assign the memory?
Is it contiguous memory or any other?

is it assign total amount of memory at a time or dynamically increased?

Please help me on this.
Last edited on
Yes, malloc() returns a single, contiguous chunk of memory. It is possible that the memory chunk you get is actually larger than you asked for, but you cannot know that. To resize/ask for larger (or smaller) memory, use realloc(). You must do this explicitly, just as all heap management, like malloc() and free().

[edit] fixed grammar typo
Last edited on
Hi,

If malloc() will assign contiguous memory then what is the difference between array (p[]) declaration and pointer (*p) ?
Why not read the links I've already posted for you? Hmm?
Thanks...Duoas....
Topic archived. No new replies allowed.