Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array address arithmetic

Reply
Thread Tools

Array address arithmetic

 
 
Bart C
Guest
Posts: n/a
 
      12-10-2007
"vjay" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) lkaboutprogramming.com...
> Check the code below guys
> void main()
> {
> char a[5] = {1,2,3,4,5};
> char *ptr;
> ptr = (char*)(&a + 1);
> printf("%d",*(ptr-1));
> }
>
> The output of the program is 5


Thanks, your post and it's replies has clarified something that I hadn't
fully grasped; in int a[5], then:

&a is not the same thing as a, when considering Type.

Apparently a is the same as &a[0] (pointer to int), &a means a pointer to
the entire array (pointer to array of 5 ints). The values however are the
same. (Although why &a doesn't decompose to &(&a[0]) is another mystery;
maybe it only decomposes when there isn't an & in front)

> void main()


With quotes, this gives 5 times as many Google hits as "int main(void)".
Looks like a lot of people can't get it right.

Bart


 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-10-2007
Bart C wrote:
>
> "vjay" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) lkaboutprogramming.com...
> > Check the code below guys
> > void main()
> > {
> > char a[5] = {1,2,3,4,5};
> > char *ptr;
> > ptr = (char*)(&a + 1);
> > printf("%d",*(ptr-1));
> > }
> >
> > The output of the program is 5

>
> Thanks, your post and it's replies
> has clarified something that I hadn't
> fully grasped; in int a[5], then:
>
> &a is not the same thing as a, when considering Type.
>
> Apparently a is the same as &a[0] (pointer to int),
> &a means a pointer to
> the entire array (pointer to array of 5 ints).
> The values however are the same.
> (Although why &a doesn't decompose to &(&a[0]) is another mystery;
> maybe it only decomposes when there isn't an & in front)


That's very close to being correct.

N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators

[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'' is converted to an expression with type ``pointer to
type'' that points to the initial element of the array
object and is not an lvalue.

--
pete
 
Reply With Quote
 
 
 
 
vjay
Guest
Posts: n/a
 
      12-10-2007
Thank you all guys now i got it.

--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      12-10-2007
"Tomás Ó hÉilidhe" <(E-Mail Removed)> wrote:
> "vjay" <(E-Mail Removed)> wrote
> > char a[5] = {1,2,3,4,5};

>
> Unusual to store numbers in a char, ...


What else does one store in a char?

--
Peter
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      12-10-2007
vjay wrote:
>
> Check the code below guys
> void main()
> {
> char a[5] = {1,2,3,4,5};
> char *ptr;
> ptr = (char*)(&a + 1);
> printf("%d",*(ptr-1));
> }
>
> The output of the program is 5
>
> But what i expected was 1.When i replace &a with a in the expression ptr =
> (char*)(a + 1); i get output as 1.what is the difference between (&a + 1)
> and (a + 1) in the above mentioned expression.Help guys its confusing.


Consider:

What does it mean when you say "&something + 1"?

What is the value of "sizeof a"?

If there were a "typeof" operator, what would be "typeof a"?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-11-2007
"Bart C" <(E-Mail Removed)> writes:
[...]
> Apparently a is the same as &a[0] (pointer to int), &a means a pointer to
> the entire array (pointer to array of 5 ints). The values however are the
> same. (Although why &a doesn't decompose to &(&a[0]) is another mystery;
> maybe it only decomposes when there isn't an & in front)

[...]

The answers to these and a number of similar questions can be found in
section 6 of the comp.lang.c FAQ, <http://www.c-faq.com>.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
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
Address of array && address of pointer to array Stanley Rice C Programming 33 09-20-2011 12:47 AM
Address of array behavior in pointer arithmetic joshc C Programming 16 06-15-2006 10:14 PM
A kind of arithmetic for subnetting and ip address management lily Cisco 0 05-08-2006 09:32 AM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Arithmetic on function address Stephen Biggs C Programming 21 04-28-2004 11:17 AM



Advertisments