Professional Documents
Culture Documents
using C
Course Code : CSE181106
MODULE : 10 : Pointers
( Lecture – 3 )
Function Name
//pass by reference
#include<stdio.h>
void set(int *ptr)
{
*ptr = 0;//set the argument value to 0
printf("Set() : Inside set function\n");
printf("a = %d\n",*ptr);
printf("&a = %d\n\n",ptr);
}
Prepared By - Gautam Nath , Asst Professor , BVEC , Karimganj , Assam 5
int main()
{
int a = 10;//set the argument value to 10
printf("Main() : Before calling set function\n");
printf("a = %d\n",a);
printf("&a = %d\n\n",&a);
set(&a);
printf("Main() : After calling set function\n");
printf("a = %d\n",a);
printf("&a = %d\n\n",&a);
return 0;
}
#include<stdio.h>
int main()
{
int age[5]={17,19,18,20,21}; /* compile time or static array allocation *
we can't change the array size. * it is always 5. */
return 0;
}
If
data size decreases then , we have to reduce the age array size
manually every time to avoid wastage of memory.
Prepared By - Gautam Nath , Asst Professor , BVEC , Karimganj , Assam 9
Dynamic Memory Allocation :
int main()
{
int *ptr,size,i;
printf("\nEnter the required size of array :");
scanf("%d",&size);
ptr = malloc(size * sizeof(int));
if(ptr != NULL)
{
//let's get input from user and print it
printf("Enter numbers\n");
for(i = 0; i < size; i++)
scanf("%d",ptr+i);
Prepared By - Gautam Nath , Asst Professor , BVEC , Karimganj , Assam 12
//printing values
printf("The numbers are\n");
for(i = 0; i < size; i++)
printf("%d\n",*(ptr+i)); // *(ptr+i) is as same as
ptr[i]
}
OUTPUT
return 0;
} Enter the required size of array :3
Enter numbers
21
32
42
The numbers are
21
32
42
int main()
{
int *ptr,n,i;
printf("\nEnter the required size of array :");
scanf("%d",&n);
ptr = calloc(n,sizeof(int));
if(ptr != NULL)
{
//let's get input from user and print it
printf("Enter numbers\n");
for(i = 0; i < n; i++)
scanf("%d",ptr+i);
Prepared By - Gautam Nath , Asst Professor , BVEC , Karimganj , Assam 16
//printing values
printf("The numbers are\n");
for(i = 0; i < n; i++)
printf("%d\n",*(ptr+i)); // *(ptr+i) is as same as
ptr[i]
}
OUTPUT
return 0;
Enter the required size of array :3
Enter numbers
41
23
37
The numbers are
41
23
37