473,769 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8951


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,tex ts[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******@myrapi dsys.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("myStruc t 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*****@earthl ink.net> wrote in message news:<9l******* **********@news read2.news.atl. earthlink.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******@rapid sys.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*****@earthl ink.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*****@earthl ink.net> wrote in message news:<9l******* **********@news read2.news.atl. earthlink.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),(uns igned
char)((myStruct >>16)&0xff),(un signed 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_**********@ya hoo.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_**********@ya hoo.com (Timo) wrote:
Martin Ambuhl <ma*****@earthl ink.net> wrote in message news:<9l******* **********@news read2.news.atl. earthlink.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)((myStruc t>>8)&0xff),(un signed
char)((myStruc t>>16)&0xff),(u nsigned 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
5281
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: Hireability Portability Flexibility The likely candidates seem to be Java, VB.Net, C, C++, C#.
15
2243
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 it as an array starting at (you guessed it) 0x0000. So I would have struct iv_s* ivt = (struct iv_s *) 0x0000;
3
2996
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
1930
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 Public mGlobalRect As GM_Rectangle_t Public mPixelWidth As Int32
12
1972
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 malloc()(stored not in a pointer variable but as an element of an array of type int*) directly to free(). My program allocating and freeing memory this way is corrupting the heap. Can anybody guide me on this. Thanks in advance
2
3276
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
5
3810
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. Here is my part of my code. struct cust_data { int nCAFID; char *szFirstName;
17
2325
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 same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
7
2813
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 naming names here to remove bias - I'm trying to tell if I'm relying on implementation defined behavior or if this is a bug in the new compiler. Consider this stripped down example:
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.