in Programming in C retagged by
5,621 views
5 votes
5 votes

Consider the following declaration :

structaddr {
    char city[10];
    char street[30];
    int pin;
};
struct {
    char name[30];
    int gender;
    struct addr locate;
} person, *kd = &person;

Then *$(kd->name + 2)$ can be used instead of:

  1. $person.name+2$
  2. $kd-> (name+2)$
  3. $*((*kd).name+2)$
  4. either $A)$ or $B)$ , not $C)$
in Programming in C retagged by
by
5.6k views

3 Comments

Is this syntax correct?

*(kd->name + 2)

Isn't it kd->name + 2?

0
0
Its correct according to me using pointer arithmetic.
0
0
edited by

Structs:

0
0

3 Answers

10 votes
10 votes
Best answer
Option C is correct. A and B points to the address of the location whereas we require to access the value at that location, which is the third character of name.
edited by

4 Comments

ok, so you are telling that while printing I should assume to print chars(%c) as per question rather than string(%s)?

In that way C) is correct.
0
0
edited by

Yes, they have specifically asked about a value stored at that location of the string by using "*" outside the brackets. Otherwise, option A and B would be correct. Your code works fine if we use %c or %d(for ASCII value) for *(kd->name + 2)

0
0
Thanks, I got it.
0
0
3 votes
3 votes

-> belongs to postfix production which has higher precedence than +. So, *(kd->name+2) is equivalent to *((*kd).name+2) which is (*kd).name[2]. Ans = (C)

0 votes
0 votes

None of the given options is correct. If we observe carefully the first line is like structaddr {. There is no space between struct and addr. So it will give following error:

main.c: In function 'main':
main.c:12:5: error: 'structaddr' undeclared (first use in this function)
     structaddr {
     ^

Had there been space between struct and addr, one of the given options would have been correct. But since in question paper, there is no space between struct and addr in first line. So the given code will give error and hence none of the options is matching (or you can say all options are matching because with all options, same error will be there). So in ISRO marks should be given to all or this question should be removed from final evaluation.

2 Comments

I think they will not consider this fact...
0
0
Its mistake on their end. So they should. I would request you to raise objection so that they consider this as a mistake. Such typos are there in other questions also. Please raise objections wherever there is mistake without thinking they will consider it or not.
0
0
Answer:

Related questions