0% found this document useful (0 votes)
3 views6 pages

C Unit 4

Pointers are memory variables that store addresses of other variables, allowing for efficient memory management and dynamic memory allocation. They can be declared using an asterisk (*) and are used in functions like malloc, calloc, and realloc for memory allocation. Static memory allocation requires predefined sizes, while dynamic memory allocation allows for flexibility during program execution.

Uploaded by

Harsha Mannem
Copyright
© © All Rights Reserved
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)
3 views6 pages

C Unit 4

Pointers are memory variables that store addresses of other variables, allowing for efficient memory management and dynamic memory allocation. They can be declared using an asterisk (*) and are used in functions like malloc, calloc, and realloc for memory allocation. Static memory allocation requires predefined sizes, while dynamic memory allocation allows for flexibility during program execution.

Uploaded by

Harsha Mannem
Copyright
© © All Rights Reserved
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
You are on page 1/ 6

Pointers:

A pointer is a memory variable that stores a memory address of another


variable. A pointer can

have any name that is legal for other variable and it is declared in the
same fashion like other

variables but is always denoted by an asterisk (*) operator.

Features of pointers:

• Pointers save the memory space.

• Execution time with pointer is faster because data is manipulated with


the address, i.e.,

direct access to memory location.

• Pointers are used to allocate memory dynamically.

• The memory is accessed efficiently with the pointers. The pointer


assigns the memory

space and also releases it.

• Pointers are used for file handling.

Pointer Declaration:

Pointer variables are declared as follows:

Syntax: datatype* identifier;

(or)

datatype * identifier;

Example: int* a;

float* f;

char* y;

Program:

#include<stdio.h>

int main()

int a;
int* pa;

a = 10;

pa = &a;

printf(“\n %u”, pa);

printf(“\n %u”, &a);

printf(“\n %d”, a);

printf(“\n %d”, *pa);

printf(“\n %d”, *(&a));

printf(“\n %u”, &pa);

Output:

2000

2000

10

10

10

3000

Static Memory Allocation:

Static memory allocation requires that the declaration and definition of


memory be fully

specified in the source program. The number of bytes reserved cannot be


changed during run

time.

Dynamic Memory Allocation:

Dynamic memory allocation uses predefined functions to allocate and


release memory for data

while the program is running. It effectively postpones the definition, but


not the data

declaration, to run time.


Memory allocation functions:

Four memory management functions are used with dynamic memory.


Three of them, malloc,

calloc and realloc are used for memory allocation. The fourth, free(), is
used to return memory

when it is no longer needed. All the memory management functions are


found in the standard

library file (stdlib.h) and alloc.h.

Block Memory allocation (malloc):

The malloc function allocates a block of memory that contains the number
of bytes specified in

its parameter. It returns a void pointer to the first byte of the allocated
memory. If it is not

successful it returns a NULL pointer. An attempt to allocate memory from


heap when memory

is insufficient it is known as overflow. If we call malloc with a size zero, the


results are

unpredictable. It may return a NULL pointer, or it may return some other


implementation-

dependent value.

The malloc function declaration is shown below:

Pointer = (data_type*) malloc(size);

Example: ptr = (int*) malloc(20);

Here, in this declaration 20 bytes are allocated to pointer variable ptr of


type int and base

address is returned to pointer ptr.

Program:

// Program to calculate the sum of n numbers entered by the user


#include <stdio.h>

#include <stdlib.h>

int main()

int n, i, *ptr, sum = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

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

// if memory cannot be allocated

if(ptr == NULL)

printf("Error! memory not allocated.");

exit(0);

printf("Enter elements: ");

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

scanf("%d", ptr + i);

sum += *(ptr + i);

printf("Sum = %d", sum);

return 0;

Output:

Enter number of elements: 5

Enter elements: 6 5 4 3 2

Sum = 20

Contiguous Memory allocation (calloc):


The second memory allocation function, calloc, is primarily used for
allocating multiple blocks

of memory. It is used to allocate memory for arrays and structures.

The calloc function declaration is shown below:

Pointer = (data_type*) calloc(count, size);

Example: ptr = (int*) calloc(4, 2);

The above declaration allocates four blocks of memory; each block of


containing 2 bytes. The base

address is stored in the integer pointer.

// Program to calculate the sum of n numbers entered by the user

#include <stdio.h>

#include <stdlib.h>

int main()

int n, i, *ptr, sum = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

ptr = (int*) calloc(n, sizeof(int));

if(ptr == NULL)

printf("Error! memory not allocated.");

exit(0);

printf("Enter elements: ");

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

scanf("%d", ptr + i);

sum += *(ptr + i);


}

printf("Sum = %d", sum);

return 0;

Output:

Enter number of elements: 5

Enter elements: 6 5 4 3 2

Sum = 20

Reallocation of memory (realloc):

This function reallocates the main memory. This is needed as and when
allocated memory is different

from the required memory. It returns the address of the reallocated block,
which can be different from

the original address. If the block cannot be reallocated or the size is 0,


realloc() returns NULL.

The realloc function declaration is shown below:

Pointer = (data_type*) realloc(void* ptr, size);

Example: str = (char*) realloc(str, 30);

You might also like