Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer-to-array issues in C vs C++

Reply
Thread Tools

Pointer-to-array issues in C vs C++

 
 
Robbie Hatley
Guest
Posts: n/a
 
      06-05-2004
About 20 days ago, "John Harrison" <>
replied to a message which I had written:

> > > Q1 . What is the use of pointer to an array?

> >
> > It points to it. (The name of an array is also a pointer to it.)

>
> No, the name of an array decays to a pointer to the first element of the
> array. A pointer to an array and a pointer to the first element of an array
> are not the same thing.
>
> int a[10];
> cout << &a + 1 << endl;
> cout << a + 1 << endl;
>
> The two statements print different pointer values, proving that a pointer to
> an array (first case) and a pointer to the first element of an array (second
> case) are not the same.


I was dubious, so I wrote this program:

#include <iostream>
using std::cout;
using std::endl;
int main(void)
{
int a[10];
cout << "int a[10];" << endl;
cout << " &a : " << (&a) << endl;
cout << " a : " << ( a) << endl;
cout << "&a + 1 : " << (&a + 1) << endl;
cout << " a + 1 : " << ( a + 1) << endl;
return 0;
}

and I got these results:

wd=C:\C\test
%array-pointer-test
int a[10];
&a : 0x6cffac
a : 0x6cffac
&a + 1 : 0x6cffd4
a + 1 : 0x6cffb0

Interestingly, the value of &a and a are the same. But when incremented,
one increments 4 bytes, the other 40 bytes.

Of course. They're pointers to different types, one of size 4 and the other
of size 40. I should have known that. ::: kicks self :::

In C, however, depending on the context, the compiler is likely to
implicitly cast the pointer from pointer-to-array to pointer-to-int.
For example, consider the following code:

/* char-array-test.c */
#include <stdio.h>
char Func(char* blat)
{
return *(blat+1);
}
int main(void)
{
char a[80];
a[0] = 'n';
a[1] = 't';
printf("Func(&a) = %c\n", Func(&a));
return 88;
}

This prints 't' in C, but it won't even compile in C++.

I think I shouldn't have learned C before C++; I've picked up some bad
habits that way.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant







----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-05-2004
"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote...
> [...]
> I was dubious, so I wrote this program:
>
> #include <iostream>
> using std::cout;
> using std::endl;
> int main(void)
> {
> int a[10];
> cout << "int a[10];" << endl;
> cout << " &a : " << (&a) << endl;
> cout << " a : " << ( a) << endl;
> cout << "&a + 1 : " << (&a + 1) << endl;
> cout << " a + 1 : " << ( a + 1) << endl;
> return 0;
> }
>
> and I got these results:
>
> wd=C:\C\test
> %array-pointer-test
> int a[10];
> &a : 0x6cffac
> a : 0x6cffac
> &a + 1 : 0x6cffd4
> a + 1 : 0x6cffb0
>
> Interestingly, the value of &a and a are the same. But when incremented,
> one increments 4 bytes, the other 40 bytes.
>
> Of course. They're pointers to different types, one of size 4 and the

other
> of size 40. I should have known that. ::: kicks self :::
>
> In C, however, depending on the context, the compiler is likely to
> implicitly cast the pointer from pointer-to-array to pointer-to-int.


I just wonder how you come to this conclusion.

> For example, consider the following code:
>
> /* char-array-test.c */
> #include <stdio.h>
> char Func(char* blat)
> {
> return *(blat+1);
> }
> int main(void)
> {
> char a[80];
> a[0] = 'n';
> a[1] = 't';
> printf("Func(&a) = %c\n", Func(&a));


So, inside that function the address that you obtain by &a, is still
interpreted as char*, as if you passed 'a' (instead of &a). What is
surprising about that?

In C, anything used to be convertible to char* (and back). Similar to
void* in C++ (and now in C99, IIRC). The difference is that you can't
really do anything with void*, whereas char* can be interpreted as
a string (or array of characters). IMO, C++ fixed what was kind of
broken in C. Or at least tried to fix it. C gived char* dual meaning
-- a generic address holder and a pointer to [an array of] character[s].

> return 88;
> }
>
> This prints 't' in C, but it won't even compile in C++.


