LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2012, 09:38 PM   #1
mitchell7man
Member
 
Registered: Jan 2007
Location: Draper, UT
Distribution: Ubuntu, Windows 10, OSX
Posts: 461

Rep: Reputation: 31
Smile 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)
 
Old 12-08-2012, 01:42 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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).

Last edited by Wim Sturkenboom; 12-08-2012 at 01:44 AM.
 
Old 12-08-2012, 03:36 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
1) I don't know what the teacher wants, but use 'sizeof' to determine the sizes of various types instead of assuming.
 
Old 12-08-2012, 10:14 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by H_TeXMeX_H View Post
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 View Post
(Assume char’s are 1 byte, int’s and pointers are 2 bytes, and floats are 4 bytes)
 
Old 12-08-2012, 11:43 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Wim Sturkenboom View Post
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 View Post
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 View Post
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.

Last edited by johnsfine; 12-08-2012 at 11:49 AM.
 
Old 12-08-2012, 05:40 PM   #6
mitchell7man
Member
 
Registered: Jan 2007
Location: Draper, UT
Distribution: Ubuntu, Windows 10, OSX
Posts: 461

Original Poster
Rep: Reputation: 31
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.
 
Old 12-08-2012, 07:12 PM   #7
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
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.)
 
Old 12-10-2012, 02:19 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 12-12-2012, 01:14 AM   #9
mitchell7man
Member
 
Registered: Jan 2007
Location: Draper, UT
Distribution: Ubuntu, Windows 10, OSX
Posts: 461

Original Poster
Rep: Reputation: 31
Thanks so much everyone! I'll have to keep using c and keep it fresh and growing!
 
Old 12-12-2012, 03:17 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by mitchell7man View Post
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.
 
  


Reply

Tags
awesome, clanguage, cprogramming, programing, questions



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
2 programming questions hk_michael Programming 4 02-01-2005 12:02 AM
programming questions eboats Programming 4 10-17-2001 02:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration