address of attribute of struct

Hello everyone!

I need to solve next question.
I have a struct such:

1
2
3
4
typedef struct {
  char one;
  char two;
}


How can i get the address of attribute?
attribute? one and two?

Edit:

1
2
3
4
5
6
7
8
9
10
11
12
typedef struct {

char one;
char two;
} MyStruct;

void main()
{
MyStruct S1;

char *ptrAddress = &S1.one;
}


Is this what you want?
Last edited on
closed account (zb0S216C)
Well, there're two ways you can obtain the address of a member:
1) Create a member function that returns a pointer to a specified member.
2) Create a pointer internally or externally that points to a specified member.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
typedef struct {
    char one; 
    char two;

    char *Get_address_one( void ) 
    {
        return &one;
    }

    char *Get_address_two( void ) 
    {
        return &two;
    }
} MyStruct;

int main( )
{ 
    MyStruct Struct;

    // Or use a pointer externally....
    char *Member_pointer( &Struct.one );

    return 0;
}
Last edited on
OMG! I'm silly... and i need a relax.

I forgot to include <string.h> and that is why compiler showed me an error with function memcpy().

1
2
char * test_p = malloc(sizeof(char)*3); // an error  was here
memcpy(test_p, &S1.one, 1);

And that is why i could not understand what is wrong with address of attribute.

Thanks, writetonsharma.
To Framework

Thanks, for explaining, but i'm using C language, that is why internal way by the function is not possible.
closed account (zb0S216C)
but i'm using C language

In that case, a function external to MyStruct could still return the address of a member within the passed instance of MyStruct.

In C, however, the keyword struct is required before creating an instance of MyStruct.

P.S: I never knew you were using C.
Topic archived. No new replies allowed.