Beruflich Dokumente
Kultur Dokumente
Pointers
A pointer is a derived data type. It is built in from one of the fundamental data type
available in c. Pointers contains memory addresses as their values. Pointers are used for the
following benefits to the programmers.
They are
Pointers are more efficient in handling arrays and data tables.
Pointers can be used to return multiple values from a function via function arguments.
Pointer reduces length and complexity of program.
They increase the execution speed.
Pointers allow C to support dynamic memory management.
It is widely used in data structure.
The use of pointer arrays to character strings results in saving of data storage space in
memory.
Accessing the address of a variable:The operator & is used to access the address of the variable associated with it.
E.g., p=&quantity;
Declaring pointer variable:The declaration of the pointer variables takes the following form: data_types *var-name;
example:- int*p;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to designate a variable as
a pointer. Some of the valid pointer declarations are
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory address. The
only difference between pointers of different data types is the data type of the variable or
constant that the pointer points to.
Initialization of pointer variable:A process of assigning the address of a variable to the pointer variable is known as
initialization.
Example:int quantity;
Int *p;
P=&quantity;
Initialization of Pointer can be done using following 4 Steps
1. Declare a Pointer Variable and Note down the Data Type.
2. Declare another Variable with Same Data Type as that of Pointer Variable.
3. Initialize Ordinary Variable and assign some value to it.
4. Now Initialize pointer by assigning the address of ordinary variable to
pointer variable.
E.g.,
#include<stdio.h>
int main()
{
int a;
// Step 1
// Step 3
// Step 4
return(0);
}
{
*rem=*temp%10;
*temp=*temp/10;
*rev=(*rev)*10+*rem;
}
printf(Reverse of %d is %d,a,*rev);
getch();
}
Chain of Pointers
A pointer to a pointer is a form of multiple indirection, or a chain of pointers.
Normally, a pointer contains the address of a variable. When we define a pointer to a pointer,
the first pointer contains the address of the second pointer, which points to the location that
contains the actual value as shown below.
y=*p1**p2;
sum=sum+*p1;
z= 5* - *p2/p1; same as (5*(-(*p2)))/ (p1)
*p2= *p2 + 10;
C allows us to add integers to or subtract integers from pointers as well as to subtract
one pointer from the other. We can also use short hand operators with the pointers p1+=;
sum+=*p2; etc.
we can also compare pointers by using relational operators the expressions such as p1
>p2 , p1==p2 and p1!=p2 are allowed.
/*Program to illustrate the pointer expression and pointer arithmetic*/
#include< stdio.h >
#include< stdio.h >
main()
{
int a, b, *p1, *p2, x, y, z;
a = 12;
b = 4;
p1 = &a;
p2 = &b;
x = *p1 * *p2 - 6;
y = 4* - *p2 / *p1 + 10;
printf("Address of a = %u\n", p1);
printf("Address of b = %u\n", p2);
printf("\n");
printf("a = %d, b = %d\n", a, b);
printf("x = %d, y = %d\n", x, y);
*p2 = *p2 + 3;
*p1 = *p2 - 5;
z
= *p1 * *p2 - 6;
printf("\na = %d, b = %d,", a, b);
printf(" z = %d\n", z);
}
Output
Address of a = 4020
Address of b = 4016
a = 12, b = 4
x = 42, y = 9
a = 2, b = 7, z = 8
1 byte
Integers
2 bytes
Floats
4 bytes
long integers
4 bytes
Doubles
8 bytes
The number of bytes used to store various data types depends on the system and can
be found by making use of the sizeof operator. For example, if x is a variable, then sizeof(x)
returns the number of bytes needed for the variable.
1)for example a series of three variables of short type are declared and a pointer is stored with
the
address
of
a
variable
short a=10,b=20,c=30;
short *p=&c;
Here p is the pointer to the variable c, holds the address of c 65520. Now p+1 gives the
address of next variable b 65522 and p+2 gives the address of next variable a 65524.
#include<stdio.h>
int main()
{
short a=10,b=20,c=30;
short *p=&c;
printf("\np %u",p);
printf("\np+1 %u",p+1);
printf("\np+2 %u",p+2);
return 0;
}
Output:
p
65520
p+1
65522
p+2 65524
By this the value of short pointer increments by 2 on adding 1 to the pointer.
2)If we take a float pointer then increments by 4 on adding 1 to the pointer.
#include<stdio.h>
int main()
{
float a=10.34,b=20.56,c=30.44;
float *p=&c;
printf("p %u",p);
printf("\np+1 %u",p+1);
printf("\np+2 %u",p+2);
return 0;
}
Output:
p
p+1
p+2 65522
Rules for using Pointer :
1.
Pointer Variable Can be Assigned the address of another Variable
65514
65518
int main()
{
int num = 10;
int *ptr;
ptr = #
return(0);
}
2.
Pointer Variable Can be Assigned the value of another Pointer Variable
int *sptr,*tptr;
sptr = #
tptr = sptr;
3.
4.
5.
6.
7.
8.
9.
Here variable arr will give the base address, which is a constant pointer pointing to the
element, arr[0]. Therefore arr is containing the address of arr[0] i.e 1000.
arr is equal to &arr[0] // by default
We can declare a pointer of type int to point to the array arr.
int *p;
p = arr;
or p = &arr[0]; //both the statements are equivalent.
Now we can access every element of array arr using p++ to move from one element to
another.
NOTE : You cannot decrement a pointer once incremented. p-- won't work.
Pointer to Array
As studied above, we can use a pointer to point to an Array, and then we can use that
pointer to access the array. Lets have an example,
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i=0; i<5; i++)
{
printf("%d", *p);
p++;
}
In the above program, the pointer *p will print all the values stored in the array one by one.
We can also use the Base address (a in above case) to act as pointer and print all the values.
In the second approach memory wastage is more, hence it is prefered to use pointer in such
cases.