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 8 8941
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/
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
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
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
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
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
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>>
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>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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#.
|
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;
|
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;
|
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
|
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
| |
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
|
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;
|
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
|
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:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |