Friday, August 03, 2012

C pointers and array examples

Some pointers and array examples
int ia[6] = {0, 1, 2, 3, 4, 5}; /* 1 */
int *ip; /* 2 */

ip = ia; /* equivalent to ip = &ia[0]; */ /* 3 */
ip[3] = 32; /* equivalent to ia[3] = 32; */ /* 4 */
ip++; /* ip now points to ia[1] */ /* 5 */
printf("%d ", *ip); /* prints 1 to the screen */ /* 6 */
ip[3] = 52; /* equivalent to ia[4] = 52 */ /* 7 */

argv
+---+
| 0 | ---> "./junk"
+---+
| 1 | ---> "-b"
+---+
| 2 | ---> "gradient"
+---+
| 3 | ---> "yeehaw"
+---+
**argv or *argv[] are equal. Array is just a pointer, and a double pointer is just an array of pointers.

No comments: