473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Pointer-to-array - screwed up my mind ...

Hey, y'all.

While doing some pointer experiments I encountered
a problem. I know that I know the answer already,
but trying to remember I just screwed up my mind.
I wonder if someone would be so kind to enlighten
me, please.

Consider the code below; my specific questions
are embedded in the comment lines:

/* sample code for pointer-to-array problem */
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
int i;

int arr[5][10];
/*
** Declare 'arr' as 'array of 5 arrays of 10 ints',
** right?
*/

int (*ap)[10];
/*
** Declare 'ap' as 'pointer to array of 10 ints',
** right?
*/

ap = (int (*)[10])arr[0]; /* this works; accepted. */
ap = &arr[0]; /* this works, too; why? */

/*
** But if I try this:
** ap = arr[0];
** my compiler produces a diagnostic:
** "Warning: assignment from incompatible pointer type"
**
** arr[0] should already serve as a
** pointer to array of 10 ints, shouldn't it ???
*/

for ( i = 0; i < 5; i++ )
printf( "ap+%d %s &arr[%d]\n",
i,
(( ap+i == &arr[i] ) ? "==" : "!=" ) ,
/* ^^^^^^^ WHY NOT: arr[i] ??? */
i );

return EXIT_SUCCESS;
}
/* end sample code */

If this is a faq, I apologize for not being able to
find it in the faq list ... :)

Thanks,

Irrwahn

--
If you don't care where you are, then you ain't lost.
Nov 13 '05 #1
6 26905
"Irrwahn Grausewitz" <ir*****@freenet.de> wrote:
/* sample code for pointer-to-array problem */
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
int i;

int arr[5][10];
/*
** Declare 'arr' as 'array of 5 arrays of 10 ints',
** right?
*/
Yes.
int (*ap)[10];
/*
** Declare 'ap' as 'pointer to array of 10 ints',
** right?
*/
Yes.
ap = (int (*)[10])arr[0]; /* this works; accepted. */
If you need a cast, you're probably doing something unsafe.
In this case, you're converting from arr[0] which has type
'array of 10 int' or as a value 'pointer to int', to type
'pointer to array 10 of int'. While these two pointers do
point to the same location in memory, they are have quite
different semantics. Note how
arr[0] + 1 points to arr[0][1]
whereas
ap + 1 points to arr[1]
ap = &arr[0]; /* this works, too; why? */
This is the correct way, as both sides of the assignment have
the same type. You can also do:
ap = arr;
Because there is is an implicit conversion from any array type
into a pointer to its first element. So, although arr has type
'array 5 of array 10 of int' it is automatically converted to
'pointer to array 10 of int'.
/*
** But if I try this:
** ap = arr[0];
** my compiler produces a diagnostic:
** "Warning: assignment from incompatible pointer type"
Exactly. As explained above, arr[0] is an array of 10 int. When
used in a value context, it is converted into a pointer to its
first element, so becomes a pointer to arr[0][0], a pointer to
int. This is the wrong type.
** arr[0] should already serve as a
** pointer to array of 10 ints, shouldn't it ???


No. If you want a pointer to array of 10 ints, simply write arr
by itself, or to be explicit, write &arr[0].

arr is an array of 5 arrays, each of 10 ints. It is not a pointer.
When used in an expression, other than as the argument of a sizeof
or address-of (&) operator, it is converted into a pointer to its
first element. Not into a pointer to itself!

arr[0] is the first array of 10 ints. It is not a pointer. When
used in an expression, other than as the argument of a sizeof or
address-of (&) operator, it is converted into a pointer to its
first element. Not into a pointer to itself!

--
Simon.
Nov 13 '05 #2
"Simon Biber" <sb****@optushome.com.au> wrote in
<3f**********************@news.optusnet.com.au>:

<SNIP>

Thanks to you and Joona for your help.
Sometimes I seem to forget about the basics...

Irrwahn

--
Learn how to splel, dmanit!
Nov 13 '05 #3
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in
<bi**********@oravannahka.helsinki.fi>:
ap = arr; /* would also work. */

This is because of Chris Torek's THE RULE: When used in a value
context (rather than in an assignment or initialisation context), an
array decays into a pointer to its first element.
Ah, yes, that was the missing link I couldn't remember!!!
(BTW: I wish there was a collection of CT's posts available :))))
** But if I try this:
** ap = arr[0];
** my compiler produces a diagnostic:
** "Warning: assignment from incompatible pointer type"


This is because ap is a pointer to an array of 10 ints, but arr[0]
is an array of 10 ints. It would be the same as trying to assign an
int to a pointer to an int.

Yes, of course, now I notice; sigh ...
** arr[0] should already serve as a
** pointer to array of 10 ints, shouldn't it ???


No. In a value context, it would "serve as" a pointer to _an int_,
not to _an array of 10 ints_.

.... dito ...
(( ap+i == &arr[i] ) ? "==" : "!=" ) ,
/* ^^^^^^^ WHY NOT: arr[i] ??? */


Because they differ in indirection levels. ap+i == arr+i would also
work.

.... dito ...

I hope I'll never forget about these issues again!

Thanks Joona!

Irrwahn

--
My opinions are not those of my ex-employer.
Nov 13 '05 #4
Irrwahn Grausewitz wrote:
.... snip ...
Ah, yes, that was the missing link I couldn't remember!!!
(BTW: I wish there was a collection of CT's posts available :))))


There is. Search google groups for articles on c.l.c with author
Chris Torek.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation
Nov 13 '05 #5
LibraryUser <de**********@made.invalid> wrote in
<3F***************@made.invalid>:

Ah, yes, that was the missing link I couldn't remember!!!
(BTW: I wish there was a collection of CT's posts available :))))


There is. Search google groups for articles on c.l.c with author
Chris Torek.


Yes, of course. I tried to make a running gag last longer ...
not a good joke, possibly. :-|

--
My opinions are not those of my ex-employer.
Nov 13 '05 #6
The declaration
int A[5][10] ;
means to compiler is an array of 10 int
placed sequentially in 5 rows.
So A is actually a collection 5 single dimensional array of 10 ints.
And as the language says the the array name is a pointer to its first
element
the the type of the A is really int (*)[10] which is pointer to sigle
dimensional array of 10 ints.
The implication of the above statement is if u do:
int (*p)[10];
p = A;

Then the p+1 will point to the next row.
This is what the pointer arithmatic.
The compiler interanally adjust the the offset from base address as
follow knowing that the type of A is pointer to array 10 of ints.
offset= 10*sizeof(int)
p+1= base address(A) + offset

I hope it answers to your query.

Irrwahn Grausewitz <ir*****@freenet.de> wrote in message news:<va********************************@4ax.com>. ..
"Simon Biber" <sb****@optushome.com.au> wrote in
<3f**********************@news.optusnet.com.au>:

<SNIP>

Thanks to you and Joona for your help.
Sometimes I seem to forget about the basics...

Irrwahn

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rasti | last post by:
Hello, I am trying to translate a C++ script to Java. Are there any differences between the C++ this-pointer and the Java this-pointer? Thank you
5
by: | last post by:
What is the difference between passing argument by reference or by pointer? Is there any difference in terms of PERFORMANCE ? When and why one of these method (by reference or by pointer) should be...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
20
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
20
by: Bill Potter | last post by:
I am a learning programmer in C and i want to know why some one would use pointers instead of going direct!
4
by: code break | last post by:
Hi all, What is the difference between stack pointer and frame pointer ? Any suggestions are welcome ,,,
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
34
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.