I am trying to do this in c:
scanf("%d",a+i);
where a is an array of size 10. And i is counter for loop. So is this possible?
I am trying to do this in c:
scanf("%d",a+i);
where a is an array of size 10. And i is counter for loop. So is this possible?
Absolutely: if a is an int* or an array int a[10], and i is between 0 and 9, this expression is valid.
The a+i expression is the pointer arithmetic equivalent of &a[i], which is also a valid expression to pass to scanf.
a is int a[10] or some such - as long as i is in range, of course.yes you can use a+i instead of &a[i],,,, The following code ask you to enter 10 numbers and will save them in an array,,,,and then displays the numbers in it.
check this code :
#include <stdio.h>
int main (void)
{
int a[10], i, j = 0;
for(i = 0; i < 10; ++i ){
printf("Element no %d = ",i);
scanf("%d",a+i);}
printf("Elements in your array are: ");
for(j = 0; j < 10; j++)
printf("%d ",a[j]);
return 0;
}
I hope if this code could help you !!
Try this solution:
#include <stdio.h>
int main (void)
{
int *p, i, j = 0, n;
printf("enter the value of n ");
scanf("%d",&n);
for(i = 0; i < n; ++i ){
scanf("%d",p+i);}
printf("Elements in your array are: ");
for(j = 0; j < 10; j++)
printf("%d ",*(p+i));
return 0;
}
a+iresults in a valid writable address for storing anint, can you think of a reason this would not work ?