1.11M Members

Scanf in arrays

 
0
 

i am confused that when to use '&' sign in arrays.
some times in string array or in character array i got confused.
bcz array name itself is an address .so an1 can plz help me out of this??

 
0
 

It looks like you're not confused at all. Please provide some examples of what you don't understand.

 
0
 

i am confused that when to use '&' sign in arrays.
some times in string array or in character array i got confused.
bcz array name itself is an address .so an1 can plz help me out of this??

In C when we take input using scanf() function we have to take the address of the variable.
Here is some Example .I think it will make you clear.

int a;
scanf("%d",&i); //address of the variable

char a[10];
scanf("%s",a);//a stands for the base address of the string.

int b[10]

scanf("%d",b);//b is also the base address of the array .
 
0
 

thnx...bt then why we take & while using integer type of array?

 
0
 

thnx...bt then why we take & while using integer type of array?

consider example given by asitmahato.

int b[10]
 
scanf("%d",b);//b is also the base address of the array .

It is same as writing:

int b[10]
scanf("%d",&b);//gives 'address of' b,(the zeroth place)
printf("%d\n",*(arr));//prints the 'value at address" zero of array b

moreover

int b[10],i;
for(i=0;i<10;i++)
    scanf("%d",&b[i]);//gives 'address of' b starting from zeroth to ninth place

We can do same thing with arrays of char also

char b[5];
scanf("%c",&b);
printf("%c\n",*(b));

Was that clear enough?

 
0
 

yaaa thnksssss

 
0
 

consider example given by asitmahato.

int b[10]
     
scanf("%d",b);//b is also the base address of the array .

It is same as writing:

int b[10]
scanf("%d",&b);//gives 'address of' b,(the zeroth place)
printf("%d\n",*(arr));//prints the 'value at address" zero of array b

No, it's not the same. The address passed to scanf() will be the same, but the type of that object is different. It's the difference between a pointer to char and a pointer to an array of char. The latter is undefined behavior because it's not the expected type for the %d format specifier. This is a very subtle error that rarely manifests as a real problem, so beginners tend to have a hard time understanding it.

In value context, array names are converted to a pointer to the first element (ie. b in the above example is converted to &b[0] ). The reason we use the address-of operator (&) in scanf() is to get a pointer to the object so that scanf() can modify it through indirection. Since the array name is already a pointer, there's no need for another level of indirection.

Of course, this whole issue with arrays can be sidestepped by being explicit:

int b[10];

scanf("%d", &b[0]);

But that still doesn't help if you have a pointer to a scalar:

int *p = malloc(sizeof *p);

scanf("%d", p); /* No '&' necessary */
 
0
 

The address passed to scanf() will be the same

I was only discussing language specifications not implementations but I should admit I didn't know about the 'undefined behavior' in the above case.I'm glad you clarified what to use and what to not.

This is a very subtle error that rarely manifests as a real problem, so beginners tend to have a hard time understanding it.

Not if they bang their heads more often.Am I right?

The latter is undefined behavior because it's not the expected type for the %d format specifier.

But I don't get it. Why don't they(those who standardize C and even the compiler manufacturers) take care of this 'undefined behavior' thing. I guess this is one of the main reasons why C is hard to grasp and is even considered as a "dangerous programming language".

 
0
 

Why don't they(those who standardize C and even the compiler manufacturers) take care of this 'undefined behavior' thing.

How would they take care of it?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: