2 September 2015

How 8 Things Will Change The Way You Approach Array With Pointers In C



Today's topic is arrays with pointers in c programming. In this post we will learn about arrays, accessing arrays with the help of pointer, basic properties of arrays, passing arrays in function with the help of pointers.


Definition of arrays-It is the collection of same data type with a continuous memory location.
Basic about array you can learn from niche link.



5 best website to learn C programming


Note- The indexing of array is starting from 0.i.e if u want to access 4th element your array index will 3(4-1).


2) In c programming language, you have to write array  index must.i.e int arr[u have to put no of item in array].Index will be 
constant number.You can't declare 
int n=6;
int arr[n]; 
It will give error.

3)If you declare and initialization of array is doing same time then array index is not necessary in first-dimensional array.But in second-dimensional array second index is necessary to fill like int arr[][must fill with number].


4)array can access and modify using a loop. Therefore array has time complexity is high. 


5)one-dimensional array can access by pointer as follow


int i;
int arr[]={3,4,5,};//
int *ptr;
ptr=&arr[0]; // u can also write ptr=(arr+0);
for(i=0;i<3;i++){
printf("%d ",*ptr);
ptr++; //it will increament ptr value.you can read here about this .

}


6)one dimensinal array pass into function with help of pointer as

void display(int *p,int n){
int i;
 for(i=0;i<n;i++){
printf("%d  ",*p);
p++;
 }

void main(){
int arr[]={3,5,6,7};
display(&arr[0],4);
}

7)

Note: We can't initialize array at declaration time but we can declared and initialize it by giving address value.We can access this pointer value by * and address by & operator as shown in above picture.
you can find more APPLY THESE 10 SECRET TECHNIQUES TO IMPROVE POINTER IN C PROGRAMING

8) We can access array data as follow
in a loop we use arr[i] which is also equal to *(arr+i), *(i+arr), i[arr] 
i.e. arr[i]=*(arr+i)=*(i+arr)=i[arr].

If you have something new related to this topic then must comment and help other. Thanks

No comments:

Post a Comment

Ads Inside Post

Contributors