Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: why no & for strings in scanf() function? [duplicate]

Tags:

c

string

scanf

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 get user input, we always use the & sign before the variable. For example:

  scanf("%d", &number);
  scanf("%c", &letter);

This ensures our input is stored in proper address. But in case of a string we do not use &.

Why is that?

like image 221
tarashish Avatar asked Dec 09 '22 03:12

tarashish


1 Answers

A "string" in C is the address of a character buffer.
You want scanf to fill the memory in the buffer, which is pointed to by the variable.

In contrast, an int is a block of memory, not an address. In order for scanf to fill that memory, you need to pass its address.

like image 170
SLaks Avatar answered Dec 24 '22 19:12

SLaks