The Gateway to Computer Science Excellence
+2 votes
301 views
#include<stdio.h>
#include<stdlib.h>

int main()
{
    struct xx
    {
        int x;
        char s;
    };
    struct xx  *t;
    t->x=5;
    t->s='a';
    printf("%d %c\n", t->x, t->s);

}
in Programming by | 301 views
0
Error at printf statement
0
thats a typo- corrected now..

2 Answers

+1 vote
Best answer

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

    t->x=5;
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;)



 

by
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

by
edited by
0
no. For every pointer in C, it must get a valid memory address before being dereferenced (using *).
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,497 answers
201,862 comments
95,319 users