Getting address of structure to string array / pointer problem

T

Timo

I am trying to get address of myStruct to a string array texts[].
I am using M$ Visual C++ 6.0 (this is not OS specific question, though,
this code should also work on 16 bit embedded compiler ;)).

This is my code:

typedef struct
{
const unsigned short a;
const unsigned long b;
} testStruct;

const testStruct myStruct[] =
{
0x0001, 0x00000002,
0x0003, 0x00000004
};

#define STR_LEN_MAX 15

const unsigned char texts[][STR_LEN_MAX] =
{
&myStruct[0], "string 1",
"string 2",
"string 3"
};

When I run the code, debugger shows the content of texts[] as:
00 73 74 72 69 6E 67 20 31 00 00 00 00 00 00
73 74 72 69 6E 67 20 32 00 00 00 00 00 00 00
73 74 72 69 6E 67 20 33 00 00 00 00 00 00 00

As you can see, the address of myStruct (0x00422020 in this case), is
not stored. I also tried some other syntaxes with no success:

(testStruct *)&myStruct[0], "string 1",

It seems that compiler is only storing 8 bits of address of myStruct
to texts[] array, which I guess is correct, because texts[] is 2-dimensional
unsigned char array. So, I figured out that I should store each byte
of myStuct address seperately, tried this:

(unsigned char *)(&myStruct[0] & 0xff), "string 1",

Compiler gives error:
error C2296: '&' : illegal, left operand has type 'const struct testStruct *'

Another guess:
(unsigned char *)((&myStruct[0]) & 0xff), "string 1",
-> error C2296: '&' : illegal, left operand has type 'const struct testStruct *'

Is there any way I can do this?

BR,
Timo
 
A

Al Bowers

Timo said:
I am trying to get address of myStruct to a string array texts[].
I am using M$ Visual C++ 6.0 (this is not OS specific question, though,
this code should also work on 16 bit embedded compiler ;)).

This is my code:

typedef struct
{
const unsigned short a;
const unsigned long b;
} testStruct;

const testStruct myStruct[] =
{
0x0001, 0x00000002,
0x0003, 0x00000004
};

#define STR_LEN_MAX 15

const unsigned char texts[][STR_LEN_MAX] =
{
&myStruct[0], "string 1",
"string 2",
"string 3"
};

When I run the code, debugger shows the content of texts[] as:
00 73 74 72 69 6E 67 20 31 00 00 00 00 00 00
73 74 72 69 6E 67 20 32 00 00 00 00 00 00 00
73 74 72 69 6E 67 20 33 00 00 00 00 00 00 00

As you can see, the address of myStruct (0x00422020 in this case), is
not stored. I also tried some other syntaxes with no success:

(testStruct *)&myStruct[0], "string 1",

It seems that compiler is only storing 8 bits of address of myStruct
to texts[] array, which I guess is correct, because texts[] is 2-dimensional
unsigned char array. So, I figured out that I should store each byte
of myStuct address seperately, tried this:

(unsigned char *)(&myStruct[0] & 0xff), "string 1",

Compiler gives error:
error C2296: '&' : illegal, left operand has type 'const struct testStruct *'

Another guess:
(unsigned char *)((&myStruct[0]) & 0xff), "string 1",
-> error C2296: '&' : illegal, left operand has type 'const struct testStruct *'

Is there any way I can do this?

Do you want to make a string of the address? If so, use function
sprintf. Use sscanf to convert it back to a pointer value.

#include <stdio.h>
#include <string.h>

typedef struct
{
const unsigned short a;
const unsigned long b;
} testStruct;

const testStruct myStruct[] ={{1,2},{3,4}};

#define STR_LEN_MAX 32

int main(void)
{
testStruct *ptr;
int i;
char texts[][STR_LEN_MAX] =
{"", "string 1", "string 2", "string 3"};

sprintf(texts[0],"%p",(void *)&myStruct[0]);
for(i = 0; i < 4;i++)
printf("texts[%d] = \"%s\"\n",i,texts);
sscanf(texts[0],"%p",(void **)&ptr);
if(ptr == &myStruct[0])
puts("\nIt worked, ptr is equal &myStruct[0]");
else puts("\nIt didn't work, ptr is not equal to &myStruct[0]");
return 0;
}
 
