• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

A pointer question

I am learning C and I am totally confused with this sample from the book.

#include <stdio.h>

int main()
{
    char data[4];
    short *s;
    int *w;
    w=(int*)data;
    *w=0x12345678;;
    s=(short*)data;
    s++;
    (*s)++;
    printf("addr of data is %x\n",data);
    printf("values are %x,%x,%x,%x,\n",data[0],data[1],data[2],data[3]);
    printf("s contains %x, dereferences to %x\n",s,*s);
    printf("w contains %x, dereferences to %x\n",w,*w);
    return 0;
}

The output in my PC is:

addr of data is 22eea4
values are 78,56,35,12,
s contains 22eea6, dereferences to 1235
w contains 22eea4, dereferences to 12355678

Suppose I know the addr of data(22eea4 in this case), what's the hell the left three line coming from? How to get their values?

Thanks a lot.
0
tiger0516
Asked:
tiger0516
  • 5
  • 4
2 Solutions
 
sunnycoderCommented:
Hi tiger0516,

Here ints are 4 bytes and shorts are 2 bytes

> addr of data is 22eea4
straight forward

> values are 78,56,35,12,
straight forward

> s contains 22eea6, dereferences to 1235
when you increment a pointer, it gets incremented by the size of its type. Since s was short, the pointer gets incremented by 2 bytes. Hence, its address is a6 and not a4

> w contains 22eea4, dereferences to 12355678
should have been 12345678 and not 12355678 ... since w points to same memory location as data, you get the same values

Cheers!
0
 
tiger0516Author Commented:
> values are 78,56,35,12,
straight forward

//blush, but why?
0
 
tiger0516Author Commented:
> w contains 22eea4, dereferences to 12355678
should have been 12345678 and not 12355678 .

I just verified. It is really 12355678 in my computer. I promise you.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sunnycoderCommented:
data is 4 char bytes .. w is a int * which places 0x12345678 at the location pointed to by w - which is same as data. Your machine would be a small endian, so a byte int 0x12345678 is stored as 78 56 35 12
You would find this information helpful:
http://whatis.techtarget.com/definition/0,,sid9_gci211659,00.html
http://www.rdrop.com/~cary/html/endian_faq.html

>I just verified. It is really 12355678 in my computer. I promise you.
This sounds rather improbable to me ... you stored 0x12345678 in a variable and simply print it without making any modifications and it prints a different value!!! Cant be if you are on any sane system. Or did you skip some code in between?
0
 
tiger0516Author Commented:
No. I just copy & past my this sample code here; did not change anything.

Oops!

(Would you mind copy & test the code on your machine?)
0
 
tiger0516Author Commented:
>data is 4 char bytes .. w is a int * which places 0x12345678 at the location pointed to by w - which is same as data. Your machine would be a small endian, so a byte int 0x12345678 is stored as 78 56 35 12

I would understand if the array elments are 78,56,34,12

But here the 3rd is 35, I am confused. :-(
0
 
sunnycoderCommented:
>But here the 3rd is 35, I am confused. :-(
I did not notice that ... my bad

I do not have a C compiler around me :( so cant compile the code myself

And this is the cause of misery
(*s)++;

you are incrementing the value at location pointed to by short .. so it increments the 34 to 35
0
 
tiger0516Author Commented:
So for 12345678

s, since it is a short, can access to 1234, right? By s++, it is 1234+1=1235, right?

Meanwhile, 12345678 is shared by s, w and data[], so any change to 12345678 will change all  of s,w,data[]?

I think I get it.

Gee! Pointer is so confusing!
0
 
sunnycoderCommented:
You got it right :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now