Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Getting address of structure to string array / pointer problem

Reply
Thread Tools

Getting address of structure to string array / pointer problem

 
 
Timo
Guest
Posts: n/a
 
      12-04-2003
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
 
Reply With Quote
 
 
 
 
Al Bowers
Guest
Posts: n/a
 
      12-04-2003


Timo wrote:
> 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[i]);
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;
}


--
Al Bowers
Tampa, Fl USA
mailto: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-04-2003
Timo wrote:

> 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?





--
Martin Ambuhl

 
Reply With Quote
 
Timo
Guest
Posts: n/a
 
      12-05-2003
Martin Ambuhl <(E-Mail Removed)> wrote in message news:<9lMzb.4042$(E-Mail Removed) link.net>...

<CLIP>

>
> 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
 
Reply With Quote
 
Timo
Guest
Posts: n/a
 
      12-05-2003
Al Bowers <(E-Mail Removed)> wrote in message news:<bqndl9$24210t$(E-Mail Removed)-berlin.de>...

<CLIP>

> 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
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      12-05-2003

On Fri, 4 Dec 2003, Timo wrote:
>
> Martin Ambuhl <(E-Mail Removed)> wrote
> >
> > 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
 
Reply With Quote
 
Timo
Guest
Posts: n/a
 
      12-05-2003
Martin Ambuhl <(E-Mail Removed)> wrote in message news:<9lMzb.4042$(E-Mail Removed) link.net>...

<CLIP>

> > #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>>&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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-05-2003
On 4 Dec 2003 04:01:06 -0800, (E-Mail Removed) (Timo) wrote:

>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>>
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-06-2003
On 4 Dec 2003 22:51:13 -0800, (E-Mail Removed) (Timo) wrote:

>Martin Ambuhl <(E-Mail Removed)> wrote in message news:<9lMzb.4042$(E-Mail Removed) link.net>...
>
><CLIP>
>
>> > #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>>&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>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Address of array && address of pointer to array Stanley Rice C Programming 33 09-20-2011 12:47 AM
structure address = structure first field address ? Vincent De Groote C Programming 31 05-19-2009 04:09 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Copy String structure "A" to string structure "B" Leo Nunez C Programming 3 02-09-2005 05:14 AM



Advertisments