Question

3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the...

3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the answer without actually compiling or running the program. Which is true about the following codes? If you choose a, you need to specify where the syntax error(s) occur; if you choose b, you need to specify what error occurs when you run it; if you choose c, you need to specify the outputs of the program.

#include <stdio.h>

#include <string.h>

struct Books //define the struct Books

{

   char title[50];

   char author[50];

   char subject[100];

   int   book_id;

} Book1;

struct Books book;

void DealBooks(struct Books b1, struct Books *p2)

{

         b1.book_id++; p2->book_id++;

         printf("%d\n",b1.book_id);

printf("%d\n",p2->book_id);

printf("%s\n",b1.title);

printf("%s\n",p2.title);

}

int main(int argc, char *argv[])

{

         strcpy(book.title,"C Programming");

         strcpy(book.author,"Nuha Ali");

         strcpy(book.subject,"C Programming Tutorial");

         book.book_id=100;

        

         strcpy(Book1.title,"Telecom Billing");

         strcpy(Book1.author,"Zara Ali");

         strcpy(Book1.subject,"Telecom Billing Tutorial");

         Book1.book_id=700;

        

         DealBooks(book,&Book1);

         printf("%d\n",book.book_id);

printf("%d\n",Book1.book_id);

         return 0;

}//end of main

  1. Compile error    b. Compiles OK, runtime error   c. Compiles and runs OK

4. (12 pts; additional 4 pts of extra credit for p2+3) This exercise further tests your knowledge of pointer arithmetic. Suppose we have three variables declared as

char *pc;

int *pi;

struct point {double x; double y;};

struct point *p1;

struct point *p2[10];

Assume sizeof(char)=1, sizeof(int)=4, sizeof(double)=8. The values of pc, pi, p1 and p2 are 240, 258, 410 and 480 respectively. What are the values of pc+1, pi+2, p1+4 (12 pts) and p2+3 (4 pts extra credit)?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

3).The right option is option a).Compile error.

The given C program will never compile because of syntax errors.

Syntax error:-

In the function DealBooks(struct Books b1, struct Books *p2) the last statement which is printf("%s\n",p2.title) has syntax error because when we access the value inside the structure with structure variable then we use '.' (dot operator) and when we access the value inside the structure with structure pointer then we need to use '->' operator but here , we are trying to access the content of structure using pointer via dot operator not '->' operator . due to this , the given code will never compile and will give immediate syntax error.

4).pc = 240, pi = 258 , p1 = 410, p2 = 480

all these are the addresses.

suppose 'p' is any pointer of type 'd' , p has value say 'a' and sizeof(d) = x

then the value of p + 1 = value of p + 1*(sizeof(d))

so , in general p + y = value of p + y*(sizeof(d))

here, pc + 1 = value of pc + sizeof(char) = 240 + 1 = 241

pi + 2 = value of pi + 2*(sizeof(int)) = 258 + 2*4 = 258 + 8 = 266

sizeof(point) = 2*sizeof(double) = 2*8 = 16

p1 + 4 = value of p1 + 4*(sizeof(point)) = 410 + 4*16 = 410 + 64 = 474

p2 + 3 = value of p2 + 3*(sizeof(point)) = 480 + 3 * 16 = 480 + 48 = 528

Add a comment
Know the answer?
Add Answer to:
3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the...
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
  • Can you help with this C programming question. I have provided the skeleton code below along...

    Can you help with this C programming question. I have provided the skeleton code below along with the Stack/Data/Process Class for you to see/reference. Along with the Stack/Data type definition.   **SKELTON CODE** #include #include #include Stack* concat_stack(Stack *s1, Stack *s2) { //your code here return NULL; } **STACK CLASS FOR YOU TO REFERENCE** #include #include #include #include Stack* create_stack(int stack_capacity) { Stack *s = (Stack*) malloc(sizeof(Stack)); if (stack_capacity < 1) { fprintf(stderr, "Error(create_stack): invalid capacity, set to 10\n"); s->capacity =...

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