I've always taught that arrays are not pointers. So how come I can do:
#include <iostream>
#include <cstdlib>
using namespace std;
int main( )
{
int myArray[10]={1, 10};
cout << sizeof(myArray) << endl;
cout << sizeof(myArray+1) << endl;
cout << *myArray << endl;
cout << *(myArray+1) << endl;
system("Pause");
return 0;
}
/*results are
40
4
1
10
*/
--
Gary 18 1339
"Gary" <gl*******@comcast.net> wrote in message
news:Z9********************@comcast.com... I've always taught that arrays are not pointers. So how come I can do: #include <iostream> #include <cstdlib>
using namespace std;
int main( ) { int myArray[10]={1, 10}; cout << sizeof(myArray) << endl; cout << sizeof(myArray+1) << endl; cout << *myArray << endl; cout << *(myArray+1) << endl;
system("Pause"); return 0; } /*results are 40 4 1 10 */
Arrays are pointers are not same but inter-related.
arr[i] == *(arr+i)
arr is the address of the starting element of the array.
arr+1 is the address of the 2nd element of the array.
*arr is the value of the 1st element
*(arr+1) is the 2nd element of the array.
Simple :-)
-Sharad
Gary wrote: I've always taught that arrays are not pointers. So how come I can do:
[SNIP]
Because in certain expressions the name of the array _decays_ into a pointer
to the first element of the array.
--
Attila aka WW
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bv**********@newstree.wise.edt.ericsson.se... Gary wrote: I've always taught that arrays are not pointers. So how come I can do: [SNIP]
Because in certain expressions the name of the array _decays_ into a
pointer to the first element of the array.
Yeah, but I was surprised that I could do pointer arithmetic on the beast.
sizeof(array+1) came back okay. I would have thought it would be the address
of the second byte of the entire array. Oh, well.
--
Gary
Gary wrote: "Attila Feher" <at**********@lmf.ericsson.se> wrote in message news:bv**********@newstree.wise.edt.ericsson.se... Gary wrote: > I've always taught that arrays are not pointers. So how come I can > do: [SNIP]
Because in certain expressions the name of the array _decays_ into a pointer to the first element of the array.
Yeah, but I was surprised that I could do pointer arithmetic on the beast. sizeof(array+1) came back okay. I would have thought it would be the address of the second byte of the entire array. Oh, well.
array+1 not the address of the second byte, but rather of the second
element. sizeof(array+1) returns the size of a pointer to int for an
array of int. What would you expect an address to be stored in?
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com... Gary wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message news:bv**********@newstree.wise.edt.ericsson.se... Gary wrote: > I've always taught that arrays are not pointers. So how come I can > do: [SNIP]
Because in certain expressions the name of the array _decays_ into a pointer to the first element of the array.
Yeah, but I was surprised that I could do pointer arithmetic on the beast. sizeof(array+1) came back okay. I would have thought it would be the address of the second byte of the entire array. Oh, well.
array+1 not the address of the second byte, but rather of the second element. sizeof(array+1) returns the size of a pointer to int for an array of int. What would you expect an address to be stored in?
Since you asked: I was just guessing that the array identifier has a size of
the full array (correct guess); that array+1 would be the address of the
second byte of the array (wrong guess); that sizeof array+1 would be
undefined (wrong guess). At least I checked. But I was surprised, that's
all.
--
Gary
Gary wrote: "Rolf Magnus" <ra******@t-online.de> wrote in message news:bv*************@news.t-online.com...
Gary wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message news:bv**********@newstree.wise.edt.ericsson.se ...
Gary wrote:
>I've always taught that arrays are not pointers. So how come I can >do:
[SNIP]
Because in certain expressions the name of the array _decays_ into a
pointer
to the first element of the array.
Yeah, but I was surprised that I could do pointer arithmetic on the beast. sizeof(array+1) came back okay. I would have thought it would be the address of the second byte of the entire array. Oh, well.
array+1 not the address of the second byte, but rather of the second element. sizeof(array+1) returns the size of a pointer to int for an array of int. What would you expect an address to be stored in?
Since you asked: I was just guessing that the array identifier has a size of the full array (correct guess); that array+1 would be the address of the second byte of the array (wrong guess); that sizeof array+1 would be undefined (wrong guess). At least I checked. But I was surprised, that's all.
If you want the gory details, search news:comp.lang.c for
"Chris Torek" and "The Rule". He has submitted many posts
explaining the relationship between arrays and pointers. http://groups.google.com/groups?q=Ch...di.com&rnum=10
{The above URL should be on one line with no spaces}
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:P6******************@newssvr16.news.prodigy.c om... Gary wrote: "Rolf Magnus" <ra******@t-online.de> wrote in message news:bv*************@news.t-online.com...
Gary wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message news:bv**********@newstree.wise.edt.ericsson.se ...
<snip> If you want the gory details, search news:comp.lang.c for "Chris Torek" and "The Rule". He has submitted many posts explaining the relationship between arrays and pointers. http://groups.google.com/groups?q=Ch...di.com&rnum=10
I checked this reference, and I believe it is wrong. He says (&a)+1 point
you past the end of the array. I tried it. It doesn't. It takes you to the
next element of the array. (See my original post for the code.)
--
Gary
> > http://groups.google.com/groups?q=Ch...di.com&rnum=10 I checked this reference, and I believe it is wrong. He says (&a)+1 point you past the end of the array. I tried it. It doesn't. It takes you to the next element of the array. (See my original post for the code.) -- Gary
He's right, if your compiler gets this wrong it is broken.
a decays to a pointer to the first element but
&a is a pointer to the _whole_ array so (&a) + 1 must point past the end of
the whole array.
John
Gary wrote: ... I checked this reference, and I believe it is wrong. He says (&a)+1 point you past the end of the array.
Yes, it does.
I tried it. It doesn't. It takes you to the next element of the array.
You must've misinterpreted the results. Or your compiler is seriously
broken.
(See my original post for the code.)
In terms of your original post it should be '&myArray + 1'. I don't see
anything similar there.
--
Best regards,
Andrey Tarasevich
Gary wrote: ... Since you asked: I was just guessing that the array identifier has a size of the full array (correct guess); that array+1 would be the address of the second byte of the array (wrong guess); ...
I don't understand why would you ever expect the result of a pointer
operation to be "the second byte" of anything. Units of pointer
arithmetic are complete objects, not "bytes". The closest you can get to
"byte pointer arithmetic" is using a 'char*' pointer. But you don't have
any 'char*'s in your code.
--
Best regards,
Andrey Tarasevich
Andrey Tarasevich wrote: Gary wrote:
... Since you asked: I was just guessing that the array identifier has a size of the full array (correct guess); that array+1 would be the address of the second byte of the array (wrong guess); ...
I don't understand why would you ever expect the result of a pointer operation to be "the second byte" of anything.
Well, I can. When I wrote my first C code (using BDS C) I was coming
off assembler code and my natural thought was that a pointer offset was
allways in bytes - it wasn't until I could not figure out why the
compiler was doing what it was and I cracked open my K&R that I read all
about pointer arithmetic and it finally made sense.
It's also one of my standard interview questions.
If p is a pointer to objects of type T, what does the expression (p+1)
evaluate to.
After having asked that question for many years, I still get the "one
byte after" answer on occaision.
Gianni Mariani wrote: I don't understand why would you ever expect the result of a pointer operation to be "the second byte" of anything.
Well, I can. When I wrote my first C code (using BDS C) I was coming off assembler code and my natural thought was that a pointer offset was allways in bytes - it wasn't until I could not figure out why the compiler was doing what it was and I cracked open my K&R that I read all about pointer arithmetic and it finally made sense.
Heh, similar for me. It really took me a while to grasp the whole idea
of pointer types and get a clue of why that's a good concept. After
all, when you're in assembler, a pointer is just a variable that holds
a memory address, and it's me who decides what will be at that address.
Gary wrote: I've always taught that arrays are not pointers. So how come I can do:
#include <iostream> #include <cstdlib>
using namespace std;
int main( ) { int myArray[10]={1, 10}; cout << sizeof(myArray) << endl; cout << sizeof(myArray+1) << endl; cout << *myArray << endl; cout << *(myArray+1) << endl; return 0; }
myArray is the *name* of the array.
The size of myArray is 40 bytes.
There is an *implicit* conversion of the name myArray
into a pointer to the first element of myArray
in the expressions 'myArray+1', '*myArray' and *(myArray+1)'.
The size of a pointer is 4 bytes on your machine.
*myArray is a reference to myArray[0] and
*(myArray+1) is a reference to myArray[1].
"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:10*************@news.supernews.com... Gary wrote: ... Since you asked: I was just guessing that the array identifier has a
size of the full array (correct guess); that array+1 would be the address of the second byte of the array (wrong guess); ...
I don't understand why would you ever expect the result of a pointer operation to be "the second byte" of anything. Units of pointer arithmetic are complete objects, not "bytes". The closest you can get to "byte pointer arithmetic" is using a 'char*' pointer. But you don't have any 'char*'s in your code.
Thank you Andrey. What really amazes me is the quickness with which you guys
respond. I saw my error in my testing rather quickly after posting and
recalled that post. In the five minutes it was "in the wild" I got 5
answers! Maybe cancel isn't working on your feed.
Your posts, Andrey, strike me as the most understandable, with the added
grace that they don't attack or downplay the intelligence of the OP.
BTW, I wasn't attempting to troll, it just happened accidentally.
I guess it just drives me crazy when I see "Use of an array decays to a
pointer to the first element IN MOST CASES." Never an explanation of what
cases it doesn't, just the hint. Then my imagination runs wild.
--
Gary
Gary wrote:
[SNIP] I guess it just drives me crazy when I see "Use of an array decays to a pointer to the first element IN MOST CASES." Never an explanation of what cases it doesn't, just the hint. Then my imagination runs wild.
Since you address me. When such comment is written, it is written to get
you into the right direction. If you need more information, you ask. And
you did. Others were faster than me and jumped in and answered you, so I
was not needed anymore.
As for my post, please point out the text where I have accused you of
trolling. If you cannot, please do not accuse me of doing that!
--
Attila aka WW
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bv**********@newstree.wise.edt.ericsson.se... Gary wrote: [SNIP] I guess it just drives me crazy when I see "Use of an array decays to a pointer to the first element IN MOST CASES." Never an explanation of what cases it doesn't, just the hint. Then my imagination runs wild.
As for my post, please point out the text where I have accused you of trolling. If you cannot, please do not accuse me of doing that!
I was not accusing anyone of accusing me of trolling. Does that compute?
It occurred to me that someone might think I was just trolling, which I
didn't mean to do, and so I add that comment in case one of the others got
that idea.
Anyway, it is not you for sure. You have always been a real gentleman,
except when dealing with sleaze. (...and then we'll get 'em!)
Thanks,
--
Gary
Gary wrote:
[SNIP] Anyway, it is not you for sure. You have always been a real gentleman, except when dealing with sleaze. (...and then we'll get 'em!)
Since I have no idea what sleaze is, I give up. :-)
--
Attila aka WW
Attila Feher wrote: Gary wrote: [SNIP] Anyway, it is not you for sure. You have always been a real gentleman, except when dealing with sleaze. (...and then we'll get 'em!)
Since I have no idea what sleaze is, I give up. :-)
It's backformed from "sleazy".
One entry found for sleazy.
Main Entry: slea·zy
Pronunciation: 'slE-zE also 'slA-
Function: adjective
Inflected Form(s): slea·zi·er; -est
Etymology: origin unknown
1 a : lacking firmness of texture : FLIMSY b : carelessly made of
inferior materials : SHODDY
2 a : marked by low character or quality <sleazy tabloids> b : SQUALID,
DILAPIDATED <sleazy bars>
I believe sense 2a is the indicated one.
Brian Rodenborn This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matteo Settenvini |
last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using
g++ 3.3.x.
I had been taught that an array isn't a lot different from a pointer
(in fact you can use the pointer arithmetics to...
|
by: Linny |
last post by:
Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the...
|
by: Ian Todd |
last post by:
Hi,
I am trying to read in a list of data from a file. Each line has a string in
its first column. This is what i want to read. I could start by saying
char to read in 1000 lines to the array( i...
|
by: Charles Banas |
last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to
get some opinions and comments on it, hopefully so i can gain some
sort of insight as to why this works.
at the top of the...
|
by: Me |
last post by:
Just a question/observation out of frustration.
I read in depth the book by Peter Van Der Linden entitled
"Expert C Programming" (Deep C Secrets). In particular the
chapters entitled:
4: The...
|
by: ketema |
last post by:
Hello, I was wondering if someone could help me with a function I am
trying to write. The purpose of the function is to read in text from a
file in the following format:
FIRSTNAME LASTNAME...
|
by: raphfrk |
last post by:
I have the following code:
char buf;
printf("%lp\n", buf);
printf("%lp\n", &buf);
printf("%lp\n", buf);
printf("%lp\n", buf);
printf("%d\n", buf-buf);
|
by: Martin Jørgensen |
last post by:
Hi,
I'm relatively new with C-programming and even though I've read about
pointers and arrays many times, it's a topic that is a little confusing
to me - at least at this moment:
----
1)...
|
by: I.M. !Knuth |
last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until
I started goofing around with this:
...
|
by: Christian Maier |
last post by:
Hi
After surfing a while I have still trouble with this array thing. I
have the following function and recive a Segmentation fault, how must
I code this right??
Thanks
Christian Maier
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| | |