Hi,
Could anyone please show me why the following segfault occurs
at line 54 in the following code?
Thanks in advance
Phil
Could anyone please show me why the following segfault occurs
at line 54 in the following code?
Code:
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void printHello()
5 { printf("Hello "); }
6
7 void printThere()
8 { printf("There\n"); }
9
10 void (*FuncPtrArry[2])() = {&printThere, &printHello}; // Init function pointer array
11
12 typedef struct mystruct
13 {
14 struct mystruct *next;
15 void (* funcPtr)();
16 } NodeStruct;
17
18 NodeStruct *topNode = NULL;
19 NodeStruct *lastNode = NULL;
20 int count = 2;
21
22 void buildlist(void)
23 {
24 for (/* count already init to 2 */ ;count > 0; count--)
25 {
26 if (topNode == (NodeStruct *)NULL)
27 {
28 topNode = (NodeStruct *)malloc(sizeof(NodeStruct));
29 lastNode = topNode;
30
31 if (lastNode == (NodeStruct *)NULL)
32 return; /*ERROR */
33 }
34 else
35 {
36 lastNode -> next = (NodeStruct *)malloc(sizeof(NodeStruct));
37 if (lastNode -> next == NULL)
38 return; /*ERROR */
39 }
40
41 lastNode = lastNode -> next;
42 lastNode -> funcPtr = FuncPtrArry[count - 1];
43 lastNode -> next = (NodeStruct *)NULL;
44 } // end of for loop
45 }
46
47 int main()
48 {
49 buildlist();
50
51 while(topNode)
52 {
53 (*(topNode->funcPtr)) ();
54 topNode = topNode->next;
55 }
56 }
Thanks in advance
Phil
Comment