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?
}

Recommended Answers

All 5 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.