0% found this document useful (0 votes)
7 views

C Programming Lesson 7

The document discusses pointers in C programming. It defines pointers as variables that store the address of another variable. It explains that pointers allow programs to dynamically allocate memory and access memory locations indirectly. It also covers declaring and initializing pointers, pointer operators, pointer arithmetic, and functions for dynamic memory allocation like malloc(), calloc(), free(), and realloc().

Uploaded by

rajeev0305
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C Programming Lesson 7

The document discusses pointers in C programming. It defines pointers as variables that store the address of another variable. It explains that pointers allow programs to dynamically allocate memory and access memory locations indirectly. It also covers declaring and initializing pointers, pointer operators, pointer arithmetic, and functions for dynamic memory allocation like malloc(), calloc(), free(), and realloc().

Uploaded by

rajeev0305
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Pointers

 KIIT 2014
Objectives
After completing this lesson, you should be able to do the following:
• Define Pointers
• Use of Pointer address operator
• Explain memory organization of Pointers
• Define size of Pointer
• Define Pointer operator
– Declaration
– Initialization
• Define void Pointer
• Use Pointer arithmetic

 KIIT 2014
Pointer
• Is an extremely powerful programming tool. They can make
some things much easier, help improve your program's
efficiency, and even allow you to handle unlimited amounts
of data.
• It is also possible to use pointers to dynamically allocate
memory

 KIIT 2014
Pointer
• Pointers are alternate name: they "point" to locations in
memory.
• Pointer is a variable which stores the address of another
variable
• Since Pointer is also a kind of variable , thus pointer itself
will be stored at different memory location.
• Types of Variable
– Simple Variable that stores a value such as integer, float, character
– Complex Variable that stores address of simple variable i.e pointer
variables

 KIIT 2014
C Pointer Operator
Syntax:
<variable_type> *<name>;

Example:
int *points_to_integer;

/* one pointer, one regular int */


int *pointer1, nonpointer1;

/* two pointers */
int *pointer1, *pointer2;

 KIIT 2014
Retrieving Value & Address
#include<stdio.h>
int main()
{
int a = 3;
int *ptr;
ptr = &a;
printf(“%d”,a);
printf(“\n%d”,ptr);
return(0);
}

3
-12

 KIIT 2014
Retrieving Value & Address
#include<stdio.h>
int main()
{
int a = 3;
int *ptr, **pptr;
ptr = &a;
pptr = &ptr;
printf(“%d”,a);
printf(“\n%d”,ptr);
printf(“\n%d”,pptr);
return(0);
}

 KIIT 2014
Memory Organization
• When we use variable in program then Compiler keeps some memory
for that variable depending on the data type
• The address given to the variable is Unique with that variable name
• When Program execution starts the variable name is automatically
translated into the corresponding address.

 KIIT 2014
Size of Pointer Variable
• Pointer is a variable which stores the address of another variable
• Variable may be integer, character, float but the address of the variable is
always integer so Pointer requires 2 bytes of memory in Turbo C Compiler.

#include<stdio.h>
int main()
{
int a = 10, *ptr;
ptr = &a;
printf("\nSize of Integer Pointer : %d",sizeof(ptr));
return(0);
}

Size of Integer Pointer : 2

 KIIT 2014
Pointer Variable
#include <stdio.h>
int main(void)
{
char ch = 'c';
char *chptr = &ch;
int i = 20;
int *intptr = &i;
float f = 1.20000;
float *fptr = &f;
char *ptr = "I am a string";
printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr,
*intptr, *fptr, *ptr, ptr);
return 0;
}

[c], [20], [1.200000], [I], [I am a string]

 KIIT 2014
C Dynamic Memory Allocation
• Dynamic memory allocation allows your program to obtain more memory space
while running, or to release it if it's not required.
• In simple terms, Dynamic memory allocation allows you to manually handle
memory space for your program.
• Although, C language inherently does not have any technique to allocate memory
dynamically, there are 4 library functions under “stdlib.h” for dynamic
memory allocation.

Function Use of Function

Allocates requested size of bytes and returns a pointer first


malloc()
byte of allocated space

Allocates space for an array elements, initializes to zero


calloc()
and then returns a pointer to memory

free() deallocate the previously allocated space

realloc() Change the size of previously allocated space

 KIIT 2014
C malloc()
• The name malloc stands for "memory allocation".
• The function reserves a block of memory of specified size and return a pointer of
type void which can be casted into pointer of any form.

• Syntax of malloc()-

ptr = (cast-type*) malloc(byte-size)

• Example -

ptr = (int*) malloc(100 * sizeof(int));

 KIIT 2014
C calloc()
• The name calloc stands for "contiguous allocation".
• The only difference between malloc() and calloc() is that, malloc() allocates single
block of memory whereas calloc() allocates multiple blocks of memory each of
same size and sets all bytes to zero.

• Syntax of calloc()-
ptr = (cast-type*) calloc(n, element-size)

• Example -
ptr = (float*) calloc(25, sizeof(float));

 KIIT 2014
C free()
• Dynamically allocated memory created with either calloc() or malloc() doesn't get
freed on its own. You must explicitly use free() to release the space.

• Syntax of free()-

free(ptr);

 KIIT 2014
Example for free()-1
• Example -
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;}

 KIIT 2014
Example for free()-2
• Example -
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int));
if(ptr == NULL) { printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}

 KIIT 2014
C realloc()
• If the previously allocated memory is insufficient or more than required, you can
change the previously allocated memory size using realloc().

• Syntax of free()-
Ptr = realloc(ptr, newsize);

 KIIT 2014
Example for realloc()
• Example -
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size of array: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Address of previously allocated memory: ");
for(i = 0; i < n1; ++i)
printf("%u\t",ptr + i);
printf("\nEnter new size of array: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2);
for(i = 0; i < n2; ++i)
printf("%u\t", ptr + i);
return 0;
}

 KIIT 2014
Summary
In this lesson, you should have learned how to:
• Define Pointers
• Use of Pointer address operator
• Explain memory organization of Pointers
• Define size of Pointer
• Define Pointer operator
– Declaration
– Initialization
• Define void Pointer
• Use Pointer arithmetic

 KIIT 2014

You might also like