Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use pointer in scanf to take input in an array?

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?

like image 734
Aman Tewary Avatar asked Mar 23 '23 15:03

Aman Tewary


1 Answers

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.

like image 63
Sergey Kalinichenko Avatar answered Apr 01 '23 08:04

Sergey Kalinichenko