in Programming in C
768 views
2 votes
2 votes
#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 in C
768 views

2 Comments

Error at printf statement
0
0
thats a typo- corrected now..
0
0

2 Answers

1 vote
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;)



 

selected by
0 votes
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

edited by

1 comment

no. For every pointer in C, it must get a valid memory address before being dereferenced (using *).
0
0