in Programming in C
966 views
2 votes
2 votes

Can someone explain the output of this code? and what (char*) is doing actually?

#include<stdio.h>
struct Ournode{
    char x, y, z;
};
int main() {
    struct Ournode p={'1', '0', 'a'+2};
    struct Ournode *q=&p;
    printf("%c, %c", '*((char*)q+1)', '*((char*)q+2)');
    return 0;
}
in Programming in C
by
966 views

5 Comments

why garbage @satbir ?

it should be print character , 0.

How ?

let p is created at base location 164,

then at location 164 character 1 is present

then at location 165 character 0 is present

then at location 166 character 'a'+2 = 'c' is present

q → contains the address of the structure block. ===> pointing 164 as structure pointer

(char*)q → typecasting the address into char type. ===> pointing 164 but as character pointer

(char*)q+1 → incrementing the char value by 1 char size (since now it is typecasted to a char). ===> pointing 165 as structure pointer

*((char*)q+1) → printing the value present at the pointing by the location 165. ===> character 0 printed !
1
1
In case of typecasting, if size of Byte changes, that will not affect the program?

Suppose, I mean int 4B and char 1B. And u typecasting from int to char or char to int

Will there be any problem in typecasting?
0
0
reshown by

@Shaik Masthan

I think you missed the ' ' in

'*((char*)q+1)'

Also, when i executed the program i am getting ),) as the answer.

0
0

@srestha

here we are typecasting an address not a value.

when q was integer and say an int takes 4 bytes so q was incrementing like  0->4->8->12.... where 0,4,8,12 are address locations.

when q was typecasted and made char and say a char takes 1 bytes so q will incrementing like  0->1->2->3.... where 0,1,2,3 are address locations.

So typecasting will definitely effect the program. Typecasting may lead to data loss.

for eg:- if 3.134 is converted into int then it becomes 3.

In the above program if q was not typecasted then q + 1 would have pointed to 164+3 =167 since q was a structure pointer.

1
1
@sresta, mam... Increment should be depend upon which data type it is working...

if it is belongs to int data type, then increment is 4B

if it is belongs to char data type, then increment is 1B

if it is belongs to struct data type, then increment is size of struct...

in this question, they type casted into char pointer...
0
0

2 Answers

1 vote
1 vote
printf("%c, %c", '*((char*)q+1)', '*((char*)q+2)');

In this line '*((char*)q+1)' is a constant since it is marked in ' ' and since %c is used it will only take the last character from the constant and print it.

So output is coming ),).

This line has no link to the structure part or pointer concept given  in the program.

For eg :-

printf("%c", '*satbir');

This will give r as output.


What is char* doing ?

(char*)q → typecasting the address into char type.

edited by

4 Comments

https://gateoverflow.in/226441/switch

read the whole discussion between me and arjun sir

infact that is the first and last downvote i got till now :)

0
0
memorization with -ve vote :P  ;)
1
1

@Shaik Masthan

thanks , yes undefined behaviour will be ans.

0
0
0 votes
0 votes
Cosidering ' ' in '*((char*)q+1)' and '*((char*)q+2)') :: the output will be ),)

because the whole content written inside single qoute is treated as character constant and the last character is printed.

If taking single quotes as mistake then the output will be 0,c

because q is pointing the first index of p i.e,pointing to '1' and thus q+1 will point to'0' and q+2 will point to 'a'+2 which is equal to 'c'

thus by typecasting,output will be 0,c

Related questions