Log In
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 302 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

0 votes
1 answer
X=2; Y=++x * ++x * ++x ; Printf("%d",Y); In the above question, we have to use the final value of x or it will be evaluated seperately and then multiplied. Ex: Y= 3*4*5; or Y=5*5*5;
asked Apr 11, 2019 in Programming Ashish Roy 1 224 views
0 votes
1 answer
#include<stdio.h> #include<stdlib.h> int main(void) { int maxLineCount = 500, maxCharCount = 500, index, j, count; char *line = NULL; size_t size; char *a[maxLineCount]; for (index = 0; index < maxLineCount; index++) a[index] = (char *)malloc(maxCharCount * ... of the code is doing. suppose we have 3 string given as input in 3 different lines then how can we access each character of the string?
asked Jun 1, 2019 in Programming Satbir 922 views
0 votes
0 answers
Can somebody explain this code line by line. I am unable to get and what will be it's output? #include<stdio.h> #include<stdlib.h> void transpose(int n, const double *A, double *B, const int *lda, int *perm) { } int transpose_equal(const double *A, const double*B, int total_size ... (ndim, B_trans, B, r_dim, r_perm); transpose_equal(A, B, total); free(A); free(B); free(B_trans); printf("\n"); } }
asked Apr 3, 2019 in Programming srestha 138 views
0 votes
0 answers
195 views main() { { extern int i; int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } int i; rude explained how output is printed but its giving error when i'm trying to run the program here
asked Nov 10, 2018 in Programming Mk Utkarsh 195 views