Pointer to an Array in C
Hello Everyone, In this article, we will cover the concept of Pointer to an Array in C Programming. Pointers are a fundamental aspect of C programming that allow for efficient manipulation and access to array elements. We will cover how to use pointers with arrays can greatly enhance your coding skills and make your programs more powerful and flexible. Whether you’re new to C or have some experience, this guide will help you to understand concet of Pointer to an Array in C effectively. Let’s get started.
A pointer to an array is a type of pointer that points to the memory address of the first element of an array. In other words, if you have an array of integers called “myArray”, a pointer to the array would contain the memory address of the first integer in that array.
In general, using a pointer to an array can be useful when you need to pass an array to a function or manipulate it in some way that requires access to its memory address.
Syntax to declare a pointer to an array
int myArray[10]; // declare an array of integers with 10 elements
int *ptr; // declare a pointer to an integer
ptr = myArray; // set the pointer to point to the first element of the array
In this example, the variable “ptr” is declared as a pointer to an integer, and then set to point to the first element of the “myArray” array using the assignment operator.
Once you have a pointer to an array, you can use it to access the elements of the array just like you would with the array itself. For example:
ptr[0] = 42; // set the first element of the array to 42
ptr[1] = 99; // set the second element of the array to 99
Here, we’re using the square bracket notation to access the elements of the array through the pointer. This is equivalent to using the square bracket notation with the array itself, like this:
myArray[0] = 42;
myArray[1] = 99;
One of the advantages of using a pointer to an array is that it allows you to easily pass the array to a function. For example, you could pass the pointer to the array as an argument to a function that needs to operate on the array:
void myFunction(int *arrayPtr, int size) {
// do something with the array here
}
int main() {
int myArray[10];
int *ptr = myArray;
myFunction(ptr, 10);
return 0;
}
In this example, we’re passing the pointer to the “myArray” array to the “myFunction” function along with the size of the array. The function can then use the pointer to access the elements of the array and perform whatever operations it needs to.
C Program to demonstrate the use of a pointer to an array:
#include <stdio.h>
int main() {
int myArray[5] = {1, 2, 3, 4, 5};
int *ptr = myArray; // create a pointer to the array
// print the array using the pointer
printf("Printing array using pointer:\\n");
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr+i)); // equivalent to ptr[i]
}
printf("\\n");
// modify array element using pointer
*(ptr+2) = 10;
printf("Array after modifying element using pointer:\\n");
for (int i = 0; i < 5; i++) {
printf("%d ", myArray[i]);
}
printf("\\n");
return 0;
}
In this program, we declare an array of integers called “myArray” with 5 elements and initialize it with some values. We then create a pointer to the array called “ptr” and initialize it to point to the first element of the array.
We use the pointer to print out the values of the array, and then modify an element of the array using the pointer. Finally, we print out the array again to show that the element was indeed modified.
Note that in the program, we use the “*(ptr+i)” notation to access the elements of the array through the pointer, which is equivalent to using the square bracket notation “ptr[i]”.