C Programming Lecture-8 Pointers and Memory Management $p!derLabWeb C Programming Lecture-8 Pointers and Memory Management
What are pointers? Pointers are the special kind of variables which can store the addresses of other variables.
How to declare a pointer? To declare a pointer we use “ * ” symbol. Syntax :- type_name *var_name; Eg. int *a; // pointer to an integer. double *b; // pointer to a double. float *c; // pointer to a float. char *d; // pointer to a character.
Null Pointers Null pointer is the pointer which is pointing to null. Eg. int *a = NULL; double *b = NULL;
How pointers are used? We know pointers are used to store the address of other variables then we have to use the & symbol to get the address of other variable. Suppose a int variable is declared Now declaring the pointer b pointing to a. int *b = &a;
Dynamically Allocating Memory For dynamically allocating memory to a pointer we use malloc function. Prototype :- void *malloc( int size ); Eg:- int *a = (*int) malloc(4 * sizeof(int));
Resizing and Releasing Memory For resizing the size of the pointer we have realloc function. Prototype :- void *realloc(void *ptr, int newsize); And for releasing the memory we have free function. void free (void *address);
Thank you!