LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C Programming Questions (https://www.linuxquestions.org/questions/programming-9/c-programming-questions-4175440522/)

mitchell7man 12-07-2012 09:38 PM

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
{
  float f;
  char c[6];
  int i;
} x;

struct
{
  float f;
  char c[6];
  int i;
} y;
(Assume char’s are 1 byte, int’s and pointers are 2 bytes, and floats are 4 bytes)

a)How many bytes would be allocated in memory for the union variable x above? (bytes)
b)How many bytes would be allocated in memory for the struct varaible y above?(bytes)

_______________________________________________
2-
Code:

int a[10];
Write the equivalent pointer arithmetic expression for the following array expressions. Omit spaces.
a) a[1] is the same as:
b) &a[3] is the same as
________________________________________________
3-
Code:

struct person
{
    int age;
    //below, add the C-code to declare dad and mom to point to struct person records
   
(a)(answer here) dad, mom;
} nate, ben, sue;

a) ? I tried
Code:

struct person
b) // C-code to set Nate's dad's age to 50 - I tried:
Code:

nate.dad.age=50 and ben.age=50
________________________________________________
4-
Code:

char pet[] = "orangutan";
a) The value of sizeof(pet) is -- I've tried 72(bits) and 9(bytes)

Wim Sturkenboom 12-08-2012 01:42 AM

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

H_TeXMeX_H 12-08-2012 03:36 AM

1) I don't know what the teacher wants, but use 'sizeof' to determine the sizes of various types instead of assuming.

ntubski 12-08-2012 10:14 AM

Quote:

Originally Posted by H_TeXMeX_H (Post 4845031)
1) I don't know what the teacher wants, but use 'sizeof' to determine the sizes of various types instead of assuming.

The question clearly states the sizes of primitive type that should be assumed to find the size of compound types; if you use sizeof you'll almost certainly get the wrong answer because you're system probably uses different sizes:
Quote:

Originally Posted by mitchell7man (Post 4844938)
(Assume char’s are 1 byte, int’s and pointers are 2 bytes, and floats are 4 bytes)


johnsfine 12-08-2012 11:43 AM

Quote:

Originally Posted by Wim Sturkenboom (Post 4845003)
In a struct, each member has its own memory space; therefore the size is the sum of the size of the members.

Unless alignment requirements adds some extra causing the size of the struct to exceed the sum of the sizes of the members.

In the OP's example, alignment didn't add anything. But as a general rule, you need to pay attention to alignment.

Quote:

Be aware that the result depends on the architecture of your machine (16 bits/32 bits/...)
Except that the problem specifies the sizes of the basic types.

Quote:

Originally Posted by mitchell7man (Post 4844938)
I've got my way though a great majority of a homework assignment

You should try 1 and 2 before asking for help on them. They aren't difficult.
Quote:

a) ? I tried
Code:

struct person

Close, but that declares a struct embedded inside the struct (impossible when they are the same type). You need a pointer to a struct embedded.

Quote:

Originally Posted by mitchell7man (Post 4844938)
b) // C-code to set Nate's dad's age to 50 - I tried:
Code:

nate.dad.age=50 and ben.age=50
_

Same problem. dad needs to be a pointer not a directly contained object. That changes the syntax for reaching members of dad.

mitchell7man 12-08-2012 05:40 PM

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.

sarin 12-08-2012 07:12 PM

Quote:

So, for number 4, I just needed to add one byte, so that was excellent!
----I'm having trouble getting it right. So for the union it would be 4 bytes? and the structure would be 7 bytes?
For the first question as pointed above by another poster, you need to look for the size of the largest member in union and add size of each members in case of struct. So, for union, since you have "char c[6];" that is the largest member, making union size 6. The size of struct is 4+6+2=12.
(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:

-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?
* de-references the pointer. Assume that your array is stored at location 100 in memory and say it is initialized to values 11,12...20.
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:

-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.
You just need to understand this question better. I do not know how to help you in understanding it. But, the expected answer is "struct person *dad,*mom;"
Quote:

----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.
nate.dad->age=50;
(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.)

theNbomr 12-10-2012 02:19 PM

Re: Q3
Code:

struct person {
    int age;
};

// parent is Pointer to a thing that is a 'struct person'
// One of these can also be a member of any struct.
struct person * parent;

That should give you enough to find your way.

--- rod.

mitchell7man 12-12-2012 01:14 AM

Thanks so much everyone! I'll have to keep using c and keep it fresh and growing!

Sergei Steshenko 12-12-2012 03:17 AM

Quote:

Originally Posted by mitchell7man (Post 4847513)
Thanks so much everyone! I'll have to keep using c and keep it fresh and growing!

I suggest to read C99 standard: http://www.open-std.org/JTC1/SC22/WG...docs/n1256.pdf . It is surprisingly user friendly.

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.