31 January 2016

Two dimensional array in C programming language

two dimensional array in C prohramming language

Hello friends, now I will talk about two-dimensional array.Here you will know in detail about two-dimensional array. It is also a type of array with multiple rows and columns like Matrics. Two dimension array used in maximum graphics processing.


We can write two dimession array as

int arr[][] we can left first but second can't. EX int arr[][2]={45,56,56,78};
Or int arr[][2]={
{45,56},
{56,78} //in last avoid ,
};
We can access it by using two for loop and arr[i][j]
int *p=&arr; //this way we can assign 2-D array to pointer


Program On Two dimesion array

#include <stdio.h>
#include <stdlib.h>
int main(){
int i,j;
int arr[][2]={23,45,56,65}; //we can't leave blank second bracket.
int *p=arr; //p pointer hold the address of zeroth row address of 2D array
for(i=0;i<2;i++){
for(j=0;j<2;j++){
//printf("%d ",*(p+i*2+j)); //array is like pointer. we can write like this.

//printf("%d ",*(*(arr+i)+j));
printf("%d ",arr[i][j]);
}
}
return 0;
}



Some important point

printf("%d",sizeof(arr)) will return 16
printf("%d",sizeof(arr[1][1])) return 4
printf("%d",sizeof(arr[0])) return 8


char *ch[]={"helloworld","morning"};//read as array of charecter pointers
sizeof(ch)=8+8=16
ch[0]=address of helloworld
ch[1]=address of morning
A memoey address of a long unsinged integer will be 8 byte.
 

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

Q. what is a dangling pointer in c?
If a pointer is pointing any memory location but meanwhile another pointer deletes the memory occupied by first Pointer while first pointer still points to that memory location,first   pointer will be known as dangling pointer , this problem is known as dangling pointer problem.

Q what is far pointer ?
A pointer which can access all the 16 segments(whole residence memory) of RAM is known as far pointer.
int far *p,*i;
sizeof(p) =4 byte
sizeof(i) =2 byte because p is close to far.

Passing 2-D array to a function

As we pass 1-D array in function with name of array and its size. Same passing 2-D array to function by name and lengtg of their row and columns.

int arr[2][3]={2,3,5,6,9,0}; //2 is row no and 3 is column no
display(arr,2,3);

Complete C program for passing two dimensional array to a function in Three methods

#include<stdio.h>
void display(int *q,int row,int col){
int i,j;
for(j=0;j<col;j++)
printf(" ",*(q+i*col+j));
printf("\n");
}
printf("\n");
}
void show(int (*q)[4],int row,int col){

int i,j;

int *p;
for(i=0;i<row;i++){

p=q+i;

 for(j=0:j<col;j++)

  printf"%d",*(p+j));


printf("\n");

}

printf("\n");

}



void print(int q[][4],int row,int col){

int i,j;

for(i=0;i<row;i++){

for(j=0;j<col;j++)

printf("%d",q[i][j]);

printf("\n");

}

printf("\n");


}
int main(){


int a[3][4]={

                     1,2,3,4,

                      5,6,7,8,

                      9,0,1,6


                    };

display(a,3,4);

show(a,3,4);

print(a,3,4);

return 0;

}

5 best website to learn C programming

pointer to array of some no of integer

There are one more method to access two dimensional



array called pointer to array of 2 integer
Like int (*p)[2];
It is read as p is a Pointer to an array of two integer.
Note parentheses in the declaration of p is necessaryy absence of them would make up p an array of 2 integer pointer. It is same as arr[][2]
It is used in two dimensionn array for keep the address of zeroth row first row etc.

Above show() function eleborate it.

Note: we can write arr[i][j]=*(arr[¡]+j)=*(*(arr+i)+j)
Two dimession array store in continuous merory
zeroth row till column no, again FiRST row till column no and so on.


If you have some points or question regarding topic then must comment or write us.

No comments:

Post a Comment

Ads Inside Post

Contributors