Array and pointer in c programming


Array and Pointer

Hi Everyone

So today we are going to discuss about the derived datatype in c programming language. We are going to discuss about the concept of array and pointer. So array is collection of data of similar datatype. Pointer is a variable which points to another variable means pointer is a variable which stores address of another variable. Here i would like to add additional topic which is pointer to pointer concept. In this concept pointer stores address of another pointer . So now all this concepts are used in memory manegement. Here I would like to share difference between * and & sign. 
 So in the concept of pointer we will use & sign to see the address of pointer and to assign address of variable to pointer. We will use * sign to print the data which is stored in pointer means the value of variable where pointer points will be printed. For example

#include<stdio.h>
main()
{
    int a=10;
    int *p;
    *p=&a;
     int **q=&p;
    printf("%d\n",a);
    printf("%d\n",&a);
    printf("%d\n",*p);
    printf("%d\n",&p);
    printf("%d",**q);
}

/*Assume that address of a=1000 and p is 1004.
*/
OUTPUT: 
10
1000
10
1004
10

Here the value of &a and p are dependent of compiler becuase these are the memory address so I is dependent upon the compiler that which address compiler would assign to it. Let's see an example of array
#include<stdio.h>
main()
{
    int a[10]={1,2,3,4,5,6,7,8,9,10};
    printf("%d\n",a[0]);
    printf("%d\n",a[1]);
    printf("%d\n",a[2]);
    printf("%d\n",a[3]);
    printf("%d\n",a[4]);
    printf("%d\n",a[5]);
    printf("%d\n",a[6]);
    printf("%d\n",a[7]);
    printf("%d\n",a[8]);
    printf("%d\n",a[9]);
    return 0;
}
 OUTPUT:
1
2
3
4
5
6
7
8
9
10

Here the address would be assigned like this assuming address of a is 1000 for 16bit machine size of  integer is 2bytes.
a[0] address will be 1000
a[1] address will be 1002
a[2] address will be 1004
a[3] address will be 1006
a[4] address will be 1008
a[5] address will be 1010
a[6] address will be 1012
a[7] address will be 1014
a[8] address will be 1016
a[9] address will be 1018.

The array index always starts from 0.
Here we can use loop also but we haven't introduce the concept of loop so i have not written loop over here.Using this you can store collection fo f  data of similar datatypes.
 Thanks for watching this blog. Thanks to all of my viewers.



Made by Great Lord Shri Krishna



Comments