pointer and array

L

Lawrence Kirby

On Wed, 22 Dec 2004 07:43:21 +0100, Emmanuel Delahaye wrote:

....
No. It's the address of the array (same value), *but the type is
different*. Actually it has the 'int (*)[10]' type, and can be assigned
to a pointer of the same type:

int arr[10];

int (*p)[10] = &arr;

Is it useful or not is another question.

It is useful for arrays of arrays e.g.

int arr2[3][10];
int (*p)[10] = &arr2[0];

arr2[0] is an array (with type array of 10 ints) and &arr2[0] is a pointer
to that array (with type pointer to an array of 10 ints, the same as p).
Because of p's type expressions like p[x][y] make sense and with p set up
like this will access the same element as arr2[x][y].

Yes, I could have written just

int (*p)[10] = arr2;

in this case but you can naturally express things like things like

p = &arr2[1];

Lawrence
 
L

Lawrence Kirby

Leon Brodskiy wrote on 19/12/04 :
I'm asking because I have found in a program the following call:

void f1(char inp[10])
{...}

void main()

You can never say too much that main returns int. :)
{...
char str[10];
...
f1(&str);
...
}

I tought this is a bug and this is not supposed to work but it does work.

It's a bug because the types are not the same. (A decent and well
configured compiler should yell on it). It 'works' because the value is
the same, but the '&' should be removed.

str and &str evaluate to pointers to different objects (an int vs. an
array of 10 ints) so in a very real sense their values are different.
Those two objects do happen to share the same starting byte in memory, so
it is common for implementations to use the same representation for
those two pointer values. But that isn't guaranteed.
..
Type is important when considering whether two values are the same. Are 1
and 1L the same value, do they have the same representation in an object?
What about 1 and 1.0? Then -1 and ~0U (think of likely representations on
a 2's complement system). Now consider different pointer types that you
can't even compare directly.
Function receives pointer to char. I send to the function a pointer to a
pointer to char<...>

Wrong. You have sent the address of a pointer to an array of 10 char.
Its type is 'char(*)[10]'.

Your main point is of course correct. Passing a pointer to an array to a
function that requires a pointer to char with a prototype in scope is a
constraint violation. a conforming C compiler must produce a diagnostic.

Lawrence
 
E

Emmanuel Delahaye

Lawrence Kirby wrote on 22/12/04 :

Your main point is of course correct. Passing a pointer to an array to a
function that requires a pointer to char with a prototype in scope is a
constraint violation. a conforming C compiler must produce a diagnostic.

Tanks for these details.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top