M

Martin Ambuhl

Timo said:
I am trying to get address of myStruct to a string array texts[].
I am using M$ Visual C++ 6.0 (this is not OS specific question, though,
this code should also work on 16 bit embedded compiler ;)).

This is my code:

typedef struct
{
const unsigned short a;
const unsigned long b;
} testStruct;

const testStruct myStruct[] =
{
0x0001, 0x00000002,
0x0003, 0x00000004
};

#define STR_LEN_MAX 15

const unsigned char texts[][STR_LEN_MAX] =
{
&myStruct[0], "string 1",
"string 2",
"string 3"
};

When I run the code, debugger shows the content of texts[] as:
00 73 74 72 69 6E 67 20 31 00 00 00 00 00 00
73 74 72 69 6E 67 20 32 00 00 00 00 00 00 00
73 74 72 69 6E 67 20 33 00 00 00 00 00 00 00

As you can see, the address of myStruct (0x00422020 in this case), is
not stored. ...

#include <stdio.h>

typedef struct
{
const unsigned short a;
const unsigned long b;
} testStruct;

const testStruct myStruct[] = {
{0x0001, 0x00000002},
{0x0003, 0x00000004}
};

const char *texts[] = {
(char *) myStruct,
"string 1",
"string 2",
"string 3"
};

int main(void)
{
printf("myStruct is at %p\n", (void *) myStruct);
printf("The first element of texts is %p\n", (void *) texts[0]);
printf("Does this do it for you?\n");
return 0;
}


myStruct is at 15e0
The first element of texts is 15e0
Does this do it for you?
 
T

Timo

const char *texts[] = {
(char *) myStruct,
"string 1",
"string 2",
"string 3"
};

