0% found this document useful (0 votes)
22 views

Pointer Vs Array in C

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Pointer Vs Array in C

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6/16/24, 1:14 PM Pointer vs Array in C

Pointer vs Array in C
Arrays and Pointers are two important language constructs in C, associated with
each other in many ways. In many cases, the tasks that you perform with a pointer
can also be performed with the help of an array.

However, there are certain conceptual differences between arrays and pointers. Read
this chapter to understand their differences and comparative advantages and
disadvantages.

Arrays in C
In a C program, an array is an indexed collection of elements of similar type, stored
in adjacent memory locations.

To declare an array, we use the following syntax −

data_type arr_name [size];

The size should be a non-negative integer. For example −

int arr[5];

The array can be initialized along with the declaration, with the elements given as
a comma-separated list inside the curly brackets. Mentioning its size is optional.

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

Each element in an array is characterized by a unique integral index, starting from


"0". In C language, the lower bound of an array is always "0" and the upper
bound is "size – 1".

Example of an Array

The following example shows how you can traverse an array with indexed subscripts

#include <stdio.h>

int main (){

https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 1/5
6/16/24, 1:14 PM Pointer vs Array in C

/* an array with 5 elements */


int arr[5] = {10, 20, 30, 40, 50};

int i;

/* output each array element's value */


printf("Array values with subscripts: \n");

for(i = 0; i < 5; i++){


printf("arr[%d]: %d\n", i, arr[i]);
}

return 0;
}

Output

When you run this code, it will produce the following output −

Array values with subscripts:


arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50

Pointers in C
C allows you to access the memory location of a variable that has been randomly
allocated by the compiler. The address−of operator (&) returns the address of the
variable.

A variable that stores the address of another variable is called a pointer. The type of
the pointer must be the same as the one whose address it stores.

To differentiate from the target variable type, the name of the pointer is prefixed
with an asterisk (*). If we have an int variable, its pointer is declared as "int *".

int x = 5;
int *y = &a;

https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 2/5
6/16/24, 1:14 PM Pointer vs Array in C

Note: In case of an array, the address of its 0th element is assigned to the pointer.

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


int *ptr = &arr[0];

In fact, the name of the array itself resolves to the address of the 0th element.

Hence, we can as well write −

int *ptr = arr;

Since the elements of an array are placed in adjacent memory locations and the
address of each subscript increments by 4 (in case of an int array), we can use this
feature to traverse the array elements with the help of pointer as well.

Example of a Pointer

The following example shows how you can traverse an array with a pointer −

#include <stdio.h>

int main (){

/* an array with 5 elements */


int arr[5] = {10, 20, 30, 40, 50};
int *x = arr;

int i;

/* output each array element's value */


printf("Array values with pointer: \n");

https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 3/5
6/16/24, 1:14 PM Pointer vs Array in C

for(i = 0; i < 5; i++) {


printf("arr[%d]: %d\n", i, *(x+i));
}

return 0;
}

Output

Run the code and check its output −

Array values with pointer


arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50

Difference between Arrays and Pointers in C


The following table highlights the important differences between arrays and pointers

Array Pointer

It stores the elements of a homogeneous data It stores the address of a variable,


type in adjacent memory locations. or an array

An array is defined as a collection of similar A pointer is a variable that stores


datatypes. address of another variable.

The number of variables that can be stored is A pointer can store the address of
decided by the size of the array. only a single variable.

The initialization of arrays can be done while Pointers cannot be initialized while
defining them. defining them.

The nature of arrays is static. The nature of pointers is dynamic.

Arrays cannot be resized according to the Pointers can be resized at any


user's requirements. point in time.

https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 4/5
6/16/24, 1:14 PM Pointer vs Array in C

The allocation of an array is done at compile The allocation of the pointer is


time. done at run time.

https://www.tutorialspoint.com/cprogramming/c_pointer_vs_array.htm 5/5

You might also like