Oop M03
Oop M03
Introduction
xyz variable
50 value
1380 address
Contd.
• During execution of the program, the system
always associates the name xyz with the
address 1380.
– The value 50 can be accessed by using either the
name xyz or the address 1380.
• Since memory addresses are simply numbers,
they can be assigned to some variables which
can be stored in memory.
– Such variables that hold memory addresses are
called pointers.
– Since a pointer is a variable, its value is also stored
in some memory location.
Contd.
int arr[20];
:
&arr;
• Pointing at array name.
&(a+b)
• Pointing at expression.
Example
#include <stdio.h>
main()
{
int a;
float b, c;
double d;
char ch;
• Example:
int *count;
float *speed;
• Once a pointer variable has been declared, it
can be made to point to a variable using an
assignment statement like:
int *p, xyz;
:
p = &xyz;
– This is called pointer initialization.
Things to Remember
#include <stdio.h>
main()
{
int a, b;
int c = 5; Equivalent
int *p;
a = 4 * (c + 5) ;
p = &c;
b = 4 * (*p + 5) ;
printf (“a=%d b=%d \n”, a, b) ;
}
Example 2
#include <stdio.h>
main()
{
int x, y; *&xx
int *ptr;
//add x - 10001, val = 10
x = 10 ;
ptr=&x;
ptr = &x ; &x&*ptr
y = *ptr ;
printf (“%d is stored in location %u \n”, x, &x) ;
printf (“%d is stored in location %u \n”, *&x, &x) ;
printf (“%d is stored in location %u \n”, *ptr, ptr) ;
printf (“%d is stored in location %u \n”, y, &*ptr) ;
printf (“%u is stored in location %u \n”, ptr, &ptr) ;
printf (“%d is stored in location %u \n”, y, &y) ;
*ptr = 25;
printf (“\nNow x = %d \n”, x);
}
Address of x: 3221224908
Output:
Address of y: 3221224904
Now x = 25
Pointer Expressions
Output:
• Three-step algorithm:
1. Read in three integers x, y and z
2. Put smallest in x
• Swap x, y if necessary; then swap x, z if necessary.
3. Put second smallest in y
• Swap y, z if necessary.
Contd.
#include <stdio.h>
main()
{
int x, y, z ;
………..
scanf (“%d %d %d”, &x, &y, &z) ;
if (x > y) swap (&x, &y);
if (x > z) swap (&x, &z);
if (y > z) swap (&y, &z) ;
………..
}
sort3 as a function
#include <stdio.h>
main()
{
int x, y, z ;
………..
scanf (“%d %d %d”, &x, &y, &z) ;
sort3 (&x, &y, &z) ;
………..
}
x &x[0] 2500 ;
• malloc
– Allocates requested number of bytes and returns a
pointer to the first byte of the allocated space.
• calloc
– Allocates space for an array of elements, initializes
them to zero and then returns a pointer to the
memory.
• free
Frees previously allocated space.
• realloc
– Modifies the size of previously allocated space.
Allocating a Block of Memory
• Examples
p = (int *) malloc (100 * sizeof (int)) ;
• A memory space equivalent to “100 times the size of an
int” bytes is reserved.
• The address of the first byte of the allocated memory is
assigned to the pointer p of type int.
p
• Example:
int **p;
p=(int **) malloc(3 * sizeof(int *));
p[0]
p int ** int *
p[1] int *
int *
p[2]
2-D Array Allocation
#include <stdio.h> void read_data(int **p,int h,int w)
#include <stdlib.h> {
int i,j;
int **allocate(int h, int w) for(i=0;i<h;i++)
{ for(j=0;j<w;j++)
int **p; Allocate array scanf ("%d",&p[i][j]);
int i,j; of pointers }
item next
• Such structures which contain a member field
pointing to the same structure type are called
self-referential structures.
Contd.
struct node_name
{
type member1;
type member2;
………
struct node_name *next;
};
Illustration
n2 n3
n1
Example
#include <stdio.h> n1.next = &n2 ;
struct stud n2.next = &n3 ;
{ n3.next = NULL ;
int roll;
char name[30]; /* Now traverse the list and print
int age; the elements */
struct stud *next;
}; p = n1 ; /* point to 1st element */
while (p != NULL)
main() {
{ printf (“\n %d %s %d”,
struct stud n1, n2, n3; p->roll, p->name, p->age);
struct stud *p; p = p->next;
}
scanf (“%d %s %d”, &n1.roll, }
n1.name, &n1.age);
scanf (“%d %s %d”, &n2.roll,
n2.name, &n2.age);
scanf (“%d %s %d”, &n3.roll,
n3.name, &n3.age);