Pointers and Array
- Arrays are internally stored as Pointers.
- So , array element can be handled using pointer.
- Any operation in Array which can be done by the subscripting(index number) that same operations can be done by pointers
- till now we have seen array of int , array of float , array of characters,but now we will see the Concept of Array of Pointers
For understanding this concept will declare an array of 5 integer number
lets declare it:
#include<stdio.h>
void main()
{
int a[5] = {1,2,3,4,5};
}
#include<stdio.h> void main() { int a[5] = {1,2,3,4,5}; }
Now will make an array pointers which will store the addresses of elements of array which we have declared above
Syntax:Declaring Array of Pointer.
Datatype *pointer_array_name[no_of _elements];
Datatype *pointer_array_name[no_of _elements];
Declaring and initializing:Array of pointers with size = 5.
#include<stdio.h>
void main()
{
int a[5] = {1,2,3,4,5};
int *p[5] = {&a[0],&a[1],&a[2],&a[3],&a[4]};
}
#include<stdio.h> void main() { int a[5] = {1,2,3,4,5}; int *p[5] = {&a[0],&a[1],&a[2],&a[3],&a[4]}; }
Let's Understand it visually.
Array 'p' is storing the addresses of elements of array 'a' , as elements in it.
- Same initialization can be done with another way.
- Let's see that way.
- As you know writing &array_name[0] is equivalent of writing array_name
- So we will use it.
Declaration and Initialization:
#include<stdio.h>
void main()
{
int a[5] = {1,2,3,4,5};
int *p[5] = {a,a+1,a+2,a+3,a+4};
}
#include<stdio.h> void main() { int a[5] = {1,2,3,4,5}; int *p[5] = {a,a+1,a+2,a+3,a+4}; }
Let's see another example:Array of float type:
#include<stdio.h>
void main()
{
float a[5] = {1.5,2.6,3.2,4.4,5.6};
float *p[5] = {a,a+1,a+2,a+3,a+4};
}
#include<stdio.h> void main() { float a[5] = {1.5,2.6,3.2,4.4,5.6}; float *p[5] = {a,a+1,a+2,a+3,a+4}; }
- This is how we can make array's of Pointers.
Further Topics:
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- How to declare Structure Pointer?
- Working with structure Pointer
- How to Create Structure Variable Dynamically ?
- Creating structure variable through malloc() and calloc()
Further Topics:
- How to initialize an structure variable?
- How to Access Structure member?
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- What are Pointers?
- how to declare pointers?
- how to initialize a pointers?
- How to access the pointer variables?
- Function of '*' and '&' operators
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
0 Comments
Post a Comment