Of course it won't compile in C++. VC++ 7.1 (when compiling C) does
actually warn about the difference in "levels of indirection". It
lets the difference slide simply because C was built to serve that.

> I think I shouldn't have learned C before C++; I've picked up some bad
> habits that way.


Learing C _correctly_ before C++ can't hurt, at least not too much.
Bad habits can be picked in either language and without them. Try
not to blame languages for the way they are [mis]used.

Also, knowing the differnces between the languages and how they do
things they do and what is allowed and not allowed there always
helps.

Don't beat yourself too much...

Victor


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      06-05-2004
On Fri, 4 Jun 2004 20:04:53 -0700, "Robbie Hatley" <lonewolfintj at
pacbell dot net> wrote in comp.lang.c++:

> About 20 days ago, "John Harrison" <>
> replied to a message which I had written:
>
> > > > Q1 . What is the use of pointer to an array?
> > >
> > > It points to it. (The name of an array is also a pointer to it.)

> >
> > No, the name of an array decays to a pointer to the first element of the
> > array. A pointer to an array and a pointer to the first element of an array
> > are not the same thing.
> >
> > int a[10];
> > cout << &a + 1 << endl;
> > cout << a + 1 << endl;
> >
> > The two statements print different pointer values, proving that a pointer to
> > an array (first case) and a pointer to the first element of an array (second
> > case) are not the same.

>
> I was dubious, so I wrote this program:
>
> #include <iostream>
> using std::cout;
> using std::endl;
> int main(void)
> {
> int a[10];
> cout << "int a[10];" << endl;
> cout << " &a : " << (&a) << endl;
> cout << " a : " << ( a) << endl;
> cout << "&a + 1 : " << (&a + 1) << endl;
> cout << " a + 1 : " << ( a + 1) << endl;
> return 0;
> }
>
> and I got these results:
>
> wd=C:\C\test
> %array-pointer-test
> int a[10];
> &a : 0x6cffac
> a : 0x6cffac
> &a + 1 : 0x6cffd4
> a + 1 : 0x6cffb0
>
> Interestingly, the value of &a and a are the same. But when incremented,
> one increments 4 bytes, the other 40 bytes.
>
> Of course. They're pointers to different types, one of size 4 and the other
> of size 40. I should have known that. ::: kicks self :::
>
> In C, however, depending on the context, the compiler is likely to
> implicitly cast the pointer from pointer-to-array to pointer-to-int.
> For example, consider the following code:


Actually, depending on the compiler ***and the way you invoke it***.

If you invoke your compiler as a conforming C compiler (which none I
know of are by default), it must issue a diagnostic when you pass a
pointer to an array to a function prototyped as accepting a pointer to
char.

Asking the compiler to perform that conversion explicitly is a
constraint violation.

Check the compiler's documentation and invoke it in standard C
conforming mode, and it will no more accept the program without
complaint than will a C++ compiler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows XP Pro clean install issues, SP2 issues too... Howie Computer Support 9 07-12-2005 04:47 PM
Windows XP Pro clean install issues, SP2 issues too... Howie Computer Support 0 07-06-2005 07:12 PM
Re: Windows XP Pro clean install issues, SP2 issues too... pcbutts1 Computer Support 0 07-06-2005 04:58 PM
Re: Windows XP Pro clean install issues, SP2 issues too... pcbutts1 Computer Support 0 07-06-2005 04:52 PM
SNMP Issues in Cisco Routers; Vulnerability Issues in TCP =?iso-8859-1?Q?Frisbee=AE?= MCSE 0 04-21-2004 03:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57