Beruflich Dokumente
Kultur Dokumente
Pointers
Structure:
3.1 Introduction
3.2 Pointer variable
3.3 Accessing Variables Through Pointers
3.4 Pointer declaration and definition
3.5 Initialization of pointer variables
3.6 Pointers and Functions
3.7 Pointers to Pointers
3.8 Compatibility
3.9 LValue and RValue
3.10 Arrays and Pointers
3.11 Pointer Arithmetic and Arrays
3.12 Passing an array to function
3.12.1 Understanding Complex Declarations
3.13 Memory Allocation Functions
3.13.1 Malloc
3.13.2 Calloc
3.13.3 Realloc
3.13.4 Free
3.1 Introduction
A pointer is a derived data type. This is the one which stores the address of data in
memory, we will be in position to access the data directly and do calculations over it. The
standard concept is, we access the data from memory using variable name it gets the
data and operations are done over them. But the pointer is different that the accessing is
done by address the data is stored so that it will be advantage of decreasing the
instructions and overheads of standard usage. This is very specific because every data
has its own memory space and we are working over it. We are able to store the address
in a variable, which is only done only using & operator. The one thing to remember here
is when we execute the program once we get one type of address and this will be
changed to other when we execute it other time.
Having pointer constant and pointer value, we can pointer variable which is the thing that
contains address of variable. Here we see how the representation is done between
pointer variable and data, we represent data a with pointer variable p. So we can access
the data with pointer variable easily and we not need the direct representation of data
whenever it is used.
address of
variable a
a
-123 123450 a -123
&a stored in
variable Pointer
variable
P 123450 p
The actual usage of pointers comes here, that accessing of variables using pointers. We
have the capability of accessing the variables and make the operations over that. The
operator that gets the value from pointer variable is * (indirection operator). This is called
the reference to pointer.
P=&a
So the pointer p has address of a and the value that that cointained in that address can
be accessed by : *p
Inverse
& *
3.4 Pointer declaration and definition
Pointer declaration is done using the indirection operator; it is then followed by data type
we are using to store in it and name of pointer variable. It can be shown as below
type identifier
*
The pointer type can be of any data type we are in use of storing in it. It can be of int,
char, float etc.
The one thing we clearly know is C do not have the capability of initializing variable, the
thing that is done here is operating system takes care of all uninitialised variables. The
garbage value will be collected in these variables till they are initialized. The same thing
happens in the pointers also, the pointer contain a value the could not be a valid address
location. So the removal of this is mandatory and if the address it has is valid it leads to
a run time error. So initializing a pointer variable is a important thing, it is done as follows
int x;
int *p = &x;
Here the two steps are done at once the two steps are
int *p;
*p=&x;
The combination of these two steps gives us the initialization of pointers. So to avoid the
run time errors we can initialize it with null. It can be as
int *p = NULL;
We have the flexibility of using pointers in different ways, we have the capability of
assigning the one pointer variable to different variable and we can do operations over
them. The other is we can assign different pointers to one variable and do operations
over it.
3.6 Pointers and Functions
We get a clear usage of pointers in the working with functions. We have a clear idea of
usage of functions, the thing done here is the values of variables are directly changed
and replicated in calling function, which is said as call by value. The new concept comes
into picture using pointers that are call by reference, the thing done here is passing a
pointer to a variable. The pass by value concept is as follows:
void exchange (int x, int y);
int main (void) {
int a =5;
int b = 7;
exchange (a,b);
printf (“%d %d\n”, a,b);
return 0;
}
Here the exchange function does not work when we go for call by reference it is done as
follows;
void exchange (int *, int *); void exchange (int *x, int *y) {
int main (void) { int temp;
int a=5; temp=*x;
int b=7; *x=*y;
exchange(&a,&b); *y=temp;
printf(“%d %d”,a,b); return;
return 0; }
}
Here the exchange gets worked because we used a pointer for referencing variable in a
function.
exchange (int *, int *)
So that pointers are directly dealt with data address and so it is easy to represent
variable and operations over it.
If we need of sending back more than one value from a function, we can use pointers.
By passing the address of variables defined in main, or any other function that calls, we
can store data directly in the calling function rather than using return. Now we shall see
the functions that returning pointer:
int *smaller (int *p1, int *p2);
int main (void) a b
….
int a;
int b;
int *p; p
… &a or &b
scanf(%d %d”, &a,&b);
p=smaller(&a,&b);
px py
The concept of pointer to pointer too has a lot of functionalities and many usages over
this concept. This concept says that we can point a pointer to pointer that is pointer
concerned to variable will have address of that variable and that pointer is pointed again
to that pointer. So that pointer with address of variable is stored in other pointer. We can
use in different operations. The indirection does not have limit but it is considerable only
up to two levels. If we take a variable a, this is pointed by pointer P and this p is pointed
by Q, we have the capability that
int a;
int *p;
int **q;
These are taken in program and if we give this
printf(“%d”, a);
printf(“%d”, *p);
printf(“%d”, **q);
All these give the same value that is the value of a. This can be represented as:
q p a
3.8 Compatibility
As we know that though the pointers are derived data types, they have specific data type
as the general variables and we cannot assign one type of variable to different data type
variable.
Casting:
We say the thing is we do not have the capability of assigning one data type to other.
The flexibility of this can be got by defining it as void or by casting which is done as
below
int a;
char *p;
p=(char *)a this is what the casting is.
Here the int a is changed to char type and it is assigned to p. By using void we can do it
as follows
void *pVoid;
char *pChar;
int *pInt;
pVoid=pChar;
pInt=pVoid;
pInt=(int *)pChar;
This is the thing we can do casting.
In every expression we have lvalue and rvalue. Lvalue is the one that gets the value
and rvalue is the one that operations done on some variables
1. An lvalue expression must be used whenever the object is receiving a value; that
is, it is being modified.
2. An rvalue expression can be used to supply a value for further use; that is, to
examine or copy its value.
So we have come across what the variables to be on left and which to be on right. Any
expression we build will fall into any type of above.
3.10 Arrays and Pointers
Arrays and pointers have a close relationship, we say it by the fact that the name of
array is the pointer constant to the first element. When the array name is dereferenced, it
refers to the only first element, not the whole array. We can say it by:
int a[5];
printf (“%d %d”, &a[0],a);
Here the output is same for two variables. Any how we can say this by making pointer
assigned to array, we can store this pointer in a pointer variable. We can make it as
below:
#include<stdio.h>
int main(void) {
int a[5]={2,4,6,7,8}
int *p=a;
int i=0;
…..
printf(“%d %d”, a[i],*p);
…….
return 0;
}
So this two outputs are one and same. We have print them by first using index notation
and then pointer notation. We can do it by intialising pointer to any of member of array.
So if we initialize as that we can access in two different ways using the pointers onto the
same array.
3.11 Pointer Arithmetic and Arrays
A 2 p-1
A+1 p
4
A+2 p+1
6
A+3 p+2
8
A+4 p+3
10
As we know that the name of array is the pointer constant, so when we pass the array,
we do not pass the address operator. So the typical will be as:
doIt (aryName);
We have the usage of complicated declarations and their interpretation has become
difficult for usage. So for interpretation we have the formula as
Right-Left rule: Here we first go for things that are right to the identifier and then for left
of identifier. We represent it as below
identifier
6 4 1 3 5
2 Start here
3.13 Memory Allocation Functions
Modern languages have the flexibility of allocating memory at run time. This concept
gives us idea of two concepts, they are static memory allocation and dynamic memory
allocation. Static memory allocation requires that the declaration and definition of
memory be fully specified in source program. This works till we have the capability of
what our requirements are known to us.
Dynamic memory allocation uses predefined functions to allocate and release memory
for data while the program is running. To use this, the programmer must use either
standard data types or already must have declared any derived types. For implementing
dynamic memory we have four management functions. They are malloc,calloc,realloc
and free. We find all these in <stdlib.h> library.
Memory
management
3.13.1 Malloc
The malloc function allocates a block of memory that contains the number of bytes
specified in its parameter. It returns a void ponter to first byte of allocated memory. The
allocated memory is not initialized. You should therefore assume that it will contain
garbage and initialize is required.The prototype is as:
malloc returns address of first byte in memory space, NULL for not successful
3.13.2 Calloc
3.13.3 Realloc
This is inefficient and shouid be used advisebly. Ealloc changes the size of block by
deleting or extending the memory at end of block. If memory is not available it gives
complete new block.The prototype is:
3.13.4 Free
The name itself indicates that it clears the memory given. The prototype is