What error is happening? Can you be more specific?
You should compile with warnings enabled. On g++ (GCC) this can be done with -Wall.
Visual Studio has warning levels available in its options.
In function 'void create_linked_structure(structure_pointer)':
83:28: warning: variable 'q' set but not used [-Wunused-but-set-variable]
In function 'void delete_linked_structure(structure_pointer)':
70:30: warning: 'head' is used uninitialized in this function [-Wuninitialized]
In function 'void create_linked_structure(structure_pointer)':
84:30: warning: 'head' is used uninitialized in this function [-Wuninitialized]
"Head" is uninitialized, so when you assign it to be p, p's value is also uninitialized, so you are trying to dereference some junk value, and your program is probably crashing or doing some other weird behavior. This will happen during your call to delete_linked_structure.
1 2
head = new Structure;
head = get_structure(1,'a');
Not really an issue (besides being a clear memory leak), but note that here you're assigning head a value, but then immediately overwriting it with the call to get_structure. I would just delete the first line.
Also, you're not calling it, but if you want create_linked_structure to actually do something beneficial, I suggest having it return the head pointer.
Okay then, like I said, you should call create_linked_structure and have it return a structure_pointer instead, and also you should change it to match the logic existing in main.
Then, call it like this, replacing lines 26 to 34 with: head = create_linked_structure();
Change prototype, as well: structure_pointer create_linked_structure();
The function itself:
1 2 3 4 5 6 7 8 9 10 11 12 13
structure_pointer create_linked_structure()
{
structure_pointer head, q, p;
head = get_structure(1, 'a');
q = head;
for (int i=2; i<=10; i++)
{
p = get_structure(i, char('a' + -1));
q->f3 = p;
q = q->f3;
}
return head;
}
int myFakeMain()
{
structure_pointer head,p,q;
head = new Structure;
head = get_structure(1,'a');
q=head;
for(int i=2; i<=10; i++)
{
p= get_structure(i, char('a' + -1));
q-> f3 = p;
q= q-> f3;
}
print_main_structure(head);
return 0;
}
int main() {
return myFakeMain();
}
But you haven't addressed any of the issues from your previous threads, like the gratuitous memory leak, and the meaningless names you use all over the place.
I mean, you use _structure or structure_ everywhere, and in doing so, you render it meaningless.
Salem's answer isn't bad, though. Turn old main into a function. Call it with new main. Clean up old main if you need to (here, make it void and remove return, move the for loop into a function or method (pass p,q, whatever it needs), and its done).