4.1 1-Dimensional Array
The code snippet below involves an
int arr[] = {1, 2, 3}; // Array declarationint *ptr; // Pointer declaration
1. Array arr
*
int arr[] = {1, 2, 3};
-
Array Declaration:
arr
is an array of integers. This array holds three integers:1
,2
, and3
. -
Memory Representation: In memory,
arr
is a contiguous block of memory that holds these three integers. Each element ofarr
is stored in adjacent memory locations.If we look at this in memory, it could be represented like this:
arr[0] --> 1arr[1] --> 2arr[2] --> 3 -
Addressing: The name of the array
arr
represents the address of the first element in the array. That is,arr
is implicitly a pointer to the first element of the array.For example:
printf("%p\n", arr); // Prints address of the first element of arrprintf("%p\n", &arr[0]); // Prints address of the first element of arr (same as above)Both
arr
and&arr[0]
give the same result—i.e., they both point to the first element of the array.
2. *Pointer ptr
int *ptr;
-
Pointer Declaration:
ptr
is a pointer to an integer. However, it hasn’t been initialized yet, so it doesn’t point to any specific memory location at this point. It could point to anything (this could lead to undefined behavior if used without assignment). -
Memory Representation: Unlike arrays, a pointer is simply a variable that stores a memory address (the address of a variable or a block of memory). It can point to a single variable, an array, or any other type of object.
The pointer
ptr
can be assigned to point to a specific location, such as a variable or an array element, like this:ptr = arr; // Now ptr points to the first element of arrAfter the assignment,
ptr
holds the address of the first element of the arrayarr
. Thus,ptr
is a pointer to the same memory location asarr[0]
, and dereferencingptr
will give the value stored at that memory location.printf("%d\n", *ptr); // Outputs 1, since ptr points to arr[0]
3. Key Differences Between arr
and `ptr
-
Type and Memory Allocation:
arr
:arr
is an array of integers. The memory forarr
is allocated statically (at compile-time), and its size is determined by the number of elements in the array.- The name
arr
is not a pointer, but it can be used as one in expressions (i.e., it is implicitly converted to a pointer to its first element).
ptr
:ptr
is a pointer to an integer. It does not hold any data directly but instead holds the memory address of an integer (or an array element, etc.).- Pointers like
ptr
need to be explicitly assigned to point to a valid memory location (such as a variable, an array, or dynamically allocated memory).
-
Size and Modifications:
-
arr
:- The size of
arr
is fixed. In this case,arr
has 3 elements (because{1, 2, 3}
has 3 integers). - Arrays cannot be resized or reassigned to point to other locations once they are defined. The array name itself (
arr
) is a constant pointer to the first element, meaning you cannot change whatarr
points to. - For example,
arr++
is invalid because the array name is not a modifiable lvalue.
- The size of
-
ptr
:- A pointer like
ptr
can be reassigned to point to any valid memory address during runtime. It is much more flexible than an array. - Pointers can also be incremented (or decremented) to move through memory locations, as they are not tied to a fixed block of memory like arrays.
- A pointer like
-
-
Pointer Arithmetic:
-
arr
:- You cannot perform pointer arithmetic on
arr
itself (e.g.,arr++
is not allowed), but you can perform pointer arithmetic with&arr[0]
orarr
to move through the array:printf("%d\n", *(arr + 1)); // Outputs 2 (moves to the second element) - When using
arr
in expressions, it behaves like a pointer to the first element.
- You cannot perform pointer arithmetic on
-
ptr
:- You can perform pointer arithmetic with
ptr
since it is a pointer. For example,ptr++
will move the pointer to the next element in memory (in this case, to the next integer):ptr = arr; // Make ptr point to the first element of arrprintf("%d\n", *ptr); // Outputs 1 (the first element)ptr++; // Move the pointer to the next elementprintf("%d\n", *ptr); // Outputs 2 (the second element)
- You can perform pointer arithmetic with
-
-
Array vs Pointer Representation:
-
arr
: When you pass an array to a function, what you are actually passing is a pointer to the first element of the array. The array itself is not passed by value (i.e., no copy of the entire array is made). -
ptr
: When you pass a pointer to a function, you are passing the address of the variable or array it points to. A pointer is just a memory address, so it’s more flexible.
-
1.4 Example Demonstrating the Difference
#include <stdio.h>
int main() { int arr[] = {1, 2, 3}; int *ptr;
ptr = arr; // Assigning the address of the first element of arr to ptr
// Access elements using array printf("arr[0] = %d\n", arr[0]); // Outputs 1 printf("arr[1] = %d\n", arr[1]); // Outputs 2 printf("arr[2] = %d\n", arr[2]); // Outputs 3
// Access elements using pointer printf("ptr[0] = %d\n", ptr[0]); // Outputs 1 printf("ptr[1] = %d\n", ptr[1]); // Outputs 2 printf("ptr[2] = %d\n", ptr[2]); // Outputs 3
// Pointer arithmetic with ptr ptr++; // Move ptr to the next element (points to arr[1]) printf("ptr[0] = %d\n", ptr[0]); // Outputs 2 (because ptr now points to arr[1])
return 0;}
5. Output
arr[0] = 1arr[1] = 2arr[2] = 3ptr[0] = 1ptr[1] = 2ptr[2] = 3ptr[0] = 2
6. Summary of Key Differences
Feature | Array arr | Pointer ptr |
---|---|---|
Memory Allocation | Static (fixed size) | Can be dynamically allocated or point to any memory location |
Reassigning | Cannot be reassigned (fixed to the first element) | Can be reassigned to point to different memory locations |
Size | Fixed at compile time (3 in this case) | Size is not fixed; it depends on what it points to |
Pointer Arithmetic | Limited pointer arithmetic (arr + n works) | Full pointer arithmetic is allowed (ptr++ ) |
Memory Access | Access via indexing or pointer arithmetic (arr[i] ) | Access via dereferencing or pointer arithmetic (*ptr , ptr[i] ) |