-->

program to understand call by value and call by reference

using codeblocks
--------------------------------------------------------------------------------
// call by value reference
#include <stdio.h>
void callbyref(int*, int*);             /* function Prototype with two pointer as parameter*/

int main()                            /* Main function */
{
  int x = 10, y = 20;                 //variable declaration

    printf("before calling\n");                         /* actual arguments will be altered */
  printf("x=%d,y=%d\n",x,y);        //value display before call
  callbyref(&x, &y);              // passing address
  printf("after calling\n");
  printf("x= %d, y= %d\n", x, y);   //displaying value after calling
  return 0;
}

void callbyref(int *m, int *n)
{
  int t;
  t = *m;
  *m = *n;
  *n = t;                       //swapping being done
}


----------------------------------------------------------------------------------------------
using turbo c++
-----------------------------------------------------------------------------------------
// call by value reference
#include <stdio.h>
#include<conio.h>
void callbyref(int*, int*);             /* function Prototype with two pointer as parameter*/

int main()                            /* Main function */
{
  int x = 10, y = 20;                 //variable declaration

    printf("before calling\n");                         /* actual arguments will be altered */
  printf("x=%d,y=%d\n",x,y);        //value display before call
  callbyref(&x, &y);              // passing address
  printf("after calling\n");
  printf("x= %d, y= %d\n", x, y);   //displaying value after calling
 getch();
  return 0;
}

void callbyref(int *m, int *n)
{
  int t;
  t = *m;
  *m = *n;
  *n = t;                       //swapping being done
}

program to understand array of pointer

using codeblocks
---------------------------------------------------------------------------------------------------------
//understanding array of pointers  for numbers     //title of program
#include <stdio.h>                    //header file

int main()
{
    int a[4],i;
    int *p[4];                        //pointer to store 4 addresses
    printf("enter array elements\n");
    for(i=0;i<=3;i++)
    {
        printf("enter element for location %d\n",i);
        scanf("%d",&a[i]);           // gets array elements
    }
    printf("the addresses are\n");
    for(i=0;i<=3;i++)
    {
        p[i]=&a[i];                 // stores address of element of array
        printf(" location =%d and address=%d\n",i,p[i]);//prints the address
    }
    printf("the values are\n");
    for(i=0;i<=3;i++)
    {
        p[i]=&a[i];                 // stores address of element of array
        printf(" location =%d and address=%d\n",i,*p[i]);//prints the value
    }
    return 0;
}

---------------------------------------------------------------------------------------
using turbo c++
-----------------------------------------------------------------------------------------
//understanding array of pointers  for numbers     //title of program
#include <stdio.h>                    //header file
#include<conio.h>

int main()
{
    int a[4],i;
    int *p[4];                        //pointer to store 4 addresses
    printf("enter array elements\n");
    for(i=0;i<=3;i++)
    {
        printf("enter element for location %d\n",i);
        scanf("%d",&a[i]);           // gets array elements
    }
    printf("the addresses are\n");
    for(i=0;i<=3;i++)
    {
        p[i]=&a[i];                 // stores address of element of array
        printf(" location =%d and address=%d\n",i,p[i]);//prints the address
    }
    printf("the values are\n");
    for(i=0;i<=3;i++)
    {
        p[i]=&a[i];                 // stores address of element of array
        printf(" location =%d and address=%d\n",i,*p[i]);//prints the value
    }
getch();
    return 0;
}

program to use 2d array/string to display address and values.

using codeblocks
---------------------------------------------------------------------------------------------------
//understanding array of pointers  for strings     //title of program
#include <stdio.h>                    //header file
int main()
{
     char *a[4];                      //pointer to store 4 addresses
    int i;
    char naam[4][6];                   //2d array of string
    printf("enter array elements\n");
    for(i=0;i<=3;i++)
    {
        printf("enter name for location %d\n",i);
        scanf("%s",naam[i]);                            // gets array elements
    }

    printf("the addresses are\n");
    for(i=0;i<=3;i++)
    {
         a[i]=&naam[i];                                // stores address of element of array
        printf(" location =%d and address=%d\n",i,a+i);//prints the address
    }
    printf("the values are\n");
    for(i=0;i<=3;i++)
    {
        a[i]=&naam[i];                                 // stores address of element of array
        printf(" location =%d and name=%s\n",i,*(a+i));//prints the stored value
    }
    return 0;
}

//note:-
//1. in 2d string of array, a=naam does not work. We have to work manually by assigning address as shown above.
//we can not print values or addresses using a[i]. it does not work.
-----------------------------------------------------------------------------------------------------
using turbo c++
---------------------------------------------------------------------------------------------------
//understanding array of pointers  for strings     //title of program
#include <stdio.h>                    //header file
#include<conio.h>                  //header file
int main()
{
     char *a[4];                      //pointer to store 4 addresses
    int i;
    char naam[4][6];                   //2d array of string
    printf("enter array elements\n");
    for(i=0;i<=3;i++)
    {
        printf("enter name for location %d\n",i);
        scanf("%s",naam[i]);                            // gets array elements
    }

    printf("the addresses are\n");
    for(i=0;i<=3;i++)
    {
         a[i]=&naam[i];                                // stores address of element of array
        printf(" location =%d and address=%d\n",i,a+i);//prints the address
    }
    printf("the values are\n");
    for(i=0;i<=3;i++)
    {
        a[i]=&naam[i];                                 // stores address of element of array
        printf(" location =%d and name=%s\n",i,*(a+i));//prints the stored value
    }
   getch();
    return 0;
}

