The Gateway to Computer Science Excellence
+2 votes

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

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 by | 210 views
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 !
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?

@Shaik Masthan

I think you missed the ' ' in


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



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.

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

2 Answers

+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
0 and c

Here where r u getting '(' as last character??

Sorry my mistake . it should be ')'.

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

i hope it should be a mistake that, keeping inverted commas....

if it is there... Then this question belongs to undefined behaviour
I checked it with different inputs in different IDEs. all are giving same answer.
I don't know...may be there could be corner case where it may fail.

read the whole discussion between me and arjun sir

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

memorization with -ve vote :P  ;)

@Shaik Masthan

thanks , yes undefined behaviour will be ans.

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

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
52,345 questions
60,483 answers
95,285 users