C Programming Questions
Hi Everyone, I've got my way though a great majority of a homework assignment, but am stuck on a few problems, if your up to it please help me answer and more importantly understand the following. You are all the best.
1- Code:
union b)How many bytes would be allocated in memory for the struct varaible y above?(bytes) _______________________________________________ 2- Code:
int a[10]; a) a[1] is the same as: b) &a[3] is the same as ________________________________________________ 3- Code:
struct person Code:
struct person Code:
nate.dad.age=50 and ben.age=50 4- Code:
char pet[] = "orangutan"; |
1)
In a union, the members share the memory space; therefore the size of a union will be the size of the biggest member. In a struct, each member has its own memory space; therefore the size is the sum of the size of the members. Be aware that the result depends on the architecture of your machine (16 bits/32 bits/...) 2) Pointer arithmetic is quite intelligent; if you have an array of integers, incrementing the pointer by one will point to the next element (and not simply one byte further). Also, remember that &a[0] means the address of the first element of array 'a' and is therefore the same as the address of 'a' 3) Not quite clear 4) Remember that strings in C are null terminated and therefore one additional character is / needs to be allocated. So the sizeof 'pet' is 9 plus 1 character (while the strlen will be 9 characters). |
1) I don't know what the teacher wants, but use 'sizeof' to determine the sizes of various types instead of assuming.
|
Quote:
Quote:
|
Quote:
In the OP's example, alignment didn't add anything. But as a general rule, you need to pay attention to alignment. Quote:
Quote:
Quote:
Quote:
|
Hi,
So, for number 4, I just needed to add one byte, so that was excellent! -For Q1 ----I'm having trouble getting it right. So for the union it would be 4 bytes? and the structure would be 7 bytes? -For Q2 ----a is *(a+1) so I got that! for b I tried *(a+3), but that is incorrect. What exactly is the difference? -For Q3 ----For A i tried struct *person, i get that its a pointer, but to what? I would think it would be structure *dad *mom, but that doesn't seem to be an option. ----For B I tried to define as a pointer, like this nate.*dad.age=50 but thats not right, I've googled around but haven't found anything on the specific subject. Thanks for your help everyone, I think I understand most of the ideas but am having a hard time knowing how to express it in correct code. |
Quote:
(Ignore next point if you think it is confusing) Usually in a normal x86 machine, I get the sizes as 8 and 12. That is because of padding. However, in your case, since the pointer and int sizes are 2 and since both 6 and 12 are perfectly divisible by 2, you don't have to bother much about it. Look at this link for quick info on padding: http://stackoverflow.com/questions/4...ucture-packing Quote:
Then *a will have a value 11, *(a+1) will have a value 12 etc. Where as, a will be 100 and a+3 will have a value 112 (Assuming the size of int is 4). &a[3] will give you the address of 4th member. So, the answer is (a+3) Quote:
Quote:
(I look at it like this, nate is not a pointer. So, using . to reference its members is fine. But, dad is a pointer, so, I need to use -> to reference its members.) |
Re: Q3
Code:
struct person { --- rod. |
Thanks so much everyone! I'll have to keep using c and keep it fresh and growing!
|
Quote:
The fundamental issues are: you either rely on gurus/"gurus", and/but as humans they can err; or you become a guru yourself by reading and understanding the ultimate source of info. It's not that I remember the standard by heart, but since I've read it and since it's a searchable (seekable ?) document, I can pretty easily find the needed info in it when there are issues to clarify. |
All times are GMT -5. The time now is 07:34 AM. |