B
bintom
What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
int *ptr[10];
and
int (*ptr)[10];
What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
bintom said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
bintom said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
The former is an array of 10 elements, each element being an int*.
The latter is a pointer to a two-dimensional array (iow. a single
array which is indexed using two index values), the second dimension
of said array being 10.
在 2012å¹´2月16日星期四UTC+8下åˆ11æ—¶15分14秒,Juha Nieminen写é“:bintom said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
The former is an array of 10 elements, each element being an int*.
The place where the * should be appeared is not only
the style problem.
The latter is a pointer to a two-dimensional array (iow. a single
array which is indexed using two index values), the second dimension
of said array being 10.
int* ptr[10];
int (*ptr2)[10];
//I prefer this style.
Victor Bazarov said:在 2012å¹´2月16日星期四UTC+8下åˆ11æ—¶15分14秒,Juha Nieminen写é“:
What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
The former is an array of 10 elements, each element being an int*.
The place where the * should be appeared is not only
the style problem.
The latter is a pointer to a two-dimensional array (iow. a single
array which is indexed using two index values), the second dimension
of said array being 10.
int* ptr[10];
int (*ptr2)[10];
//I prefer this style.
You do, do you? In this declaration:
int* a[10], b[20];
what's the type of 'b[2]'?
int of course. Yet another reason to place declarations individually.
int *a[10];
int *b[10];
Easier to pick out the identifier visually this way.
...
Besides that, why waste a line of code in the following idiom:
sometype obj, *ptr = &obj;
?
bintom said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
LR said:bintom said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
You may want to read about the Right Left Rule.
Juha Nieminen said:bintom said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
The former is an array of 10 elements, each element being an int*.
The latter is a pointer to a two-dimensional array (iow. a single
array which is indexed using two index values), the second dimension
of said array being 10.
Juha said:LR said:bintom said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
You may want to read about the Right Left Rule.
Unfortunately that rule doesn't help here because the "(*)[]"
syntax is special and has a particular meaning that isn't obvious.
One just has to know what it means.
Jens Thoms Toerring said:Juha Nieminen said:bintom said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];The former is an array of 10 elements, each element being an int*.The latter is a pointer to a two-dimensional array (iow. a single
array which is indexed using two index values), the second dimension
of said array being 10.
Sorry for chiming in, but the second is just a pointer to
an array of 10 ints, nothing two-dimensional involved at
this stage
LR said:Juha said:LR said:bintom wrote:
What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
You may want to read about the Right Left Rule.
Unfortunately that rule doesn't help here because the "(*)[]"
syntax is special and has a particular meaning that isn't obvious.
One just has to know what it means.
I'm sorry, but I don't understand why the Right Left Rule does not apply
to the second example above.
On 2/16/2012 12:18 PM, Scott Lurndal wrote:
[...] Yet another reason to place declarations individually.int *a[10];
int *b[10];Easier to pick out the identifier visually this way.
Identifiers should have a meaningful number of characters for easy
picking out, IMNSHO. 'a' or 'b' aren't very good no matter whether you
declare them each on a separate line, or together.
Juha said:LR said:Juha said:bintom wrote:
What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
You may want to read about the Right Left Rule.
Unfortunately that rule doesn't help here because the "(*)[]"
syntax is special and has a particular meaning that isn't obvious.
One just has to know what it means.
I'm sorry, but I don't understand why the Right Left Rule does not apply
to the second example above.
I didn't say it doesn't apply. I said it doesn't help understanding
what the syntax means.
LR said:However, the OP asked about the
difference between the two, and I think the RLR is very useful for that
purpose.
Juha Nieminen said:Jens Thoms Toerring said:Juha Nieminen said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];The former is an array of 10 elements, each element being an int*.The latter is a pointer to a two-dimensional array (iow. a single
array which is indexed using two index values), the second dimension
of said array being 10.
Sorry for chiming in, but the second is just a pointer to
an array of 10 ints, nothing two-dimensional involved at
this stage
If you want to be really pedantic, that might be so. It's the same as
with an "int*" which doubles as a pointer to a single int and a pointer
to an array of ints.
In practice "int*" is basically always a pointer to an array of ints
because it has intrinsic support for it being an array (ie. you can
index the potential array behind it). If it's pointing to a single int
you can think of it as pointing to an array of size 1.
Likewise "int (*ptr)[10]" is always a pointer to a two-dimensional array
Ok, let me really pedantic;-) An "int*" doesnt double as a pointer
to an array (of ints), it just can point to an element of an array
(just as it can point to a "lonely" int).
Juha Nieminen said:If you want to be really pedantic, that might be so. It's the same asJens Thoms Toerring said:What is the difference between the following declarations?
int *ptr[10];
and
int (*ptr)[10];
The former is an array of 10 elements, each element being an int*.
The latter is a pointer to a two-dimensional array (iow. a single
array which is indexed using two index values), the second dimension
of said array being 10.
Sorry for chiming in, but the second is just a pointer to
an array of 10 ints, nothing two-dimensional involved at
this stage
with an "int*" which doubles as a pointer to a single int and a pointer
to an array of ints.
Ok, let me really pedantic;-) An "int*" doesnt double as a pointer
to an array (of ints), it just can point to an element of an array
(just as it can point to a "lonely" int).
In practice "int*" is basically always a pointer to an array of ints
There are a lot of cases where there are pointers to single ints,
e.g. when passing them to a function (admitedly, more often in C
were ypu don't have references and pointers are the only way to
pass something to a function that can be modified from within
the function).
I understand what you mean, but this is saddling the horse from
the back. An "int*" never points to more than a single int.
This is not correct see above.Due
to the shortcoming of C (and thus C++) that arrays aren't "first
class" objects (i.e. they can't be passed to functions by value
since an array isn't a "value", contrary to e.g. structures) a
special case was introduced: when an array appears in a place
were a value is required then the array gets converted to a
pointer to the first element of that array. And this has
resulted in many people (even experienced C and C++ program-
mers) getting confused about the distinction between arrays
and pointers. But that doesn't make a pointer anything "array-
like" - it's just that in certain situations when an array is
found it is converted silently to a pointer to that array's
first element. That's all there's about "intrinsic support".
Likewise "int (*ptr)[10]" is always a pointer to a two-dimensional array
Sorry, but no. Like an "int*" only points to a single int,
The pointer type int(*)a "int
(*ptr)[10]" points to just a single array of 10 ints. But because,
when incremented, it points to the next following array of 10
ints it can be used to iterate over the sub-arrays of a two-
dimensional array when that is an array of arrays of 10 ints.
Am 18.02.2012 23:39, schrieb Jens Thoms Toerring:
I think I have a deja vu
Peter
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.