Re: [Dev-C++] =?utf-8?q?Pointer_location?=
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2007-08-24 12:03:38
|
6c6c6cMany errors.
The 6th element of an array is not array[6] but array[5]. The first
element is array[0], not array[1].
In this case, your pointer usage is wrong.
An int pointer may be used with the address of an integer.
x is an array of integers, and the language does not promise any
guaranteed behaviour if you use a type cast to assign a pointer of a
different type with the array, and then try to use this pointer to try to
reach around.
If we ASSUME that the struct is laid out as three integers, where the
middle integer is 4 charactesr, and the integers are 4 bytes large and
that the struct totals 12 bytes etc, etc then you would get:
(pa+2) would be the same as pa[2] and be 2*12 =3D 24 bytes forward in the
int array. 24 bytes - given 4-byte integers - would mean that the pointer
jumps over 6 integers. So, it will access the 7th integer (x[6]) which has
the value 6.
Just as expected if all assumptions was correct. But the code would still
be wrong. A change of the memory layout of the struct - which the compiler
is allowed to do - or a change of the integer size - depending on
architecture - would make your code to either crash or to emit completely
different values.
Never play with pointers. Only convert between compatible pointers.
an int pointer may be set to point at the a member of a bartek struct. But
may not be used to access anything but that specific a element.
A bartek pointer may never be used to point to an integer. A type cast
will make the compiler happy, but your code would break the assumptions
allowed by the language standard.
/pwm
On Fri, 24 Aug 2007, [UTF-8] Bartosz =C5=9Aliwa 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 =3D (bartek*) x;
>
> for(int i =3D 0 ; i < 100; ++i) {
> x[i] =3D 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
>
|