| C Tutorial | Plus Syntax | |||
|
Advanced C Arrays Plus Syntax Pointer Style and strcpy() Pointer Type Effects Arrays and Pointers Array Names Are Const Heap Memory Memory Management Dynamic Arrays Advantages of being in the heap Disadvantages of being in the heap Dynamic Strings
|
'+' Syntax
In a closely related piece of syntax, a + between a pointer and an integer does the same offset computation, but leaves the result as a pointer. The square bracket syntax gives the nth element while the + syntax gives a pointer to the nth element.
So the expression (intArray + 3) is a pointer to the integer intArray[3]. (intArray + 3) is of type (int*) while intArray[3] is of type int. The two expressions only differ by whether the pointer is dereferenced or not. So the expression (intArray + 3) is exactly equivalent to the expression (&(intArray[3])). In fact those two probably compile to exactly the same code. They both represent a pointer to the element at index 3.
Any [] expression can be written with the + syntax instead. We just need to add in the pointer dereference. So intArray[3] is exactly equivalent to *(intArray + 3). For most purposes, it's easiest and most readable to use the [] syntax. Every once in a while the + is convenient if you needed a pointer to the element instead of the element itself. Want To Know more with Video ??? Contact for more learning: webmaster@freehost7com |
|