C programming pointers and arrays


Pointers and arrays in c programming

Array tutorial in c programming language by examples


An array is derived data type in c programming language which can store similar type of data in continuous memory location. Data may be primitive type (int, char, float, double…), address of union, structure, pointer, function or another array.

Example of array declaration:
int arr[5];
char arr[5];
float arr[5];
long double arr[5];
char * arr[5];
int (arr[])();
double ** arr[5];


Array is useful when:

(a) We have to store large number of data of similar type. If we have large number of similar kind of variable then it is very difficult to remember name of all variables and write the program. For example:

//PROCESS ONE
#include<stdio.h>
int main(){
    int ax=1;
    int b=2;
    int cg=5;
    int dff=7;
    int am=8;
    int raja=0;
    int rani=11;
    int xxx=5;
    int yyy=90;
    int p;
    int q;
    int r;
    int avg;
    avg=(ax+b+cg+dff+am+raja+rani+xxx+yyy+p+q+r)/12;
    printf("%d",avg);

    return 0;        
}
If we will use array then above program can be written as:

//PROCESS TWO
#include<stdio.h>
int main(){
    int arr[]={1,2,5,7,8,0,11,5,50};
    int i,avg;
    for(int i=0;i<12;i++){
         avg=avg+arr[i];
    }
    printf("%d",avg/12);
    return 0;        
}

Question: Write a C program to find out average of 200 integer number using process one and two.

(b) We want to store large number of data in continuous memory location. Array always stores data in continuous memory location.

(q) What will be output when you will execute the following program?

#include<stdio.h>
int main(){
int arr[]={0,10,20,30,40};
    char *ptr=arr;
    arr=arr+2;
    printf("%d",*arr);
    return 0;        
}

Advantage of using array:

1. An array provides singe name .So it easy to remember the name of all element of an array.

2. Array name gives base address of an array .So with the help increment operator we can visit one by one all the element of an array.

3. Array has many application data structure.

Array of pointers in c:
Array whose content is address of another variable is known as array pointers.  For example:

#include<stdio.h>
int main(){
float a=0.0f,b=1.0f,c=2.0f;
    float * arr[]={&a,&b,&c};
    b=a+c;
    printf("%f",arr[1]);
    return 0;        
}


Complex arrays in c

1. Declaration of an array of size five which can store address such functions whose parameter is void data type and return type is also void data type:


void ( arr[5] )( );

2. Declaration of an array of size five which can store address such function which has two parameter of int data type and return type is  float data type:

float ( arr[5] )(intint);

3. Declaration of an array of size two which can store the address of printf or sacanf function:

int ( arr[2] )( const char *, … );

Note: prototype of printf function is:  int printf( const char *, … );

Different type of array in c:

(a) Array of integer
    An array which can hold integer data type is known as array of integer.

(b) Array of character
    An array which can hold character data type is known as array of character.

(c) Array of union
    An array which can hold address of union data type is known as union of integer.

For example:

(1) What will be output when you will execute the following program?

#include<stdio.h>
union A{
char p;
float const * const q;
};
int main(){
    union A arr[10];
    printf("%d",sizeof arr);
   return 0;      
}
  
Output: 20

(2) What will be output when you will execute the following program?

#include<stdio.h>
union A{
    char character;
    int ascii;
};
int main(){
    union A arr[2]={{65},{'a'}};
    printf("%c %c",arr[0],arr[1]);
       return 0;      
}
  
Output: A a

(d) Array of structure
 An array which can hold address of structure data type is known as array of structure. For example:

(1) What will be output when you will execute the following program?

#include<stdio.h>
typedef struct stu{
    char * name;
    int roll;
}s;
int main(){
    s arr[2]={{"raja",10},{"rani",11}};
    printf("%s %d",arr[0]);
    return 0;        
}

Output: raja 10

(2) What will be output when you will execute the following program?

#include<stdio.h>
struct A{
    int p;
    float q;
    long double *r;
};
int main(){
    struct A arr[10];
    printf("%d",sizeof arr);
   
    return 0;        
}

Output: 80

(e) Array of string
    An array which can hold integer data type is known as array of integer.

(f) Array of array
    An array which can hold address of another array is known as array of array.

(g) Array of address of integer
    An array which can hold address integer data type is known as array of address of integer.

Pointer to array
A pointer which holds base address of an array or address of any element of an array is known as pointer to array. For example:

(a)

#include<stdio.h>
int main(){
    int arr[5]={100,200,300};
    int *ptr1=arr;
    char *ptr2=(char *)arr;
    printf("%d   %d",*(ptr1+2),*(ptr2+4));

       return 0;       
}
Output: 300   44

(b)

#include<stdio.h>
int main(){
    static int a=11,b=22,c=33;
    int * arr[5]={&a,&b,&c};
    int const * const *ptr=&arr[1];
    --ptr;
    printf("%d ",**ptr);
        return 0;        
}

Output: 11

Pointers tutorial in c programming language by examples

Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure, union, enum.
Examples:


int *ptr;
int (*ptr)();
int (*ptr)[2];


In c programming every variable keeps two type of value.
1. Contain of variable or value of variable.
2. Address of variable where it has stored in the memory.


(1) Meaning of following simple pointer declaration and definition:
int a=5;
int * ptr;
ptr=&a;

Explanation:

About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory : 1025 (assume)

About variable ptr:
4. Name of variable : ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory : 5000 (assume)

Pictorial representation:




Note: A variable where it will be stored in memory is decided by operating system. We cannot guess at which location a particular variable will be stored in memory.

(2) Meaning of following pointer declaration and definition:
int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&pt1;


Explanation:

About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 50
3. Address where it has stored in memory : 5000 (assume)

About variable ptr1:
4. Name of variable : ptr1
5. Value of variable which it keeps: 5000
6. Address where it has stored in memory : 9000 (assume)

About variable ptr2:
7. Name of variable : ptr2
8. Value of variable which it keeps: 9000
9. Address where it has stored in memory : 9555 (assume)

Pictorial representation of above pointer declaration and definition:



Note:
* is known as indirection operator which gives content of any variable.
& is known as reference operator which gives address where variable has stored in memory.


Cancellation rule of above two operators:
* and & operators always cancel to each other i.e.
*&p=p

But it is not right to write:
&*p=p

Simple example:


What will be output of following c program?
#include<stdio.h>
int main(){

    int x=25;
    int *ptr=&x; //statement one
    int **temp=&ptr; //statement two
    printf(“%d %d %d”.x.*ptr,**temp);
    return 0;
}


Output: 25 25 25
Explanation:
As we know value of variable x is 25.


*ptr= *(&x) //from statement one
=*&x
=x //using cancellation rule
=25


**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25

Definition of pointer
How to read complex pointer
Arithmetic operation with pointer
Pointer to function
Pointer to array of function
Pointer to array of string
Pointer to structure
pointer to union
Multi level pointer
Pointer to array of pointer to string
Pointer to three dimentional array
Pointer to two dimensional array
Sorting of array using pointer
Pointer to array of array
Pointer to array of union
Pointer to array of structure
Pointer to array of character
Pointer to array of integer
Complex pointer
Generic pointer
Null pointer
Wild pointer
Dangling pointer
Near pointer
Far pointer
Graphics video memory
Text video memory
Huge pointer
Memory model in C
C tutorial

No comments: