Homework Help Question & Answers

Give the minimum size of each of the following C data structures, assuming that char values...


5. Give the minimum size of each of the following C data structures, assuming that char values occupy on byte, int and float values occupy four bytes, double values occupy eight bytes, and pointers occupy fou bytes. (a) (b) (c) char str[]= Curly. double *a[41[4]; char *str[3] = {Moe, Larry, Curly}; (Include the space occupied by the string literals in your answer.) (d union int a; char b float cl4]; u; (e) struct int a char b float cl4]; S, (f union int a[3]; double b; struct t float c char d[4] S, (g) struct float a union double bl2]; int c char d; S,
Give the minimum size of each of the following C data structures, assuming that char values occupy byte, int and float values occupy four bytes, double values occupy eight bytes, and pointers occupy bytes. (a) char str[] = "Curly": (b) double *a[4][4]: (c) char *str[3] = {"Moe", "Larry", "Curly"}: (Include the space occupied by the string literals in your answer.) (d) union { int a: char b: float c[4]: }u: (e) struct { int a: char b: float c[4]: }s: (f) union { int a[3]: double b: struct { float c: char d[4]: }s: }u: (g) struct { float a: union { double b[2]: int c: }u: char d: }s;
0 0
Add a comment
Answer #1

In c there is a method sizeof which helps you calculate the number of bytes.

eg :

#include <stdio.h>

int main()
{
char str[] = "Curly";
printf("Size of %d bytes\n",sizeof(str));

return 0;
}

Output :

6 bytes.

a) char str[] = "Curly";

Assuming char as 1 byte : size is 6 bytes

b) double *a[4][4];

size of double is 8

size of a is 16*8 = 128 bytes

c) char *str[3] = {"Moe", "Larry", "Curly"};

(Include the space occupied by the string literals in your answer.)

char * str occupies 8 bytes.

str[3]

8*3 = 24 bytes.

D) union {

int a;

char b;

float c[4];

} u;

In union, considers only the datatype with highest number of bytes : sizeof c is 4*4 = 16 bytes.

e) struct {

int a;

char b;

float c[4];

} s;


sum of all variables : 4+1+ (4*4) = 21 bytes.

f) union {

int a[3];

double b;

struct {

float c;

char d[4];

} s;

} u;


In the above example the total size of u is the size of u.s (which happens to be the sum of the sizes of u.s.u and u.s.d), since s is larger than both i and f. When assigning something to u.i, some parts of u.f may be preserved if u.i is smaller than u.f.

Reading from a union member is not the same as casting since the value of the member is not converted, but merely read.

Total size = 16 bytes

g) struct {

float a;

union {

double b[2];

int c;

} u;

char d;

} s;


Total size is 32 bytes.

Add a comment
Know the answer?
Add Answer to:
Give the minimum size of each of the following C data structures, assuming that char values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coin

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Give the minimum size of each of the following C++ types, assuming that char values occupy one...

    2. Give the minimum size of each of the following C++ types, assuming that char values occupy one byte, short values occupy two bytes, int and float values occupy four bytes, double and long values occupy eight bytes, and pointers occupy four bytes. a) union u type ( double a[3]; int *b; char c[10] b) struct s1type float *d [2]; long e[4] char f[6] short *gi c) struct s2 type ( s1_type s; u type u [2]; int *h[3]; short...

  • C language not C++ 1. Write the statements to do the following: (2 pts) a. Define...

    C language not C++ 1. Write the statements to do the following: (2 pts) a. Define a struct with member variables width, height, topleft x, topleft y all floats). Use a tag to call it Rectangle. b. Declare a variable struct type Rectangle 2. Consider the following variables: struct int x; float y; char zi var1; union nt x; float y; char[20] z;) var2 f float and int are stored using 4 bytes each, what is the size (in bytes)...

  • Problem 4. (20 points) Consider the following C structs: struct S1 { char a; char b;...

    Problem 4. (20 points) Consider the following C structs: struct S1 { char a; char b; char c; } struct S2 { S1 d; S1 e; S1 *f; short g; char h; double i; int j; } A. Show how the S1 struct would be in memory on a 64-bit machine. Label the bytes that belong to the various fields with their names and clearly mark the end of the struct. Use the character 'X' to indicate bytes that are...

  • S. Modułus operator can operate upon A. double B、 float C. char D. int E. C...

    S. Modułus operator can operate upon A. double B、 float C. char D. int E. C and D

  • Provide the definition for each of the following structures and unions:

    This is from chapter 10 in c how to program deitel.a) Structure inventory containing character array partName [ 30 ], integer partNumber, floating point price, integer stock and integer reorder.b) Union data containing char c, short s, long b, float f and double d.c) A structure called address that contains character arrays streetAddress[ 25 ], city[ 20 ], state[ 3 ] and zipCode[ 6 ].d) Structure student that contains arrays firstName[ 15 ] and lastName[ 15 ] variable homeAddress of...

  • Find the error(s) in each of the following statements: This is in C

    a) Assume: char str [ 5 ];Scanf( “%s”, str ); /* User types hello */b) Assume: int a[ 3 ];Printf( “$d %d %dn”, a[ 1 ], a[ 2 ], a[ 3 ] );c) Doublef[ 3 ] = { 1, 1, 10.01, 100.001, 1000.001 };d) Assume: double d[ 2 ] [ 10 ];d[ 1, 9 ] = 2.345;

  • QUESTION 1 In order to print the members of structures in the array, what will be...

    QUESTION 1 In order to print the members of structures in the array, what will be the contents of blanks in the printf statement of the code snippet given below? #include <stdio.h> struct virus {    char signature[25];       char status[20]; } v[2] = {                               "Yankee Doodle", "Deadly",                               "Dark Avenger", "Killer"                   } ; void main( ) {       for (int i = 0; i < 2; i++)                   printf( "\n%s %s", _______________ , _____________ ); } A. signature, status B. v.signature,...

  • State true or false: (6 x 2 = 12 pts) Using -- with a reverse iterator...

    State true or false: (6 x 2 = 12 pts) Using -- with a reverse iterator moves it from the back toward the front of a list b) The body of a do-while loop is always executed at least once. c) If p points to the array element a[j], then p + i points to a[i+j]. d) Any number of pointer variables may point to the same object. In terms of both execution speed and memory usage usually iterative solutions...

  • In C# (sharp) Assuming a string variable named movie Title has already been declared, which one...

    In C# (sharp) Assuming a string variable named movie Title has already been declared, which one of the following statements combines the strings "Hidden" and "Figures" and then assigns the resulting string to the variable? 1. movieTitle="Hidden" + "Figures"; 2. "Hidden" +"Figures" - moveTitle; 3. movie Title("Hidden" +"Figures"); 4. movie title="Hidden" & "Figures": Look at the following code: int distance; // Declare distance as an int double half://Declare half as a double distance = 35; // Assign 35 to distance...

  • The variable cp_arr has been declared as an array of 26 pointers to char

    The variable cp_arr has been declared as an array of 26 pointers to char. Allocate 26 character values, initialized to the letters 'A' through 'Z' and assign their pointers to the elements of cp_arr (in that order). int *arr1[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; arr1=cp_arr[0]; for(int i = 0; i < 26; i++) cout << cp_arr[i]; is the newest, char a ='A', b = 'B', c = 'C', d='D', e='E', f='F', g='G', h='H', i='I', j='J', k='K',l='L', m='M', n='N', o='O', p='P', q='Q', r='R', s='S', t='T', u='U',...

Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.