RDoc38062
RDoc38062
1
Basics of Pointers
2
Introduction
A pointer is a variable that represents the location (rather than the value) of a data item.
3
Basic Concept
In memory, every stored data item occupies one or more contiguous memory cells.
• The number of memory cells required to store a data item depends on its type (char, int,
double, etc.).
Whenever we declare a variable, the system allocates memory location(s) to hold the value
of the variable.
• Since every byte in memory has a unique address, this location will also have its own
(unique) address.
4
Example
Consider the statement
1380
int xyz = 50; (xyz) 50
• This statement instructs the compiler to allocate a location for the integer
variable xyz, and put the value 50 in that location.
• The value 50 can be accessed by using either the name xyz or the address
1380.
5
Example (Contd.)
1380
int xyz = 50; 50
(xyz)
int *ptr; // Here ptr is a pointer to an integer
----
ptr = &xyz; (ptr)
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.
6
Pointer Declaration
7
Making it point
8
Accessing the Address of a Variable
The address of a variable can be determined using the „&‟ operator.
• The operator „&‟ immediately preceding a variable returns the address of the variable.
Example:
p = &xyz;
• The address of xyz (1380) is assigned to p.
The „&‟ operator can be used only with a simple variable or an array element.
&distance
&x[0]
&x[i-2]
9
Illegal usages
Following usages are illegal:
&235
• Pointing at constant.
int arr[20];
:
&arr;
• Pointing at array name.
&(a+b)
• Pointing at expression.
10
Pointer Declarations and Types
General form:
data_type *pointer_name;
11
Pointers have types
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.
12
Things to remember
Pointer variables must always point to a data item of the same type.
float x;
int *p;
p = &x; // This is an erroneous assignment
int *count;
count = 1268;
13
Pointer Expressions
Like other variables, pointer variables can be used in expressions.
14
More on pointer expressions
What are allowed in C?
• Add an integer to a pointer.
• Subtract an integer from a pointer.
• Subtract one pointer from another
• If p1 and p2 are both pointers to the same array, then p2–p1 gives the number of
elements between p1 and p2.
15
More on pointer expressions
16
Scale Factor
We have seen that an integer value can be added to or subtracted from a pointer variable.
p = &x[1];
printf( “%d”, *p); // This will print 20
17
More on Scale Factor
struct complex {
float real;
float imag;
};
struct complex x[10];
p = &x[0]; // The pointer p now points to the first element of the array
The increment of p is not by one byte, but by the size of the data type to which p points.
This is why we have many data types for pointers, not just a single “address” data type
18
Pointer types and scale factor
Data Type Scale Factor
char 1
int 4
float 4
double 8
19
Scale factor may be machine dependent
• The exact scale factor may vary from one machine to another.
• Can be found out using the sizeof function.
#include <stdio.h>
main( )
{
printf (“No. of bytes occupied by int is %d \n”, sizeof(int));
printf (“No. of bytes occupied by float is %d \n”, sizeof(float));
printf (“No. of bytes occupied by double is %d \n”, sizeof(double));
printf (“No. of bytes occupied by char is %d \n”, sizeof(char));
}
Output:
Number of bytes occupied by int is 4
Number of bytes occupied by float is 4
Number of bytes occupied by double is 8
Number of bytes occupied by char is 1
20
Passing Pointers to a Function
Pointers are often passed to a function as arguments.
• Allows data items within the calling program to be accessed by the function, altered,
and then returned to the calling program in altered form.
• Called call-by-reference (or by address or by location).
21
Passing arguments by value or reference
#include <stdio.h> #include <stdio.h>
main( ) main( )
{ {
int a, b; int a, b;
a = 5; b = 20; a = 5; b = 20;
swap (a, b); swap (&a, &b);
printf (“\n a=%d, b=%d”, a, b); printf (“\n a=%d, b=%d”, a, b);
} }
void swap (int x, int y)
void swap (int *x, int *y)
{
{
int t; int t;
t = x; x = y; y = t;
Output t = *x; *x = *y; *y = t; Output
}
}
a=5, b=20 a=20, b=5
22
Pointers and Arrays
23
Example
• Suppose that the base address of x is 2500, and each integer requires 4 bytes.
24
Example (contd)
Both x and &x[0] have the value 2500.
p = x; and p = &x[0]; are equivalent
• We can access successive values of x by using p++ or p-- to move from one element to
another.
25