0% found this document useful (0 votes)
74 views5 pages

Pointers and Function: Call by Value

This document discusses pointers and functions in C programming. It covers call by value vs call by reference, using pointers to pass arguments to functions. It also provides examples of using pointers to sort an array using bubble sort and to access array elements through a pointer. Finally, it shows an example of a pointer to an array that stores the addresses of other variables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views5 pages

Pointers and Function: Call by Value

This document discusses pointers and functions in C programming. It covers call by value vs call by reference, using pointers to pass arguments to functions. It also provides examples of using pointers to sort an array using bubble sort and to access array elements through a pointer. Finally, it shows an example of a pointer to an array that stores the addresses of other variables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Pointers and function:

Call by value:

/* Include <stdio.h>
void main()
{
int x,y;
x=20;
y=30;
printf(“n Value of a and b before function call =%d %d”,a,b);
fncn(x,y);
printf(“n Value of a and b after function call =%d %d”,a,b);
}

fncn(p,q)
int p,q;
{
p=p+p;
q=q+q;
}

Call by Reference:

/* example of call by reference*?

/* Include <stdio.h>
void main()
{
int x,y;
x=20;
y=30;
printf(“n Value of a and b before function call =%d %d”,a,b);
fncn(&x,&y);
printf(“n Value of a and b after function call =%d %d”,a,b);
}

fncn(p,q)
int p,q;
{
*p=*p+*p;
*q=*q+*q;
}
Bubble sort in pointers

#include <stdio.h>

int arr[10] = { 3,6,1,2,3,8,4,1,7,2};

void bubble(int a[], int N);

int main(void)

int i;

putchar('\n');

for (i = 0; i < 10; i++)

printf("%d ", arr[i]);

bubble(arr,10);

putchar('\n');

for (i = 0; i < 10; i++)

printf("%d ", arr[i]);

return 0;

}
void bubble(int a[], int N)

int i, j, t;

for (i = N-1; i >= 0; i--)

for (j = 1; j <= i; j++)

if (a[j-1] > a[j])

t = a[j-1];

a[j-1] = a[j];

a[j] = t;

Pointer to arrays:

/* A program to display the contents of array using pointer*/


main()
{
int a[100];
int i,j,n;
printf(“nEnter the elements of the arrayn”);
scanf(“%d”,&n);
printf(“Enter the array elements”);
for(I=0; I < n; I++)
scanf(“%d”,&a[I]);
printf(“Array element are”);
for(ptr=a; ptr < (a+n); ptr++)
printf(“Value of a[%d]=%d stored at address %u”,j+=,*ptr,ptr);
}

#include

int main()

printf("size of a short is %d\n", sizeof(short));

printf("size of a int is %d\n", sizeof(int));

printf("size of a long is %d\n", sizeof(long));

#include <stdio.h>
#include <conio.h>
main() {
  clrscr();
  int *array[3];
  int x = 10, y = 20, z = 30;
  int i;
  array[0] = &x;
  array[1] = &y;
  array[2] = &z;
  for (i=0; i< 3; i++) {
    printf("The value of %d= %d ,address is %u\t \n", i, *(array[i]),
        array[i]);
  }
  getch();
  return 0;
}

You might also like