//note:-
//1. in 2d string of array, a=naam does not work. We have to work manually by assigning address as shown above.
//we can not print values or addresses using a[i]. it does not work.

program to reverse a string using pointer

using codeblocks
--------------------------------------------------------------------------------------------
//program to reverse a string using pointer//comment of program
#include <stdio.h>                         //header file
#include<string.h>
int main()                              //main function
{
    char name[40];                     //character declaration
    char *p;                          //pointer declaration.
    int length;
    puts("enter string\n");           //display of message
    gets(name);                      // gets data/string
    length=strlen(name);             //stores length os string
    p=name+length;     
 // assigning total address as in given string; it becomes total taken from variable length starting from //0.
    //p=name takes address location 0 to pointer.
    while(p>=name)                 //the value of 'p' decreases from total location  until it reaches 0.
    {
        putchar(*p);                 //prints the value stored in particular location
        p--;                             //decreases the value
    }
    return 0;
}
----------------------------------------------------------------------------------------------
using turbo c++
----------------------------------------------------------------------------------------------
//program to reverse a string using pointer//comment of program
#include <stdio.h>                         //header file
#include<string.h>
#include<conio.h>
int main()                              //main function
{
    char name[40];                     //character declaration
    char *p;                          //pointer declaration.
    int length;
    puts("enter string\n");           //display of message
    gets(name);                      // gets data/string
    length=strlen(name);             //stores length os string
    p=name+length;     
 // assigning total address as in given string; it becomes total taken from variable length starting from //0.
    //p=name takes address location 0 to pointer.
    while(p>=name)                 //the value of 'p' decreases from total location  until it reaches 0.
    {
        putchar(*p);                 //prints the value stored in particular location
        p--;                             //decreases the value
    }
getch();
    return 0;
}

program to reverse a string using pointer.

using codeblocks
------------------------------------------------------------------------------------------
//program to reverse a string using pointer//comment of program
#include <stdio.h>                         //header file

int main()                              //main function
{
    char name[40]="ramesh Shrestha";  //assignment of string
    char *p;                          //pointer declaration
    p=name+15;  //p=name takes address location 0 to pointer.

  // assigning total address as in given string; it becomes 15 starting from 0
    while(--p>=name)                 //the value of 'p' decreases from location 15 until it reaches 0.
    {
        putchar(*p);                 //prints the value stored in particular location
    }
    return 0;
}
 ----------------------------------------------------------------------------------------------------

using turboc++
--------------------------------------------------------------------------------------------

//program to reverse a string using pointer//comment of program
#include <stdio.h>                         //header file
#include<conio.h>

int main()                              //main function
{
    char name[40]="ramesh Shrestha";  //assignment of string
    char *p;                          //pointer declaration
    p=name+15;       //p=name takes address location 0 to pointer.
                
// assigning total address as in given string; it becomes 15 starting from 0
    while(--p>=name)                 //the value of 'p' decreases from location 15 until it reaches 0.
    {
        putchar(*p);                 //prints the value stored in particular location
    }
getch();
    return 0;
}

storing character in pointe and displaying them

using codeblocks
--------------------------------------------------------------------------------------------------
//understanding array as pointer      //comment of program
//storing character in an array using pointer and displaying them
#include <stdio.h>                     //header file
int main()
{
    char *p;              //character array pointer
    char  a[5];           //CHARACTER ARRAY
    for(p=a;p<a+5;p++)
    {
     printf("address=%d\n",p);// prints 5 addresses

    }
       for(p=a;p<a+5;p++)   //runs the loop from 0 location to 4th location
       {
        *p=getchar();       //gets character
        putchar(*p);        //prints that character
    }

        return 0;
}

/*note:-
p=a------- it takes address of zeroth element
a+5-----------means upto 5th location
p++--------increase the location by 1
----------------------------
while inputting character,
    It accepts one character, then that is replaced by next address.
    Then it is replaced by next address.
    So altogether, we input three characters.
    */
----------------------------------------------------------------------------------------------------------
using turbo c++
-------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
//understanding array as pointer      //comment of program
//storing character in an array using pointer and displaying them
#include <stdio.h>                     //header file
#include<conio.h>
int main()
{
    char *p;              //character array pointer
    char  a[5];           //CHARACTER ARRAY
    for(p=a;p<a+5;p++)
    {
     printf("address=%d\n",p);// prints 5 addresses

    }
       for(p=a;p<a+5;p++)   //runs the loop from 0 location to 4th location
       {
        *p=getchar();       //gets character
        putchar(*p);        //prints that character
    }
       getch();
        return 0;
}

Program to know array as pointer

using codeblocks
----------------------------------------------------------------------------------------------------------
//understanding array as pointer      //comment of program
#include <stdio.h>                     //header file
int main()
{
    int a[]={2,3,4,5,6};                 //array with some values
    int i;
    int *p;
    p=a;                               //assigns base address to pointer
    printf("the address of array=%d\n",p);//prints address of array
    p=&a[0];                            //assigns address of zeroth element
    printf("the address of array=%d\n",p);//prints address of zeroth element
    return 0;
}

//note:- In both cases, we get same address.
//Because computer takes address of array same  as of address of zeroth element.
//So , this array acts as pointer

/*If we want to display all addresses of all elements of an array, then
we write ;click below

https://practisingprograms.blogspot.com/2019/01/program-to-understand-array-as-pointer_12.html


*/