3

see i have seen some code like

int (*b)[10];

which means

declare b as pointer to array 10 of int

so i want to ask you what is the purpose of doing this ?

why don't we write instead of that just

int array[10];

and use as passing address of array.

both are same or any difference ?

Edit : Dont be confuse with

int *b[10];  // this one declare b as array 10 of pointer to int

and

int (*b)[10];  // this one declare b as pointer to array 10 of int

one more Edit

see int (*b)[10];

1>will allocate memory for 10 elements of int and the address of that memory will be assigned to b ?

or

2>there will be no memory allocated for array. here it says b is capable of holding address of and int array of 10 element ?

which option is right ? if any one is right then why one should use this complex syntax rather then using different method?

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • possible duplicate of [C pointer to array/array of pointers disambiguation](http://stackoverflow.com/questions/859634/c-pointer-to-array-array-of-pointers-disambiguation) – trojanfoe Oct 31 '11 at 09:24
  • @trojanfoe hey this is not duplicate i know the deference among all i want to know what is purpose of using 1st one over 2nd one method. – Jeegar Patel Oct 31 '11 at 09:27
  • Arrays and pointers are not the same thing. Read section 6 of the [comp.lang.c faq](http://c-faq.com/). – pmg Oct 31 '11 at 09:29
  • @pmg ya i know both are different things – Jeegar Patel Oct 31 '11 at 09:38

3 Answers3

1

There's a difference actually. In int b[10], b is a pointer constant (which means it can be used to modify its underlying data but it can't be changed to point to something else). The pointer in int (*b)[10] on the other hand can be changed to point to something else (as well as being able to change its underlying data of course). So the difference is that by saying that b is int (*b)[10]; you're hereby warning the next developer who sees your code that this b can point to something else somewhere in the code. That's why - obviously - don't do that except if you really intend to change what b is pointing to (otherwise you're just confusing who'll come after you).

Regarding your question, I checked using sizeof(b).

int (*b)[10] ==> sizeof(b) is 4 (which means no memory allocated - Option 2).

int b[10] ==> sizeof(b) is 40 (which means memory is allocated as expected - Option 1).

Look at this:

int (*b)[10];

int x[10];
b = &x;

This compiled. Change the size of the array to anything else. It'll not compile! This means that you're extremely correct :-D. Option 2 is written perfectly: This pointer can only point to an array of size 10 and nothing else.

[Added as per request of question owner]

What's the advantage of such a syntax? It's just a feature that's already there, and maybe it's useful for somebody. This is how I understand it (and please correct me if wrong): For example, you can say: string (*names_of_players_in_soccer_team)[11]; and the advantage is - obviously - restricting the array to be exactly 11 names to handle the application logic - the team must actually have exactly 11 names. This gives more readability for those who'll read your code, and emphasizes its correctness...

OmarOthman
  • 1,718
  • 2
  • 19
  • 36
  • okey got it man...now why someone write int (*b)[10] instead of just using any generic pointer to any array? whats advantage/meaning of saying "pointer to array 10 of int"? – Jeegar Patel Oct 31 '11 at 09:46
  • 1
    By the way, +1 for the great question: It's my first time to see this syntax... thank you very much - I learned something new. – OmarOthman Oct 31 '11 at 09:58
  • 2
    i think 1st time in stackoverflow any answerer says thanks to questioners...he he hehe – Jeegar Patel Oct 31 '11 at 09:59
  • -1 Arrays are not constant pointers. Arrays are arrays, and they can decay into pointers. – fredoverflow Nov 05 '11 at 07:55
0

int (*b)[10] is a pointer to an array of int, int b[10] is an array of int. (Next time maybe choose different names?) Perhaps you want to malloc the array storage, b = malloc(sizeof(*b)). For this you would need a pointer to array type.

rlibby
  • 5,931
  • 20
  • 25
0

C allows the use of an array's name without a subscript as an rvalue meaning the address of the base of the array. It also allows a pointer to be implicitly dereferenced with an offset as an array is typically accessed. The declarations are therefore logically and syntactically equivalent if and only if b never needs to point to a different array or different element of the same array. Technically, they are dissimilar in that int b[10] requires sizeof(int)*10 bytes to hold the array, and int (*b)[10] requires an additional sizeof(int*) bytes to hold a pointer to said array. If the array is declared outside of a function, accessing elements of int b[10] may use a smaller opcode and one fewer registers.

I recommend declaring it as you plan to use it. If you don't know why it might be a pointer, declare it as an array. If it needs to be dynamically allocated on the heap, reseated, or is declared as a function parameter, it should be a pointer.

sqykly
  • 1,586
  • 10
  • 16