The Gateway to Computer Science Excellence
+1 vote
149 views

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 by Veteran (117k points) | 149 views
+1
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 !
0
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

@Shaik Masthan

I think you missed the ' ' in

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

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

+1

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

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

0 votes
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.

by Boss (21.6k points)
edited by
0
0 and c

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

Sorry my mistake . it should be ')'.

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

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

if it is there... Then this question belongs to undefined behaviour
0
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.
0

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

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

@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
by (299 points)

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
50,647 questions
56,508 answers
195,529 comments
100,962 users