printf(
"
hello inside queue_initialize\n"
); <br />
list_initialize(queue_list->ptr_list);<br />
printf(
"
hello after queue_initialize\n"
);<br />
return
;<br />
}<br />
void
list_initialize(List *ptr_list)<br />
{<br />
printf(
"
hello in list_initialize\n"
);<br />
ptr_list->head=
0
;<br />
printf(
"
hello\n"
);<br />
ptr_list->tail=
0
;<br />
printf(
"
hello\n"
);<br />
ptr_list->number_of_nodes=
0
;<br />
printf(
"
hello after list_initialize\n"
);<br />
when i call the function queue_initialize(Queue *queue_list) i get this output
hello before queue_initialize<br />
hello inside queue_initialize<br />
hello
in
list_initialize
but the expected output is
hello before queue_initialize<br />
hello inside queue_initialize<br />
hello
in
list_initialize<br />
hello <br />
hello<br />
hello after list_initialize
can anyone tell me whats wrong with this code? your effort is really appriciated please help me fast
i am pretty sure that error is here in these lines
ptr_list->head=
0
;<br />
ptr_list->tail=
0
;<br />
ptr_list->number_of_nodes=
0
;
Quote:
A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).
On Unix-like operating systems, a signal called SIGSEGV is sent to a process that accesses an invalid memory address.
For some details on
Segmentation Fault
[
^
].
Your case, most likely, your ptr_list does not have a valid value. Your queue setup looks incorrect and I am not sure if how is your Node defined (should be defined if you are getting some output).
In C, it is not the correct setup - you need to add struct keyword before the pointers for connecting them:
struct
Node {
int
data;
struct
Node* next;
Would suggest to read and learn about linked list.
Refer:
Linked List Program in C - Tutorialspoint
[
^
]
Linked List | Set 1 (Introduction) - GeeksforGeeks
[
^
]
#include
<
stdio.h
>
#include
<
stdlib.h
>
typedef
struct
node
int
value
;
struct
node * next;
} Node;
typedef
struct
list
Node *head;
Node *tail;
int
number_of_nodes;
} List;
typedef
struct
queue
List *ptr_list;
} Queue;
void
list_initialize(List *ptr_list)
printf(
"
hello in list_initialize\n"
);
ptr_list-
>
head=
0
;
printf(
"
hello\n"
);
ptr_list-
>
tail=
0
;
printf(
"
hello\n"
);
ptr_list-
>
number_of_nodes=
0
;
printf(
"
hello after list_initialize\n"
);
void
queue_initialize(Queue *queue_list)
queue_list-
>
ptr_list = (List*) malloc(
sizeof
(List));
printf(
"
hello inside queue_initialize\n"
);
list_initialize(queue_list-
>
ptr_list);
printf(
"
hello after queue_initialize\n"
);
return
;
int
main()
Queue queue;
queue_initialize(&queue);
return
0
;
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.