Hi, every one know that scanf statement should use "&" ambers ion symbol, but why it is not used while getting a string.
eg:
main()
{
int a;
char str[10];
scanf("%d",&a);
scanf("%s",str); -->why "&" sybbol is not used in getting string?
}
Hi, every one know that scanf statement should use "&" ambers ion symbol, but why it is not used while getting a string.
eg:
main()
{
int a;
char str[10];
scanf("%d",&a);
scanf("%s",str); -->why "&" sybbol is not used in getting string?
}
Answering directly: scanf needs a pointer so it can store characters into caller memory. For a plain variable like int a you must write &a to pass its address. A char array is different because the array name usually converts (decays) to a pointer to its first element when used as a function argument. That is what meant and what expanded on — arrays are not pointers, but the name converts to a char * in this context, so you pass str (or &str[0]) rather than &str.
A couple of important technical details to clear up: the object char str[10] is an array type, not a pointer type. The expression str used in a call becomes a char *. The expression &str is a pointer-to-array (char (*)[10]) — same numeric address but a different type, so don’t use &str with functions expecting char *. You can see a practical difference with sizeof:
char s[10];
char *p = s;
printf("%zu %zu\n", sizeof s, sizeof p); // sizeof s == 10, sizeof p == size of pointer Safety and best practice (missing from earlier replies): never use an unbounded %s because it can overflow the buffer. Either limit the width in scanf (leave room for the NUL) or use fgets and strip the newline:
char buf[10];
scanf("%9s", buf); // leave one byte for '\0'
// safer: read a line and remove trailing newline
if (fgets(buf, sizeof buf, stdin)) {
size_t n = strlen(buf);
if (n && buf[n-1] == '\n') buf[n-1] = '\0';
} One last correction to ’s example: print pointer addresses with %p and cast to void * for portability, e.g. printf("%p\n", (void*)buf);. Bottom line: pass the pointer the function expects — for arrays str already provides that pointer — and always guard buffer sizes.
Jump to Post— Radical Edward 301> because a char array is the same as a char *
This can be a misleading statement. Arrays are not pointers, but an array name *is* converted to a pointer most of the time in practice.> every one know that scanf statement should use "&" ambers ion
…
Jump to Post— Radical Edward 301Sorry, Ed is going to take the easy way out by linking you to a comprehensive tutorial on pointers. It's in C++, but the principles for C are all still there. Until you …
because a char array is the same as a char *, just it's initialized on creation.
> because a char array is the same as a char *
This can be a misleading statement. Arrays are not pointers, but an array name *is* converted to a pointer most of the time in practice.
> every one know that scanf statement should use "&" ambers ion
> symbol, but why it is not used while getting a string.
To understand this you need to understand passing by reference in C. scanf() expects a pointer because that is the only way the argument can be modified inside scanf() without that modification being lost. Here is a sample exhibiting the underlying issue.
#include <stdio.h>
void pass_by_value(int x)
{
x = 1;
}
void pass_by_ref(int *x)
{
*x = 1;
}
int main(void)
{
int x = 0, y = 0;
pass_by_value(x); /* The change does not persist! */
pass_by_ref(&y); /* The change persists! */
printf("%d\n%d\n", x, y);
return 0;
} That is why you use & for non-pointer objects in scanf(). But an array is special. An array name evaluates to a pointer to the first element.
scanf("%s", str); /* == to scanf("%s", &str[0]); */ The argument is already a pointer of the right type, so & is not needed.
> because a char array is the same as a char *
This can be a misleading statement. Arrays are not pointers, but an array name *is* converted to a pointer most of the time in practice.> every one know that scanf statement should use "&" ambers ion
> symbol, but why it is not used while getting a string.
To understand this you need to understand passing by reference in C. scanf() expects a pointer because that is the only way the argument can be modified inside scanf() without that modification being lost. Here is a sample exhibiting the underlying issue.#include <stdio.h> void pass_by_value(int x) { x = 1; } void pass_by_ref(int *x) { *x = 1; } int main(void) { int x = 0, y = 0; pass_by_value(x); /* The change does not persist! */ pass_by_ref(&y); /* The change persists! */ printf("%d\n%d\n", x, y); return 0; }That is why you use & for non-pointer objects in scanf(). But an array is special. An array name evaluates to a pointer to the first element.
scanf("%s", str); /* == to scanf("%s", &str[0]); */The argument is already a pointer of the right type, so & is not needed.
Thank you for helping me but can u explain me in detail...
u mean that while getting other values like integer, character, float it's value is stored in the specific memory location which is specified by the compiler. In order to access the memory location we use "&" ambers. Am i wright?
but in case of string what happen, rather using memory location what it uses. i got some what that it uses pointer and I also know pointer uses memory location so we transfer memory address with the ambers symbol.
I can get some thing but not fully so can u pls explain me in detail.............
Sorry, Ed is going to take the easy way out by linking you to a comprehensive tutorial on pointers. It's in C++, but the principles for C are all still there. Until you have a solid grasp on pointers, any details Edward can think of would probably be unhelpful.
int a;
scanf("%d",&a);
here a is the variable and &a is the address of the variable.
so, we can understand that the second parameter need the starting address of the variable;
char str[10];
scanf("%s",str);
here str is not variable, it is the address of str[0]. i.e. the starting address of str[10].
you can test it by-
printf("%u",&str[0]);
printf("%u",str);
**"%u" because address is unsigned integer type.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.