I am getting a segmentation fault when running the following code. I know there is something fundamental I'm misunderstanding about pointers here, but I can't seem to figure it out.
#include <iostream>
using namespace std;
struct intptr{
intptr* a;
int value;
};
void printint(intptr q){
if (q.a == &q){
cout<<q.value<<endl;
return;
}
else
printint(*(q.a));
}
int main(){
intptr y,z;
z.value = 1;
y.value = 2;
*(y.a) = z;
*(z.a) = z;
printint(x);
}
I've also tried the following, but it never recognizes that q.a = &q
is true.
#include <iostream>
using namespace std;
struct intptr{
intptr* a;
int value;
};
void printint(intptr q){
if (q.a == &q){
cout<<q.value<<endl;
return;
}
else
cout<<"not finished"<<endl;
printint(*(q.a));
}
int main(){
intptr y,z;
z.value = 1;
y.value = 2;
y.a = &z;
z.a = &z;
printint(y);
}