473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Nov 13 '05 #1
8 8920


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

Nov 13 '05 #2
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

Nov 13 '05 #3
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<9l*****************@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
Nov 13 '05 #4
Al Bowers <xa******@rapidsys.com> wrote in message news:<bq*************@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
Nov 13 '05 #5

On Fri, 4 Dec 2003, Timo wrote:

Martin Ambuhl <ma*****@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
Nov 13 '05 #6
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<9l*****************@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
Nov 13 '05 #7
On 4 Dec 2003 04:01:06 -0800, t_**********@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>>
Nov 13 '05 #8
On 4 Dec 2003 22:51:13 -0800, t_**********@yahoo.com (Timo) wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<9l*****************@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>>
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

80
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: ...
15
by: dandelion | last post by:
Hi, Just another question for the standards jockeys... Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit machine). I want to dump the context of the IVT, by treating...
3
by: Leo Nunez | last post by:
Hello! I need copy from structure "A" to "B" that contains "strings" in a one line code. Me problem like this : typedef struct tHeader{ char field1; char field2; char field3;
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
12
by: FI | last post by:
Hello All, I am relatively new to C programming and I am struck with a problem in dynamic memory allocation. I would like to know if it is ok to pass the 'memory address' returned by...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.