Possible duplicate:Why doesn't scanf need an ampersand for strings and also works fine in printf (in C)?
In C, when we use the scanf() function to enter the user, we always use the & sign in front of the variable. For instance:
scanf()
&
scanf("%d", &number); scanf("%c", &letter);
This ensures that our input is stored in the appropriate address. But in the case of a string, we do not use & .
Why is this?
A "string" in C is the address of the character buffer.You want scanf fill memory in the buffer pointed to by the variable.
scanf
In contrast, int is a block of memory, not an address. To scanf fill this memory, you need to pass its address.
int
Since the identifier buf in char buf[512]; degrades to a pointer to the first element of the array, you do not need to point to a pointer to a buffer in the scanf argument. By passing &buf pointer passed by scanf is now a bi-directional pointer (this is a pointer to a pointer to the beginning of the buffer) and is likely to cause a program crash or other bad behavior.
buf
char buf[512];
&buf
Source: https://habr.com/ru/post/1391772/More articles:How to reject all the prompts in the table when cell editing starts? - javahow to get distance using Wi-Fi - objective-cDo fill_n fill and fill the same function, but with different parameter overloads? - c ++How to implement Save and New functionality on a VisualForce page - apex-codeJVM 32-bit crashes on a 64-bit machine - javaHow can I execute an external program without interrupting a PHP script - phpCross browser callback solution when loading multiple images? - javascriptResult of caching SELECT statement for reuse in multiple queries - performanceHow to specify pid file in relative path in rails server command - ruby-on-railsUnexpected method call or property access in IE7 and IE8 - jqueryAll Articles