Tobi schrieb:
> Mit dieser Zeile
>
1 | > const char *ptr2 = (const char*)&MyTest[0];
|
2 | >
|
>
> will auf die einzelnen Member Test1 bis Test3 mittels Adressen zugreifen
> können.
Ich denke, es wir Zeit das mal aufzulösen.
&MyTest[0] ist die Adresse von MyTest selber. Das ist aber nicht das was
du willst! DU willst den Inhalt von MyTest[0] haben! Das ist ebenfalls
eine Adresse, nämlich die Adresse4 von test1, die du vorher dort
reingeschrieben hast.
Aber lass uns erst mal von vorne anfangen
Du hast
1 | const char test1[] = {"test 1"};
|
2 | const char test2[] = {"test 2"};
|
3 | const char test3[] = {"test 3"};
|
1 | test1
|
2 | +---+---+---+---+---+---+---+
|
3 | | t | e | s | t | | 1 | \0|
|
4 | +---+---+---+---+---+---+---+
|
5 |
|
6 | test2
|
7 | +---+---+---+---+---+---+---+
|
8 | | t | e | s | t | | 2 | \0|
|
9 | +---+---+---+---+---+---+---+
|
10 |
|
11 |
|
12 | test3
|
13 | +---+---+---+---+---+---+---+
|
14 | | t | e | s | t | | 3 | \0|
|
15 | +---+---+---+---+---+---+---+
|
weiters hast du
1 | const char *ptr1 = test1;
|
ok. damit sieht die ganze Sache so aus
1 | test1
|
2 | +---+---+---+---+---+---+---+
|
3 | +------->| t | e | s | t | | 1 | \0|
|
4 | | +---+---+---+---+---+---+---+
|
5 | |
|
6 | | test2
|
7 | | +---+---+---+---+---+---+---+
|
8 | | | t | e | s | t | | 2 | \0|
|
9 | | +---+---+---+---+---+---+---+
|
10 | |
|
11 | |
|
12 | | test3
|
13 | | +---+---+---+---+---+---+---+
|
14 | | | t | e | s | t | | 3 | \0|
|
15 | | +---+---+---+---+---+---+---+
|
16 | |
|
17 | +----------+
|
18 | |
|
19 | ptr1 |
|
20 | +-------+ |
|
21 | | o----------+
|
22 | +-------+
|
Ausserdem hast du noch
1 | static MyStructure MyTest[] =
|
2 | {
|
3 | {test1},
|
4 | {test2},
|
5 | {test3},
|
6 | };
|
damit landest du bei
1 | MyTest
|
2 | +---------------------+
|
3 | | +----------------+|
|
4 | | | temp: o-------------------------+
|
5 | | +----------------+| |
|
6 | +---------------------+ |
|
7 | | +----------------+| |
|
8 | | | temp: o---------------------+ |
|
9 | | +----------------+| | |
|
10 | +---------------------+ | |
|
11 | | +----------------+| | |
|
12 | | | temp: o-----------------+ | |
|
13 | | +----------------+| | | |
|
14 | +---------------------+ | | |
|
15 | | | |
|
16 | +-------------------------------+ | |
|
17 | | +--------------------------------+ |
|
18 | | | +---------------------------------+
|
19 | | | |
|
20 | | | |
|
21 | | | |
|
22 | | | |
|
23 | | | | test1
|
24 | | | +--->+---+---+---+---+---+---+---+
|
25 | | +------->| t | e | s | t | | 1 | \0|
|
26 | | || +---+---+---+---+---+---+---+
|
27 | | ||
|
28 | | || test2
|
29 | | |+------>+---+---+---+---+---+---+---+
|
30 | | | | t | e | s | t | | 2 | \0|
|
31 | | | +---+---+---+---+---+---+---+
|
32 | | |
|
33 | | |
|
34 | | | test3
|
35 | +-|------->+---+---+---+---+---+---+---+
|
36 | | | t | e | s | t | | 3 | \0|
|
37 | | +---+---+---+---+---+---+---+
|
38 | |
|
39 | +----------+
|
40 | |
|
41 | ptr1 |
|
42 | +-------+ |
|
43 | | o----------+
|
44 | +-------+
|
Zum Schluss machst du noch
1 | const char *ptr2 = (const char*)&MyTest[0];
|
1 | ptr2
|
2 | +-------------+
|
3 | +-------------------------o |
|
4 | | +-------------+
|
5 | |
|
6 | |
|
7 | | MyTest
|
8 | | +---------------------+
|
9 | +-> | +----------------+|
|
10 | | | temp: o-------------------------+
|
11 | | +----------------+| |
|
12 | +---------------------+ |
|
13 | | +----------------+| |
|
14 | | | temp: o---------------------+ |
|
15 | | +----------------+| | |
|
16 | +---------------------+ | |
|
17 | | +----------------+| | |
|
18 | | | temp: o-----------------+ | |
|
19 | | +----------------+| | | |
|
20 | +---------------------+ | | |
|
21 | | | |
|
22 | +-------------------------------+ | |
|
23 | | +--------------------------------+ |
|
24 | | | +---------------------------------+
|
25 | | | |
|
26 | | | |
|
27 | | | |
|
28 | | | |
|
29 | | | | test1
|
30 | | | +--->+---+---+---+---+---+---+---+
|
31 | | +------->| t | e | s | t | | 1 | \0|
|
32 | | || +---+---+---+---+---+---+---+
|
33 | | ||
|
34 | | || test2
|
35 | | |+------>+---+---+---+---+---+---+---+
|
36 | | | | t | e | s | t | | 2 | \0|
|
37 | | | +---+---+---+---+---+---+---+
|
38 | | |
|
39 | | |
|
40 | | | test3
|
41 | +-|------->+---+---+---+---+---+---+---+
|
42 | | | t | e | s | t | | 3 | \0|
|
43 | | +---+---+---+---+---+---+---+
|
44 | |
|
45 | +----------+
|
46 | |
|
47 | ptr1 |
|
48 | +-------+ |
|
49 | | o----------+
|
50 | +-------+
|
So.
Das ist dein Szenario, das du aufgebaut hast.
Warum erwartest du jetzt, dass ptr1 und ptr2 als gleich gewertet werden.
Schau dir dir Grafik an. Die Pfeile, die in ptr1 bzw. ptr2 losgehen,
münden an komplett unterschiedlichen Stellen. ptr1 und ptr2 sind daher
ganz einfach nicht gleich.
Der Pfeil, der in ptr1 beginnt, endet bei test1
Der Pfeil, der in ptr2 beginnt, endet beim ersten Array Element in
MyTest.
Logisch, dass die beiden nicht gleich sein können.
Und nochwas:
Wenn du das casten musst, dann machst du mit Sicherheit etwas falsch.
Pointer Umcasten ist die absolute Ausnahme und sollte bei dir NIE
notwendig sein.