Arrays and Pointers - C Array

C examples for Array:Introduction

Introduction

Array in C is a constant pointer to the first element in the array.

By incrementing the pointer by one, you move to the next element in the array.

Changes to a pointer's address are implicitly multiplied by the size of the pointer's data type.

Demo Code

#include <stdio.h>
int main(void) {
   int myArray[] = { 1, 2, 3 }; /* alternative */
   *(myArray+1) = 10; /* myArray[1] = 10; */
   printf("%d", myArray[1]);
}

Result

The four arithmetic operators that can be used with pointers include: +, -, ++, and --.

Demo Code

#include <stdio.h>
int main(void) {
   int myArray[] = { 1, 2, 3 }; /* alternative */

    int* ptr =  myArray;
    printf("Address of myArray[0]: %p \n", ptr); /* ex. 0028FF14 */
    ptr++;/*w w w . ja  v  a 2 s  .  c o  m*/
    printf("Address of myArray[1]: %p", ptr); /* ex. 0028FF18 */
}

Result


Related Tutorials