Click here to Skip to main content
15,888,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
using namespace std;
typedef struct student stud
{
    int rno;
    stud *s;
}
int main()
{
    s->rno=30;
    cout<<s->rno;
    return 0;
}


What I have tried:

is this program showing error because we can't give an alternative name to a memory before its existence.If this is the the case then we couldn't have been able to use typedef even after structure definition, since a structure memory is created when it's variable is created?
my question is:
is it possible to create self-refrential structure using typedef keyword?
Posted
Updated 10-Sep-17 7:20am
v2
Comments
[no name] 10-Sep-17 12:44pm    
"is it possible to create self-refrential structure using typedef keyword":
Yes it is possible. But your code is responsible to allocate/deallocate stud *s;
Richard MacCutchan 10-Sep-17 14:52pm    
You have not declared a variable with the name s.

1 solution

The program is showing errors because there are errors...
Please note you don't need to typedef structs in C++.
Try
C++
#include<iostream>
using namespace std;

struct stud
{
    int rno;
    stud *s;
};

int main()
{
    stud * s = new stud();
    s->rno=30;
    s->s = nullptr;
    cout << s->rno <<endl;
    delete s;

    return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900