Getting address of structure to string array / pointer problem

Discussion in 'C Programming' started by Timo, Dec 4, 2003.

  1. Timo

    Timo Guest

    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
     
    Timo, Dec 4, 2003
    #1
    1. Advertising

  2. Timo

    Al Bowers Guest

    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);
    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: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Dec 4, 2003
    #2
    1. Advertising

  3. 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
     
    Martin Ambuhl, Dec 4, 2003
    #3
  4. Timo

    Timo Guest

    Martin Ambuhl <> wrote in message news:<9lMzb.4042$>...

    <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, Dec 5, 2003
    #4
  5. Timo

    Timo Guest

    Al Bowers <> wrote in message news:<bqndl9$24210t$-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
     
    Timo, Dec 5, 2003
    #5
  6. On Fri, 4 Dec 2003, Timo wrote:
    >
    > Martin Ambuhl <> 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
     
    Arthur J. O'Dwyer, Dec 5, 2003
    #6
  7. Timo

    Timo Guest

    Martin Ambuhl <> wrote in message news:<9lMzb.4042$>...

    <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
     
    Timo, Dec 5, 2003
    #7
  8. On 4 Dec 2003 04:01:06 -0800, (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, Dec 5, 2003
    #8
  9. On 4 Dec 2003 22:51:13 -0800, (Timo) wrote:

    >Martin Ambuhl <> wrote in message news:<9lMzb.4042$>...
    >
    ><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>>
     
    Barry Schwarz, Dec 6, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leo Nunez
    Replies:
    3
    Views:
    1,230
    Neil Kurzman
    Feb 9, 2005
  2. jimjim
    Replies:
    16
    Views:
    843
    Jordan Abel
    Mar 28, 2006
  3. Vincent De Groote

    structure address = structure first field address ?

    Vincent De Groote, Apr 29, 2009, in forum: C Programming
    Replies:
    31
    Views:
    2,029
  4. Stanley Rice

    Address of array && address of pointer to array

    Stanley Rice, Sep 14, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,144
    Keith Thompson
    Sep 20, 2011
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    457
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page