954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Scanf in arrays

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

devanshee
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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 .
asitmahato
Newbie Poster
17 posts since Aug 2009
Reputation Points: 6
Solved Threads: 1
 

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

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 
thnx...bt then why we take & while using integer type of array?

consider example given byasitmahato.

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?

diwakar wagle
Posting Whiz in Training
203 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
 

yaaa thnksssss

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

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 avery 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 */
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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".

diwakar wagle
Posting Whiz in Training
203 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
 
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?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: