473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Hi Please explain this concept related to structure

Hi All

I read in book that mentioning the structure name gives the whole structure itself and not its base address? What is meant by that?

I wrote a small program like this

struct a
{
int a;
int b;
} *obj, obj1;

main()
{
obj=&obj1;
printf(”Address is %pn”, obj);
}

Output

Address is 80608bc

I guess what I got is base address of the structure. But I am not sure.

We would also read that if we know the address of one member of the structure we can get the base address of the structure. I am really confused by this.

Explanation please…

Thanks & Regards
Sathish Kumar
Sep 28 '07 #1
8 1315
obj is a structure pointer (struct a *) and obj1 is a plain struct (struct a).

When passing obj1 as argument to a function, the structure is entirely duplicated in stack and changes made to it will not be saved in calling function.

Passing the pointer is a gain of memory and time, and permits direct modification of pointed structure.
Sep 28 '07 #2
This code :

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. struct a
  4. {
  5.    int a;
  6.    int b;
  7. } *obj, obj1;
  8.  
  9. void change_struct(struct a mystruct)
  10. {
  11.    mystruct.a = 3;
  12.    mystruct.b = 4;
  13. }
  14.  
  15. void change_struct_pointer(struct a *pmystruct)
  16. {
  17.    pmystruct->a = 5;
  18.    pmystruct->b = 6;
  19. }
  20.  
  21. int main()
  22. {
  23.    obj1.a = 1;
  24.    obj1.b = 2;
  25.    printf("a = %d, b = %d\n", obj1.a, obj1.b);
  26.  
  27.    change_struct(obj1);
  28.    printf("a = %d, b = %d\n", obj1.a, obj1.b);
  29.  
  30.    obj = &obj1;
  31.    change_struct_pointer(obj);
  32.    printf("a = %d, b = %d\n", obj1.a, obj1.b);
  33.  
  34.    return 0;
  35. }
produces following output :

Expand|Select|Wrap|Line Numbers
  1. a = 1, b = 2
  2. a = 1, b = 2
  3. a = 5, b = 6
Sep 28 '07 #3
Hi


What you have said is correct? But I am sorry. I think your answer is not related to my question.


Thanks & Regards
Sathish Kumar


obj is a structure pointer (struct a *) and obj1 is a plain struct (struct a).

When passing obj1 as argument to a function, the structure is entirely duplicated in stack and changes made to it will not be saved in calling function.

Passing the pointer is a gain of memory and time, and permits direct modification of pointed structure.
Sep 28 '07 #4
Hi

Sorry actually I meant to say that your answer is not sufficient for my 2nd point. i.e. If we know the address of one member of structure we can get the base of the address of the structure. Please explain this concept.

Actually I have come across the macro which is used to find the base address of the structure. If you explain it will be very useful

Thanks & Regards
Sathish Kumar

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. struct a
  4. {
  5.    int a;
  6.    int b;
  7. } *obj, obj1;
  8.  
  9. void change_struct(struct a mystruct)
  10. {
  11.    mystruct.a = 3;
  12.    mystruct.b = 4;
  13. }
  14.  
  15. void change_struct_pointer(struct a *pmystruct)
  16. {
  17.    pmystruct->a = 5;
  18.    pmystruct->b = 6;
  19. }
  20.  
  21. int main()
  22. {
  23.    obj1.a = 1;
  24.    obj1.b = 2;
  25.    printf("a = %d, b = %d\n", obj1.a, obj1.b);
  26.  
  27.    change_struct(obj1);
  28.    printf("a = %d, b = %d\n", obj1.a, obj1.b);
  29.  
  30.    obj = &obj1;
  31.    change_struct_pointer(obj);
  32.    printf("a = %d, b = %d\n", obj1.a, obj1.b);
  33.  
  34.    return 0;
  35. }
produces following output :

Expand|Select|Wrap|Line Numbers
  1. a = 1, b = 2
  2. a = 1, b = 2
  3. a = 5, b = 6
[/quote]
Sep 28 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
struct a
{
int a;
int b;
} *obj, obj1;

main()
{
obj=&obj1;
printf(”Address is %pn”, obj);
}

Output

Address is 80608bc

I guess what I got is base address of the structure. But I am not sure.

We would also read that if we know the address of one member of the structure we can get the base address of the structure. I am really confused by this.

Explanation please…
OK here goes:

When a struct variable is created, the members are aligned. That is, the compiler sets the position of the various members along word boundaries. A word, in this case, is the processor register size. So, a 32-but processor has a 32-bit word. That means, it is convenient if the variables are set on 32-bit boundaries. Usually, the compiler sees this as the sizeof(int).

This struct:
Expand|Select|Wrap|Line Numbers
  1. struct Test
  2. {
  3.     int a:
  4.     char b;
  5.     int c;
  6. };
  7.  
will have the int a on a 32-bit boundary, char b on a 32-bit boudary and int c on a 32-bit boundary. The char is only one byte. Therefore, 3 slack bytes (sometimes call pad bytes) will be added after char b.

Therefore, the sizeof(Test) is not the same as sizeof(Test.a) + sizeof(Test.b) + sizeof(Test.c).

Therefore, if you have the address of a member variable it may not be as easy as you think to deduce the address of the struct variable itself. This is further complicated in C++ when hidden addresses are tuck away in the struct (or class) variable to support object-oriented programming.
Sep 28 '07 #6
If you know the address of one of structure's members and know which member it is, you can find out base address by substracting sizes of previous members. This works for packed structure, otherwise you have to take care about memory alignment issues.
Sep 28 '07 #7
Hi

Thanks for your explanation. I clearly understood the concept.


1) But I dont understand why we should have such a approach?

2) Can you give me an example why we should go for this or under which circumstance we will need this?

Because I am able to get the base address of the structure by writing this simple code

struct a
{
int a;
int b;
} *obj, obj1;

main()
{
obj=&obj1;
printf(”Address is %pn”, obj);
}

Output

Address is 80608bc


Please explain....


Thanks & Regards
Sathish Kumar


OK here goes:

When a struct variable is created, the members are aligned. That is, the compiler sets the position of the various members along word boundaries. A word, in this case, is the processor register size. So, a 32-but processor has a 32-bit word. That means, it is convenient if the variables are set on 32-bit boundaries. Usually, the compiler sees this as the sizeof(int).

This struct:
Expand|Select|Wrap|Line Numbers
  1. struct Test
  2. {
  3.     int a:
  4.     char b;
  5.     int c;
  6. };
  7.  
will have the int a on a 32-bit boundary, char b on a 32-bit boudary and int c on a 32-bit boundary. The char is only one byte. Therefore, 3 slack bytes (sometimes call pad bytes) will be added after char b.

Therefore, the sizeof(Test) is not the same as sizeof(Test.a) + sizeof(Test.b) + sizeof(Test.c).

Therefore, if you have the address of a member variable it may not be as easy as you think to deduce the address of the struct variable itself. This is further complicated in C++ when hidden addresses are tuck away in the struct (or class) variable to support object-oriented programming.
Oct 4 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
1) But I dont understand why we should have such a approach?

2) Can you give me an example why we should go for this or under which circumstance we will need this?

Because I am able to get the base address of the structure by writing this simple code

struct a
{
int a;
int b;
} *obj, obj1;

main()
{
obj=&obj1;
printf(”Address is %pn”, obj);
}
Alignment is used to speed up access to the variables. If a variable si split over two int boundaries, it will take two operations instead of one to access the variable plus you now have additional time spent working with the value in two pieces.

Your code above uses &obj1 to get the address of the struct variable. This will always work. But that was not your original question which was how to get the address of the struct variable when all you have is the address of a member. This you cannot reliably do. In fact, C++ adds hidden addresses inside the struct variable that are not declared in the struct.
Oct 4 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
1
by: googleo | last post by:
Hi, in my application I want to handle and store data in a hierarchic data structure. For example: persons who manage houses; houses have various numbers of floors; floors have various numbers...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
27
by: postrishi | last post by:
Hello Folks, I am doing my engineering in computer science. I basically want to work in systems rather than in application. But the main problem with me that i dont have adequate guidance...
1
by: | last post by:
Hi. This is a a semi-newbie question about how to store arbitrary information about my apps such that I can code quickly, mimizing complexity and the number of things I have to hold in my brain. I...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.