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

Recommended Answers

All 8 Replies

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

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 .

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

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?

yaaa thnksssss

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 */

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".

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?

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.