Professional Documents
Culture Documents
Lect-10-Pointers Memory Allocations
Lect-10-Pointers Memory Allocations
POINTER TO POINTERS
MEMORY ALLOCATIONS
Pointers
Pointer is one of the powerful feature of C language
programming
Pointers are used in C
program to access the
memory and manipulate
the address
Figure on the side:
Pointer “a” pointing to the
memory address associated
with variable “b”
Both pointers & non-pointers;
this need not be the case
Pointer (2/2)
Pointer is a programming language data type
whose value "points to" another value stored
elsewhere in the computer memory using its address
A pointer must now the data type its points to,
therefore it must be declared similar to data type
Several languages support some type of pointer,
but may have restrictions on theirs
Example in a book:
a page number in a book's index could be
considered a pointer to the corresponding page
Example of pointer
Take a look at the following programs
Example-of-pointer.c
Explanation of program and figure
•Code int *pc, p; creates a pointer pc and a variable c.
Pointer pc points to some address and that address has garbage value. Similarly,
variable c also has garbage value at this point.
•Code c=22; makes the value of c equal to 22, i.e.,22 is stored in the memory
location of variable c.
•Code pc=&c; makes pointer, point to address of c.
Note that, &c is the address of variable c (because c is normal variable) and pc is
the address of pc (because pc is the pointer variable). Since the address of pc and
address of c is same, *pc (value of pointer pc) will be equal to the value of c.
•Code c=11; makes the value of c, 11.
Since, pointer pc is pointing to address of c. Value of *pc will also be 11.
•Code *pc=2; change the address pointed by pointer pc to change to 2.
Since, address of pointer pc is same as address of c, value of c also changes to 2.
• Code int *pc, p; creates a pointer pc and a variable c. Pointer pc points to
some address and that address has garbage value. Similarly, variable c
also has garbage value at this point.
• Code c=22; makes the value of c equal to 22, i.e.,22 is stored in the
memory location of variable c.
• Code pc=&c; makes pointer, point to address of c. Note that, &c is the
address of variable c (because c is normal variable) and pc is the address
of pc (because pc is the pointer variable). Since the address of pc and
address of c is same, *pc (value of pointer pc) will be equal to the value of
c.
• Code c=11; makes the value of c, 11. Since, pointer pc is pointing to
address of c. Value of *pc will also be 11.
• Code *pc=2; change the address pointed by pointer pc to change to 2.
Since, address of pointer pc is same as address of c, value of c also
changes to 2.
Output of example-to-pointer.c
1. Address of c: 2686784
2. Value of c: 22
3. Address of pointer pc: 2686784
4. Content of pointer pc: 22
5. Address of pointer pc: 2686784
6. Content of pointer pc: 11
7. Address of c: 2686784
8. Value of c: 2
Pointers
Since a pointer is a variable which contains the
address in memory of another variable a pointer
can be of any variable type
C uses pointers a lot for:
producing compact and efficient code.
providing a very powerful tool for data manipulations
ptr=(cast-type*)malloc(byte-size);
Example:
ptr=(int*)malloc(100*sizeof(int));
ptr=(cast-type*)calloc(n,element-size);
Example:
ptr=(float*)calloc(25,sizeof(float));
free(ptr);
ptr=realloc(ptr,newsize);
Pointer to pointer (1/3)
We have learnt about a pointer to a variable
We have new concept of a pointer pointing to a
pointer to a variable
Pointer to pointer (2/3)
So we can see that in memory, pointer p1 holds the
address of pointer p2. Pointer p2 holds the address
of character ‘ch’
So ‘p2′ is pointer to character ‘ch’, while ‘p1′ is
pointer to ‘p2′ or we can also say that ‘p2′ is a
pointer to pointer to character ‘ch’
Pointer to pointer (3/3)
Pointer declaration for the above problem:
char *p2 = &ch;
char **p1 = &p2;
Example of pointer to pointer
#include <stdio.h> // file pointer-to-pointer.c
void main(void)
{
char **ptr = NULL;
char *p = NULL;
char c = 'd';
p = &c;
ptr = &p;
printf("\n c = [%d]\n",c);
printf("\n *p = [%d]\n",*p);
printf("\n **ptr = [%d]\n",**ptr);
}
Memory allocation for matrix
Example program:
Static memory allocation matrix-2-static.c
Dynamic memory allocation matrix-3-pointer.c
Program and operating system
One of the task of an operating system (OS) is to
manage memory
When a program requires more memory, it will ask
the OS for the required memory:
OS provide the needed memory if available
OS return error if the needed memory is unavailable
Network sockets
Database handles
Windows-user interaction
Always do free() when pointer not used from
malloc()
Assignment
Create program to multiply array1 and array2
Ask the size of the array
Allocate the memory for the array using malloc
Ask for the array members
Multiply the array
Display the results