Chapter 14 Pointers
Chapter 14 Pointers
POINTERS
3
© Oxford University Press 2016. All rights reserved.
UNDERSTANDING THE COMPUTER’S MEMORY contd.
• Global Memory- The block of code that is the main() program (along with other
functions in the program) is stored in the global memory. The memory in the global
area is allocated randomly to store the code of different functions in the program in
such a way that one function is not contiguous to another function. Besides, the
function code, all global variables declared in the program are stored in the global
memory area.
• Other Memory Layouts- C provides some more memory areas like- text segment,
BSS and shared library segment.
• The text segment is used to store the machine instructions corresponding to the
compiled program. This is generally a read-only memory segment
• BSS is used to store un-initialized global variables
• Shared libraries segment contains the executable image of shared libraries that are
being used by the program.
© Oxford University Press 2016. All rights reserved.
INTRODUCTION
• Every variable in C has a name and a value associated with it. When a variable is
declared, a specific block of memory within the computer is allocated to hold the
value of that variable. The size of the allocated block depends on the type of the
data.
int x = 10;
• When this statement executes, the compiler sets aside 2 bytes of memory to hold
the value 10. It also sets up a symbol table in which it adds the symbol x and the
relative address in memory where those 2 bytes were set aside.
• Thus, every variable in C has a value and an also a memory location (commonly
known as address) associated with it. Some texts use the term rvalue and lvalue for
the value and the address of the variable respectively.
• The rvalue appears on the right side of the assignment statement and cannot be
used on the left side of the assignment statement. Therefore, writing 10 = k; is
illegal.
© Oxford University Press 2016. All rights reserved.
DECLARING POINTER VARIABLES
• Actually pointers are nothing but memory addresses.
• A pointer is a variable that contains the memory location of another variable.
• The general syntax of declaring pointer variable is
data_type *ptr_name;
Here, data_type is the data type of the value that the pointer will point to. For
example:
int *pnum; char *pch; float *pfnum;
int x= 10;
int *ptr = &x;
The '*' informs the compiler that ptr is a pointer variable and the int specifies that it
will store the address of an integer variable.
The & operator retrieves the lvalue (address) of x, and copies that to the contents of
the pointer ptr.
© Oxford University Press 2016. All rights reserved.
DE-REFERENCING A POINTER VARIABLE
• We can "dereference" a pointer, i.e. refer to the value of the variable to which it points by using
unary '*' operator as in *ptr. That is, *ptr = 10, since 10 is value of x.
#include<stdio.h>
int main()
{
int num, *pnum;
pnum = #
printf(“\n Enter the number : “);
scanf(“%d”, &num);
printf(“\n The number that was entered is : %d”, *pnum);
return 0;
}
OUTPUT:
Enter the number : 10
The number that was entered is : 10
9
© Oxford University Press 2016. All rights reserved.
NULL POINTERS
A null pointer which is a special pointer value that is known not to point anywhere.
This means that a NULL pointer does not point to any valid memory address.
To declare a null pointer you may use the predefined constant NULL,
int *ptr = NULL;
You can always check whether a given pointer variable stores address of some
variable or contains a null by writing,
if ( ptr == NULL)
{ Statement block;
}
Null pointers are used in situations if one of the pointers in the program points
somewhere some of the time but not all of the time. In such situations it is always
better to set it to a null pointer when it doesn't point anywhere valid, and to test to
see if it's a null pointer before using it.
• A generic pointer is pointer variable that has void as its data type.
• The generic pointer, can be pointed at variables of any data type.
• It is declared by writing
void *ptr;
• You need to cast a void pointer to another kind of pointer before using it.
14
© Oxford University Press 2016. All rights reserved.