N
N
Nikita Kolosov2011-02-28 13:39:09
C++ / C#
Nikita Kolosov, 2011-02-28 13:39:09

C program question

There is the following piece of code. If we enter “1”, the program execution ends for some reason, the message “Segmentation error” is displayed. In any other case, everything is ok. If we write int act = X, act, value; similarly - if X == 1, then "Segmentation error", otherwise - everything is OK. Compiler gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 What could be the problem?

...
int main(int argc, char* argv[])
{
int act, act2, value;

printf("Выберите действие:\n1. Вывод списка\n2. Добавить элемент в список\n0. Выйти из программы\n->");
scanf("%d", &act);

printf("Введенное значение:\n%d\n", act);
...
}





Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
VBart, 2011-02-28
@Anexroid

Naturally, the error occurs in return (item->next == NULL)? ten;
when you enter 1, item contains NULL, and you are trying to dereference it and refer to a non-existent offset "next"

D
Dzuba, 2011-02-28
@Dzuba

On which line is the error thrown? Exactly in this piece of code?
I checked your piece of code on gcc (CentOS) - no errors.

V
VBart, 2011-02-28
@VBart

Well, you don’t see the last printf, because it gets into the buffer and your program sigfaults before the buffer has time to be read into the console.
If you do fflush(STDOUT) after printf("+"), you will see "+".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question