Store Address of object pointed to

Discussion in 'C Programming' started by nicollete, Nov 10, 2009.

  1. nicollete

    nicollete Guest

    I am trying to store the address of the memory location pointed to by
    a union pointer

    say I have

    union header* p ;
    No p is pointing to some memory location during the course of the
    program and I need to store that address in a table , so that would be
    a different struct.

    what should be the variable I must use ? If I try to use (char*)p will
    the code break?

    please explain with a short example.

    Thanks!
     
    nicollete, Nov 10, 2009
    #1
    1. Advertising

  2. nicollete

    Seebs Guest

    On 2009-11-10, nicollete <> wrote:
    > union header* p ;
    > No p is pointing to some memory location during the course of the
    > program and I need to store that address in a table , so that would be
    > a different struct.


    Why not just store a "union header *"?

    > what should be the variable I must use ? If I try to use (char*)p will
    > the code break?


    You can use a pointer converted to (char *) or (void *), but if you need
    to store a pointer to a union, a pointer to that particular union is of
    course the most obvious data format to use.

    struct { int i, union header *p; } table[] = {
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    };
    ...
    union_header *p;
    ...
    table[0].p = p;

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Nov 10, 2009
    #2
    1. Advertising

  3. nicollete

    Tarique. Guest

    ...snip..

    > Why not just store a "union header *"?
    >
    > > what should be the variable I must use ? If I try to use (char*)p will
    > > the code break?

    >
    > You can use a pointer converted to (char *) or (void *), but if you need
    > to store a pointer to a union, a pointer to that particular union is of
    > course the most obvious data format to use.
    >
    >         struct { int i, union header *p; } table[] = {
    >                 { 0, 0 },
    >                 { 0, 0 },
    >                 { 0, 0 },
    >                 { 0, 0 },
    >         };
    >         ...
    >         union_header *p;
    >         ...
    >         table[0].p = p;
    >


    Ok! I've got another silly question. Sorry Just can't figure it out!

    Ive got the union in file memroutines.c:

    union header {
    struct {
    union header* next;
    union header* prev;
    size_t size;
    int h4byte1;
    int h4byte2;
    int h4byte3;
    int h4byte4;
    }s;
    double dummy_align_var;
    };

    and the following function definition in TagTables.c:

    void add_tag(char* file_name_p,
    unsigned int line_number_p,
    union header* real_address_p, /*get the warning here*/
    union header* returned_address_p,
    size_t size_p );

    When I compile with /W4 on MS VC++ 08 ,I get a warning
    1>c:pathname\tagtable.h(19) : warning C4115: 'header' : named type
    definition in parentheses

    The TagTable.h header file has the folloing content :

    #ifndef TAG_TABLE_H
    #define TAG_TABLE_H

    #ifndef _MAIN_H
    #include <stdio.h>
    #include <stdlib.h>
    #endif

    #ifndef MEM_ROUTINES_H
    #include "MemRoutines.h"
    #endif

    /*Other Functions*/
    void add_tag(char* file_name_p,
    unsigned int line_number_p,
    union header* real_address_p,
    union header* returned_address_p,
    size_t size_p );

    #endif



    Compiling with /W3 however produces no warning. I'm confused !!!
    What's wrong ?
    Please Help!

    Thanks!
     
    Tarique., Nov 11, 2009
    #3
  4. nicollete

    Seebs Guest

    On 2009-11-11, Tarique. <> wrote:
    > Ive got the union in file memroutines.c:


    Uh.

    How is any other module supposed to find that?

    Put type definitions in headers and include them everywhere.

    So, this:

    > union header {
    > struct {
    > union header* next;
    > union header* prev;
    > size_t size;
    > int h4byte1;
    > int h4byte2;
    > int h4byte3;
    > int h4byte4;
    > }s;
    > double dummy_align_var;
    > };


    would go in something like "header.h" (or see below), and then:

    > and the following function definition in TagTables.c:


    Somewhere before this function, you'd

    #include "header.h"

    > void add_tag(char* file_name_p,
    > unsigned int line_number_p,
    > union header* real_address_p, /*get the warning here*/
    > union header* returned_address_p,
    > size_t size_p );


    .... and you'd do the same thing in the file where you currently define
    the union, so it can find the same definition.

    > When I compile with /W4 on MS VC++ 08 ,I get a warning
    > 1>c:pathname\tagtable.h(19) : warning C4115: 'header' : named type
    > definition in parentheses


    Hmm.

    > The TagTable.h header file has the folloing content :


    Okay, this isn't going to affect you right now, but:

    In general, you should not rely on the ability to change case at
    will. If the file is named "tagtable.h" (which is what the compiler
    is reporting), you should not refer to it as TagTable.h -- while some
    systems let you do this, others might not.

    > #ifndef MEM_ROUTINES_H
    > #include "MemRoutines.h"
    > #endif


    Don't do the #ifndef here. Have MemRoutines.h do it, and just #include
    it unconditionally when you think you need it.

    Most likely, you just need to move the union header definition into
    MemRoutines.h.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Nov 11, 2009
    #4
  5. "Tarique." <> writes:
    <snip>
    > Ive got the union in file memroutines.c:
    >
    > union header {
    > struct {
    > union header* next;
    > union header* prev;
    > size_t size;
    > int h4byte1;
    > int h4byte2;
    > int h4byte3;
    > int h4byte4;
    > }s;
    > double dummy_align_var;
    > };
    >
    > and the following function definition in TagTables.c:
    >
    > void add_tag(char* file_name_p,
    > unsigned int line_number_p,
    > union header* real_address_p, /*get the warning here*/
    > union header* returned_address_p,
    > size_t size_p );
    >
    > When I compile with /W4 on MS VC++ 08 ,I get a warning
    > 1>c:pathname\tagtable.h(19) : warning C4115: 'header' : named type
    > definition in parentheses


    Seebs has given you a good run-down of the most common solution, but
    there is another. You don't have to put all of the declaration of
    'header' into TagTables.h. You can just add:

    union header;

    at file scope. This says to the compiler "header is a union tag --
    I'll say more about when it matters". Because you pass pointers to
    objects of this union type, you don't have to say anything else right
    now. The full declaration of the union can stay in the .c file that
    needs the details.

    Of course, this does not work if any other parts of the program need
    to know the details, but if you can do it, it is a good idea because
    it keep implementation details hidden. As result, you don't have to
    recompile the parts the use TagTables.h when/if the declaration of the
    header changes.

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Nov 11, 2009
    #5
  6. nicollete

    Seebs Guest

    On 2009-11-11, Ben Bacarisse <> wrote:
    > Seebs has given you a good run-down of the most common solution, but
    > there is another. You don't have to put all of the declaration of
    > 'header' into TagTables.h. You can just add:
    >
    > union header;
    >
    > at file scope. This says to the compiler "header is a union tag --
    > I'll say more about when it matters". Because you pass pointers to
    > objects of this union type, you don't have to say anything else right
    > now. The full declaration of the union can stay in the .c file that
    > needs the details.


    This is true, of course -- you and Scott both thought of it, and I didn't
    until later.

    That said, I have a sneaking suspicion that the function taking those
    pointers is likely to try to use them in some way, or need to know the
    size of what they point to.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Nov 11, 2009
    #6
  7. nicollete

    Tarique. Guest

    Ah that worked like a charm! Another important lesson learned :)
    Thank You Seebs,Scott & Ben.

    Appreciate all your comments.

    Thanks!

    ( PS: I've snipped the entire message I'm replying to . Hope that's
    Ok!)
     
    Tarique., Nov 11, 2009
    #7
    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. Benny Ng
    Replies:
    1
    Views:
    676
    Benny Ng
    Mar 30, 2006
  2. Matt
    Replies:
    10
    Views:
    634
    Chris Smith
    Apr 7, 2004
  3. Wei-Chao Hsu

    Store an address of object

    Wei-Chao Hsu, Nov 21, 2003, in forum: C++
    Replies:
    5
    Views:
    418
    Wei-Chao Hsu
    Nov 22, 2003
  4. Replies:
    12
    Views:
    809
    Daniel Pitts
    Aug 25, 2008
  5. Arkaitz Jimenez

    Modify pointed object method behaviour

    Arkaitz Jimenez, Jun 10, 2009, in forum: C++
    Replies:
    1
    Views:
    246
    James Kanze
    Jun 11, 2009
Loading...

Share This Page