0

Pointers

In the code inserted below : What is the meaning of ptr+i and why don’t we use ptr(i) ? int a[5] = {22, 33, 44, 55, 66}; int *ptr = NULL; int i; ptr = a; for (i = 0; i < 5; i++) { printf("%d ", *(ptr + i));

11th Jun 2020, 6:51 PM
Menna Ahmed
Menna Ahmed - avatar
3 Answers
+ 2
https://www.learncpp.com/cpp-tutorial/6-8a-pointer-arithmetic-and-array-indexing/#:~:text=The%20C%2B%2B%20language%20allows%20you,the%20previous%20integer%20before%20ptr%20. This is called as pointers arithmetic, check this link, there is everything what you neeed
11th Jun 2020, 6:55 PM
꧁༒☬Bad☬Boy☬༒꧂
꧁༒☬Bad☬Boy☬༒꧂ - avatar
+ 1
>> why don’t we use ptr(i) ? ptr(i) is an error, because ptr is not a function taking some argument; but ptr[i] is effectively equivalent to *(ptr+i) and can be used in place of that.
11th Jun 2020, 8:47 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
[user id=11348303]Bilbo[/user] thanks
11th Jun 2020, 8:48 PM
Menna Ahmed
Menna Ahmed - avatar