Problem with scanf () on mac

I am compiling a C library on mac os x. When I typed in the input and after printing the data on the screen, I do not see anything.

char *path = NULL;
peerdisplay = "Bob";
printf("Insert the full 'To' path: ");
scanf(" %a[^\n]", &path);
printf("A path: %s \n", &path);

When I replace% a with% s, printing is fine, but after starting I have a segmentation error. I like working as a script.

+3
source share
4 answers

% a is a gnu-specific non-standard extension for graf. What does your OS X say about this?

GNU C ,               % s % a [range].        ( ,% as       % []). (3)

^ - ?

, ,

a , gcc       -std = c99 gcc -D_ISOC99_SOURCE ( _GNU_SOURCE ,       fied), a ,       (. ).

+2

. , , NULL.

+2

path - , , , null char.

path scanf printf, path.

, %s %a

+1

First of all, if you pass null to scanf, as you do here, you basically tell the c library to copy any line entered into zero space (for example, the first page of memory, usually write-protected for this reason). Secondly,% a should match a floating point number, not a string. Thirdly, it might be a good idea to really read the documentation for the library function before you start calling it.

+1
source

Source: https://habr.com/ru/post/1762298/


All Articles