Table of Contents
In this article, we will discuss the pointers with the one-dimensional array in C language
Already, we have looked at pointer in C language in this blog. Now, we’ll look at array and pointer. There is a very strong relationship between these two concepts. In this blog, we will discuss this relationship.
Let’s try to remember:
What is a pointer?
The pointer is a variable which stores the address of another address.
We can look at every array as a pointer. Elements of the array can also be accessed using the pointer.
We can declare a d array in C
main()
{
int marks(5)={23,67,85,97,45};
}
Here, we’ll assign a single dimension array of array size 5 initialised with the elements. “marks” is the name of the array and it’s also known as the base address of the array.
We can assume that it can allocate space in memory with a memory address.
This can be demonstrated by an example
Program 1
This example shows how to print the value of an element and store memory address of a value in an array.
marks[0] – array name is arr and this point to value of 0 index
&marks[0] – this point to address of marks[0];
int main()
{
int marks[5]={23,67,85,97,45};
// this is statements print value of element an arrary
printf(“value of marks 1 %dn”,marks[0]);
// print first value of index 0
printf(“value of marks 2 %dn”,marks[1]);
// print second value of index 1
printf(“value of marks 3 %dn”,marks[2]);
// print third value of index 2
printf(“value of marks 4 %dn”,marks[3]);
// print forth value of index 3
printf(“value of marks 5 %dn”,marks[4]);
// print fifth value of index 4
printf(“…………n”);
// this is statements print address of value in arrary
printf(“memory address of marks 1%un”,&marks[0]);
// print address value of index 0
printf(“memory address of marks 2%un”,&marks[1]);
// print address value of index 1
printf(“memory address of marks 3%un”,&marks[2]);
// print address value of index 2
printf(“memory address of marks 4%un”,&marks[3]);
// print address value of index 3
printf(“memory address of marks 5%un”,&marks[4]);
// print address value of index 4
return 0;
}
We may get the different address for every element of an array.
We can get the memory address using for loop and display statement.
This can be demonstrated by an example
program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks[]={15,20,25,30,35}; // one d array with elememt
int i;
for(i=0; i<6; ++i)
{
printf(“Address of marks in memory[%d]=%un”,i,&marks[i]);
}
return 0;
}
when the above code compiled and executed, it produces the following resut
In the example above, we can print the memory address of 5 elements in an array.
Memory address in char array displayed here
program 3
#include <stdio.h>
#include <stdlib.h>
int main()
{
char charArray[6];
int x;
for(x=0; x<7; ++x){
printf(“Address of charArray[%d]=%un”,x,&charArray[x]);
}
return 0;
}
when the above code compiled and executed, it produces the following resut
program 4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks[5]={23,67,85,97,45};
// this is statements print address of value in arrary
printf(“memory address of marks 1%un”,marks);
printf(“memory address of marks 1%un”,&marks);
printf(“memory address of marks 2%un”,&marks[0]);
return 0;
}
when the above code compiled and executed, it produces the following result
In the example above, we can print every element and element address. We use ‘&’ with element to print element address in the array.
address of marks+2 equivalent to (address of) &marks[2] // forget second element address,
address value incremented by 2
address of marks+3 equivalent to (address of) &marks[3] // forget third element address
address value incremented by 3
program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[]={15,20,25,30,35}; // one d array with elememt
int *ptr;//pointer diclaration
ptr=&arr; //Assign the address of array name to pointer
printf(“address of array %un”,&arr);
//display address of index 0 an array
printf(“address of array %un”,arr);
//display address of index 0 an array
printf(“address of array %un”,ptr);
//display address of index 0 an array using pointer
printf(“address of array %un”,ptr+1);
//array address increamant by 1, go to index 1
printf(“address of array %un”,arr+1);
//array address increamant by 1, go to index 1
printf(“address of array %un”,ptr+2);
//array address increamant by 2, go to index 2
printf(“address of array %un”,arr+2);
//array address increamant by 2, go to index 2
printf(“address of array %un”,ptr+3);
printf(“address of array %un”,arr+3);
//array address increamant by 3, go to index 3
printf(“address of array %un”,ptr+4);
printf(“address of array %un”,arr+4);
//array address increamant by 4, go to index 4
return 0;
}
arr,arr[0] are the base address of the above array program which is point to arr[0]
ptr is a pointer to point to array address
ptr+1, arr+1 are increment by 1 in an array address from base address
ptr+n, arr+n are increment by n in an array address from base address
when the above code compiled and executed, it produces the following result
Related Links
Pointers in C Programming language
Pointers in C++ Programming language
Pointer to pointer in C Programming Language
Pointer to pointer in C++ Programming Language
Structure with pointers in C Language
Structure with pointers in C++ Language
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.