Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Getting address of structure to string array / pointer problem (http://www.velocityreviews.com/forums/t316482-getting-address-of-structure-to-string-array-pointer-problem.html)

Timo 12-04-2003 12:01 PM

Getting address of structure to string array / pointer problem
 
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

Al Bowers 12-04-2003 01:43 PM

Re: Getting address of structure to string array / pointer problem
 


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: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


Martin Ambuhl 12-04-2003 07:57 PM

Re: Getting address of structure to string array / pointer problem
 
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


Timo 12-05-2003 06:00 AM

Re: Getting address of structure to string array / pointer problem
 
Martin Ambuhl <mambuhl@earthlink.net> wrote in message news:<9lMzb.4042$Oe5.2810@newsread2.news.atl.earth 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

Timo 12-05-2003 06:07 AM

Re: Getting address of structure to string array / pointer problem
 
Al Bowers <xabowers@rapidsys.com> wrote in message news:<bqndl9$24210t$1@ID-169908.news.uni-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

Arthur J. O'Dwyer 12-05-2003 06:18 AM

Re: Getting address of structure to string array / pointer problem
 

On Fri, 4 Dec 2003, Timo wrote:
>
> Martin Ambuhl <mambuhl@earthlink.net> 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

Timo 12-05-2003 06:51 AM

Re: Getting address of structure to string array / pointer problem
 
Martin Ambuhl <mambuhl@earthlink.net> wrote in message news:<9lMzb.4042$Oe5.2810@newsread2.news.atl.earth 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>>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

Barry Schwarz 12-05-2003 11:09 PM

Re: Getting address of structure to string array / pointer problem
 
On 4 Dec 2003 04:01:06 -0800, t_i_m_0_news@yahoo.com (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>>

Barry Schwarz 12-06-2003 04:24 AM

Re: Getting address of structure to string array / pointer problem
 
On 4 Dec 2003 22:51:13 -0800, t_i_m_0_news@yahoo.com (Timo) wrote:

>Martin Ambuhl <mambuhl@earthlink.net> wrote in message news:<9lMzb.4042$Oe5.2810@newsread2.news.atl.earth 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>>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>>


All times are GMT. The time now is 04:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.