Write the array notation as pointer notation
The question is &str[2]
, if I write str+2
then it would give the address and its logical but whe开发者_StackOverflow社区re did I used pointer notation in it?
Should I prefer writing &(*(str+2))
?
You can use either
&str[2]
or
(str + 2)
Both of these are equivalent.
This is pointer arithmetic. So when you mention an array str
or &str
you refer to the base address of the array (in printf
for example) i.e. the address of the first element in the array str[0]
.
From here on, every single increment fetches you the next element in the array. So str[1]
and (str + 1)
should give you the same result.
Also, if you have num[] = {24, 3}
then num[0]
== *(num + 0)
== *(0 + num)
== 0[num]
精彩评论