That doesn't work in M$ Visual C++ 6.0. The address of myStruct is
0x00422020. Debugger shows memory of texts[0] as:
00 73 74 72 69 6E 67 20 31 00 00 00 00 00 00
The address is still not stored to texts[0]. What compiler are you using?
Does MSVC++ follow ANSI-C standard here (wouldn't suprise me if it
didn't).

Timo
 
T

Timo

Do you want to make a string of the address?
No.

If so, use function
sprintf. Use sscanf to convert it back to a pointer value.

Can't use it, texts[] has to be const, the address of myStruct
has to be stored there at compile time. Changing type/format of
texts[] is not possible.

Timo
 
A

Arthur J. O'Dwyer

Martin Ambuhl said:
const char *texts[] = {
(char *) myStruct,
"string 1",
"string 2",
"string 3"
};

That doesn't work in M$ Visual C++ 6.0. The address of myStruct is
0x00422020. Debugger shows memory of texts[0] as:
00 73 74 72 69 6E 67 20 31 00 00 00 00 00 00
The address is still not stored to texts[0]. What compiler are you using?
Does MSVC++ follow ANSI-C standard here (wouldn't suprise me if it
didn't).

What does Martin's program print when you *compile* it and
*run* it? That will tell much more than what some debugger
prints. (More specifically, I think you're probably seeing
the data stored at the *address* 'texts[0]', not the contents
of the array 'texts'. Yes, for those paying attention, that
doesn't quite fit the data -- but I'm still betting that Timo
didn't run the program yet.)

-Arthur
 
T

Timo

#define STR_LEN_MAX 15

const unsigned char texts[][STR_LEN_MAX] =
{
&myStruct[0], "string 1",
"string 2",
"string 3"
};
const char *texts[] = {
(char *) myStruct,
"string 1",
"string 2",
"string 3"
};


Now I got it, your texts[] is array of char pointers, mine is
2-dimensional char array. However, your solution doesn't quite suite
me, I don't want to change the format of texts[] in any way, I have
some code which is dependent that the format/definition of texts[] is
_exactly_ what it is now, don't wan't to change the code (don't ask
why ;)). So, could it be possible to store the 32/16 bit address of
mystruct in 2/4 pieces, to texts[]? I tried something like this:

(unsigned char)(myStruct),(unsigned
char)((myStruct>>8)&0xff),(unsigned
char)((myStruct>>16)&0xff),(unsigned char)((myStruct>>24)&0xff),
"string 1",

but got all sorts of error messages, such as:
error C2296: '>>' : illegal, left operand has type 'const struct
testStruct [2]'

Any idea how to force the address of myStuct to texts[], _without_
anyhow
changing type of texts[]?

Timo
 
B

Barry Schwarz

I am trying to get address of myStruct to a string array texts[].
I am using M$ Visual C++ 6.0 (this is not OS specific question, though,
this code should also work on 16 bit embedded compiler ;)).

This is my code:

typedef struct
{
const unsigned short a;
const unsigned long b;
} testStruct;

const testStruct myStruct[] =
{
0x0001, 0x00000002,
0x0003, 0x00000004
};

#define STR_LEN_MAX 15

const unsigned char texts[][STR_LEN_MAX] =
{
&myStruct[0], "string 1",

How did you avoid getting an error message here. My VC++ 6 reports
"C4047: 'initializing' : 'const unsigned char ' differs in levels of
indirection from 'const struct testStruct *'". The address of
myStruct[0] cannot possibly be a valid initializer for an array of
array of char.
"string 2",
"string 3"
};

When I run the code, debugger shows the content of texts[] as:
00 73 74 72 69 6E 67 20 31 00 00 00 00 00 00
73 74 72 69 6E 67 20 32 00 00 00 00 00 00 00
73 74 72 69 6E 67 20 33 00 00 00 00 00 00 00

As you can see, the address of myStruct (0x00422020 in this case), is
not stored. I also tried some other syntaxes with no success:

(testStruct *)&myStruct[0], "string 1",

It seems that compiler is only storing 8 bits of address of myStruct
to texts[] array, which I guess is correct, because texts[] is 2-dimensional
unsigned char array. So, I figured out that I should store each byte
of myStuct address seperately, tried this:

(unsigned char *)(&myStruct[0] & 0xff), "string 1",

Compiler gives error:
error C2296: '&' : illegal, left operand has type 'const struct testStruct *'

Another guess:
(unsigned char *)((&myStruct[0]) & 0xff), "string 1",
-> error C2296: '&' : illegal, left operand has type 'const struct testStruct *'

Is there any way I can do this?

BR,
Timo



<<Remove the del for email>>
 
B

Barry Schwarz

#define STR_LEN_MAX 15

const unsigned char texts[][STR_LEN_MAX] =
{
&myStruct[0], "string 1",
"string 2",
"string 3"
};
const char *texts[] = {
(char *) myStruct,
"string 1",
"string 2",
"string 3"
};


Now I got it, your texts[] is array of char pointers, mine is
2-dimensional char array. However, your solution doesn't quite suite
me, I don't want to change the format of texts[] in any way, I have
some code which is dependent that the format/definition of texts[] is
_exactly_ what it is now, don't wan't to change the code (don't ask
why ;)). So, could it be possible to store the 32/16 bit address of
mystruct in 2/4 pieces, to texts[]? I tried something like this:

(unsigned char)(myStruct),(unsigned
char)((myStruct>>8)&0xff),(unsigned
char)((myStruct>>16)&0xff),(unsigned char)((myStruct>>24)&0xff),
"string 1",

but got all sorts of error messages, such as:
error C2296: '>>' : illegal, left operand has type 'const struct
testStruct [2]'

Any idea how to force the address of myStuct to texts[], _without_
anyhow
changing type of texts[]?
You cannot store an address in texts. texts is not a pointer or any
aggregate that contains a pointer. It is an array of array of char.
The best you can do is initialize entry 0 with a dummy string (the
remaining initializers are good for subsequent entries) and as part of
your code you can copy (with memcpy, not strcpy) the contents of
myStruct[0] to texts[0].

You will have the additional problem that the contents of myStruct[0]
is not a string so texts[0] is fundamentally different than texts[1],
[2], and [3]. But it certainly is acceptable as an array of unsigned
char.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top