C
code break
what is the difference in this pointers decalarition ?
int *ptr[10];
and
int (*ptr)[10];
int *ptr[10];
and
int (*ptr)[10];
relient said:One is an array of pointers while the other is a pointer to an array.
Example:
int *ptr[10];
int var1, var2;
ptr[0] = &var1;
ptr[1] = &var2;
.....
int (*ptr)[10];
int multiDimensionalArray[5][10];
ptr = multiDimensionalArray;
relient said:The same way you would with the multiDimensionalArray variable.
ptr[0][1] = 5;
Vladimir said:relient said:The same way you would with the multiDimensionalArray variable.
ptr[0][1] = 5;
*Please* quote what (and /who/) you're replying to. Many people can't
see previous posts, for various reasons. It's also possible to do it
via Google, if you click Show Options, and then Reply below the message
header. It's only one click more than Reply at the bottom of the
message, and will do everything for you.
--
BR, Vladimir
Some people live life in the fast lane.
You're in oncoming traffic.
Thank you for what? Please follow Vladimir's advice.code said:Thank you very much.
relient
int (*ptr)[10]; ---> is a pointer to arrays ?.
code said:int (*ptr)[10]; ---> is a pointer to arrays ?.
Andrey Tarasevich said:code said:int (*ptr)[10]; ---> is a pointer to arrays ?.
It is a pointer to an array if 10 ints. As with any pointer in C, the
object pointed to by this pointer (i.e. the 'int[10]' array) might be a
standalone object, or it might be an element of larger array:
Keith said:Andrey Tarasevich said:code said:int (*ptr)[10]; ---> is a pointer to arrays ?.
It is a pointer to an array if 10 ints. As with any pointer in C, the
object pointed to by this pointer (i.e. the 'int[10]' array) might be a
standalone object, or it might be an element of larger array:
Note that pointers to arrays are rarely useful. Usually the easiest
way to access an array is via a pointer to its first element; this has
the advantage of being able to deal with arrays of varying sizes.
Andrey Tarasevich said:Keith said:Andrey Tarasevich said:code break wrote:
int (*ptr)[10]; ---> is a pointer to arrays ?.
It is a pointer to an array if 10 ints. As with any pointer in C, the
object pointed to by this pointer (i.e. the 'int[10]' array) might be a
standalone object, or it might be an element of larger array:
Note that pointers to arrays are rarely useful. Usually the easiest
way to access an array is via a pointer to its first element; this has
the advantage of being able to deal with arrays of varying sizes.
I wouldn't put an equality sign between "can be avoided" and "rarely
useful". In my opinion, the technique of accessing with array through a
pointer to its first element should be restricted to situations when it
is really necessary to deal with arrays of varying sizes. In cases when
the array size is fixed (i.e. known at compile time) the preference
should be given to the more strongly typed approach: using pointer to
the entire array.
Keith said:I disagree. First, it's very common for array sizes not to be known
at compile time; very often, you don't know how big the array needs to
be until run time.
Even if you have a fixed-size array, you might
need to pass it to a function that can deal with arrays of any size.
Dealing with arrays through element pointers is a common idiom. In
fact, array indexing is defined in terms of pointer arithmetic.
With
pointers to arrays, it's to easy to get the syntax wrong, and to mix
up a pointer-to-array with a pointer-to-array-element.
If I really needed a pointer to a fixed-size array, I might wrap the
array in a structure.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.