|
From: Siva C. <siv...@ya...> - 2007-08-24 11:54:56
|
Hi,
First let me make a few notes which will help
understand your code better. I am assuming that you
are working on a 32 bit system.
1. pa is a pointer of type bartek.
2. The size of the bartek datatype is 12 bytes. It is
not 9 bytes since the size in bytes should be a
multiple of 4 in a 32 bit system.
3. Since size of bartek is 12 bytes, pa+2 will point
to a location which is 24 bytes ahead of the location
pointed by pa.
4. The size of int is 4 bytes.
With the above points in mind, pa+2 points to a
location which is 24/4=6 int locations away from pa.
Which is to say that pa+2 points to the 7th integer in
the array x. The 7th integer in the array is x[6].
Hope I could clear your confusion.
/Siva Chandra
--- Bartosz Åliwa <ba...@o2...> wrote:
> Hi.
>
> Could you tell me, why pointer 'pa' shows 6 element
> of the table (x[6]). Why it does not show second
> element? Here is the code:
>
> #include <cstdlib>
> #include <iostream>
> #include <stdio.h>
> #include <conio.h>
>
>
>
> using namespace std;
>
> struct bartek {
> int a;
> char c[4];
> int b;
> } *wsk1_bartek;
>
> int x[1000];
>
> int main(int argc, char *argv[])
> {
> system("PAUSE");
>
> bartek *pa;
>
> pa = (bartek*) x;
>
> for(int i = 0 ; i < 100; ++i) {
> x[i] = i;
> }
>
> cout <<" After shift +2: " << (pa+2)->a << endl;
>
> cout <<" Global object read on 6 position x[6]: " <<
> x[6]
> << endl;
>
> system("PAUSE");
>
> return EXIT_SUCCESS;
>
> }
>
>
> ...and here is the output:
>
> Press any key to continue...
> After shift +2: 6
> Global object on 6 position x[6]: 6
> Press any key to continue...
>
>
>
-------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems?
> Stop.
> Now Search log events and configuration files using
> AJAX and a browser.
> Download your FREE copy of Splunk now >>
> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE:
> http://www23.brinkster.com/noicys/devcpp/ub.htm
>
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
____________________________________________________________________________________
Sick sense of humor? Visit Yahoo! TV's
Comedy with an Edge to see what's on, when.
http://tv.yahoo.com/collections/222
|