Store Address of object pointed to

N

nicollete

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!
 
S

Seebs

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
 
T

Tarique.

...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!
 
S

Seebs

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
 
B

Ben Bacarisse

Tarique. said:
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>
 
S

Seebs

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
 
T

Tarique.

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!)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top