C Programs Tutorials | IT Developer
IT Developer

C Programming - C Pointer vs Array



Share with a Friend

C Programming - C Pointer vs Array

C Pointer vs Array

In C programming, pointers and arrays are closely related, but they are not the same. Understanding their differences and similarities is crucial for efficient memory management and programming. Below, we will compare pointers and arrays in C, covering their characteristics, differences, and use cases.

  1. Definition:
  • Array: An array is a collection of elements of the same type, stored in contiguous memory locations. The array name represents a pointer to the first element of the array.

C

int arr[5] = {1, 2, 3, 4, 5};

  • Pointer: A pointer is a variable that holds the address of another variable or data. Pointers can point to any data type, including arrays, structures, or other pointers.

C

int *ptr;

  1. Memory Allocation:
  • Array: Arrays are statically allocated when declared. This means the memory for an array is allocated at compile time.
    • The size of the array is fixed and cannot be changed during runtime.
  • Pointer: Pointers are dynamically allocated using functions like malloc(), calloc(), or realloc() during runtime. This provides flexibility in memory allocation.

C

int *ptr = (int *)malloc(5 * sizeof(int)); // Dynamically allocated memory for 5 integers.

  1. Size:
  • Array: The size of an array is fixed and determined at compile time. You can use the sizeof() operator to determine the total size of the array, but you cannot resize it after declaration.

C

int arr[10];

printf("Size of array: %zu\n", sizeof(arr)); // Prints the total size of the array.

  • Pointer: The size of a pointer variable is always fixed (the size of the pointer itself), but it can point to any dynamically allocated memory, which can be resized.

C

int *ptr = (int *)malloc(5 * sizeof(int));

printf("Size of pointer: %zu\n", sizeof(ptr)); // Prints the size of the pointer itself, not the allocated memory.

  1. Accessing Elements:
  • Array: Arrays are accessed using the array index, and the array name represents the address of the first element.

C

printf("First element: %d\n", arr[0]);  // Access using index

  • Pointer: Pointers can be dereferenced using the dereference operator * and the pointer arithmetic.

C

printf("First element: %d\n", *ptr);  // Dereferencing the pointer

  1. Pointer Arithmetic:
  • Array: You can use the array name to represent a pointer to the first element. Array elements can be accessed using the array index, but you can also use pointer arithmetic to traverse the array.

C

int *ptr = arr;

printf("First element: %d\n", *ptr);     // Equivalent to arr[0]

printf("Second element: %d\n", *(ptr + 1)); // Equivalent to arr[1]

  • Pointer: Pointers allow you to perform arithmetic (e.g., increment or decrement) to navigate through memory addresses.

C

ptr = arr;  // Pointer points to the first element of the array

printf("First element: %d\n", *ptr);      // dereference the pointer

ptr++;  // Increment pointer to the next element

printf("Second element: %d\n", *ptr);

  1. Array vs Pointer Representation:
  • Array: The name of an array represents the base address (pointer to the first element), but it is not a pointer itself. The name of the array cannot be reassigned.

C

int arr[5];

arr = ptr; // Error: Cannot assign a new address to an array.

  • Pointer: A pointer holds the address of a variable and can be reassigned to point to a different memory location.

C

int *ptr = arr;

ptr = another_ptr; // Valid: Pointers can be reassigned.

  1. Array vs Pointer in Function:
  • Array: When an array is passed to a function, it is passed as a pointer (the address of the first element). So, modifying an array within a function affects the original array.

C

void modifyArray(int arr[]) {

    arr[0] = 10;  // Modifies the original array

}

int main() {

    int arr[5] = {1, 2, 3, 4, 5};

    modifyArray(arr);

    printf("First element: %d\n", arr[0]);  // Output will be 10

    return 0;

}

  • Pointer: You can also pass pointers to functions. The pointer passed can be modified to point to different addresses.

C

void modifyPointer(int *ptr) {

    *ptr = 10;  // Modifies the value pointed to by the pointer

}

int main() {

    int num = 5;

    modifyPointer(&num);

    printf("Value: %d\n", num);  // Output will be 10

    return 0;

}

  1. Dynamic Memory Allocation:
  • Array: Arrays cannot be resized during runtime. If you want to resize an array, you would have to declare a new array with a larger size and copy the elements.
  • Pointer: Pointers allow dynamic memory allocation with functions like malloc(), calloc(), and realloc(). You can allocate and resize memory during runtime, which gives flexibility.

C

int *ptr = (int *)malloc(5 * sizeof(int));  // Allocate memory

ptr = (int *)realloc(ptr, 10 * sizeof(int)); // Resize the allocated memory

  1. Initialization:
  • Array: Arrays can be initialized using a list of values, and each element is assigned its value directly.

C

int arr[5] = {1, 2, 3, 4, 5};

  • Pointer: Pointers need to be initialized with an address. If you're using dynamic memory, you need to allocate memory explicitly.

C

int *ptr = (int *)malloc(5 * sizeof(int));

  1. Performance:
  • Array: Arrays are generally faster in terms of memory access, as they are contiguous in memory and have a fixed size.
  • Pointer: Pointer operations can be slower compared to array access due to the overhead of dynamic memory management. However, pointers are more flexible and efficient when managing large or dynamic data structures like linked lists.

Summary of Differences:

Feature Array Pointer
Memory Allocation

Statically allocated

Dynamically allocated

Size

Fixed, cannot be resized

Variable, can point to dynamic memory

Accessing Elements

Using index arr[i]

Using pointer arithmetic *(ptr + i)

Pointer Arithmetic

Array name can be treated as a pointer

Pointer arithmetic can be performed

Memory

Contiguous block

Can point to any part of memory

Function Passing

Passed as a pointer to the first element

Passed as a pointer to the memory

Reassignability

Cannot be reassigned

Can be reassigned to point to different memory locations

Conclusion:

Both pointers and arrays are important concepts in C programming, and while they share some similarities, such as both representing memory addresses, they have distinct differences. Arrays are best suited for fixed-size collections, while pointers provide more flexibility for dynamic memory management and complex data structures. Understanding the relationship between arrays and pointers is key to mastering C programming.