The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+2 votes

int main()
    struct xx
        int x;
        char s;
    struct xx  *t;
    printf("%d %c\n", t->x, t->s);



asked in Programming by Loyal (2.8k points) | 174 views
Error at printf statement
thats a typo- corrected now..

2 Answers

+1 vote
Best answer

 struct xx  *t;
//Here t is a pointer to struct xx

Assigns 5 to the int part of struct xx object pointed to by t. But t is not assigned any struct xx object to point to. So, this is doing invalid memory access and should result in segmentation fault. 

(similar to int *p; *p = 5;)


answered by Veteran (14.6k points)
selected by
0 votes

there is no error in program other than printf statement

 printf("%d %c\n",t⟶x, t⟶s); will be the correct statement

only expession syntax will be there

answered by Veteran (76.8k points)
edited by
no. For every pointer in C, it must get a valid memory address before being dereferenced (using *).

Related questions

+3 votes
2 answers
+1 vote
1 answer
asked in Programming by set2018 Boss (7.6k points) | 42 views
0 votes
1 answer

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

32,620 questions
39,267 answers
36,656 users