Write a C Program to Access elements of an array using pointer. Here’s simple Program to Access elements of an array using pointer in C Programming Language.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
The unary or monadic operator & gives the “address of a variable’”.
The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.
Below is the source code for C Program to Access elements of an array using pointer which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program to Access elements of an array using pointer */
#include <stdio.h>
int main()
{
int data[5], i;
printf("Enter 5 elements below :: \n");
for(i = 0; i < 5; ++i)
{
printf("\nEnter %d element :: ",i+1);
scanf("%d", data + i);
}
printf("\nNumbers Entered are :: \n");
for(i = 0; i < 5; ++i)
{
printf("\nElement %d = %d\n",i+1,*(data + i));
}
return 0;
}
Output : :
/* C Program to Access elements of an array using pointer */
Enter 5 elements below ::
Enter 1 element :: 7
Enter 2 element :: 2
Enter 3 element :: 5
Enter 4 element :: 1
Enter 5 element :: 4
Numbers Entered are ::
Element 1 = 7
Element 2 = 2
Element 3 = 5
Element 4 = 1
Element 5 = 4
Process returned 0
Above is the source code for C Program to Access elements of an array using pointer which is successfully compiled and run on Windows System.The Output of the program is shown above .
What are Pointers?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
The general form of a pointer variable declaration is −
Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
The unary or monadic operator & gives the “address of a variable’”.
The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.
Below is the source code for C Program to Access elements of an array using pointer which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
Output : :
Above is the source code for C Program to Access elements of an array using pointer which is successfully compiled and run on Windows System.The Output of the program is shown above .
need an explanation for this answer? contact us directly to get an explanation for this answer