Question

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,
0 0
Add a comment Improve this question Transcribed image text
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 Coins

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

  • 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

  • QUESTION 10 What will be the output of following code snippet? char *str; str = "%s";...

    QUESTION 10 What will be the output of following code snippet? char *str; str = "%s"; printf(str, "S"); A. S B. Garbage Value C. Compile-time Error D. Run-time Error 4 points    QUESTION 11 What will be the output of the following statements assuming that the array begins at the memory address location 7002 and size of an integer is 4 bytes? int a[3][4] = { 1, 2, 3, 4,                     5, 6, 7, 8,                     9, 10, 11, 12 }; printf("%d,...

  • Prints out the size of (there’s a hint, by the way) variables with the following C...

    Prints out the size of (there’s a hint, by the way) variables with the following C data types – int, long, unsigned, long long, char, float and double. This is how many bytes of memory a variable of that type occupies on this machine, using your chosen compiler. (Xcode) Answer the following question: Does adding the static keyword to the declaration of these variables in your program change their size? Answer the following question: What is the largest number and...

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

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • Consider the following methods’ headers: public static int one(int a, char b, double c, String d)...

    Consider the following methods’ headers: public static int one(int a, char b, double c, String d) public static double two(double x, double y) public static char three(int r, int s, char t, double u) Answer the following questions: Q1) What is the signature of method one? Answer: Q2) What is the return type of method two? Answer: Q3) What the formal parameters of method three? Answer: Q4) How many actual parameters are needed to call the method three? Answer: Q5)...

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