Home | | Computer Programming | C Pointer- C Programming

Chapter: Computer Programming

C Pointer- C Programming

C Pointer is a variable that stores/points the address of the another variable.

 

POINTERS

 

Definition:

 

§    C Pointer is a variable that stores/points the address of the another variable.

 

§    C Pointer is used to allocate memory dynamically i.e. at run time.

 

§    The variable might be any of the data type such as int, float, char, double, short etc.

 

§    Syntax : data_type *var_name; Example : int *p; char *p;

 

Where, * is used to denote that “p” is pointer variable and not a normal variable.

Key points to remember about pointers in C:

 

 

§    Normal variable stores the value whereas pointer variable stores the address of the variable.

§    The content of the C pointer always be a whole number i.e. address.

 

§    Always C pointer is initialized to null, i.e. int *p = null.

 

§    The value of null pointer is 0.

 

§    & symbol is used to get the address of the variable.

 

§    * symbol is used to get the value of the variable that the pointer is pointing to.

 

§    If pointer is assigned to NULL, it means it is pointing to nothing.

 

§    The size of any pointer is 2 byte (for 16 bit compiler).

 

§    No two pointer variables should have the same name. 

§  But a pointer variable and a non-pointer variable can have the same name.

 

1 Pointer –Initialization:

 

 

Assigning value to pointer:

 

 

It is not necessary to assign value to pointer. Only zero (0) and NULL can be assigned to a pointer no other number can be assigned to a pointer. Consider the following examples;

 

 

int *p=0;

 

int *p=NULL; The above two assignments are valid. int *p=1000; This statement is invalid.

 

 

Assigning variable to a pointer:

 

 

int x; *p; p = &x;

 

This is nothing but a pointer variable p is assigned the address of the variable x. The address of the variables will be different every time the program is executed.

Reading value through pointer:

 

 

int x=123; *p; p = &x;

 

Here the pointer variable p is assigned the address of variable x. printf(“%d”, *p); will display value of x 123. This is reading value through pointer printf(“%d”, p); will display the address of the variable x.

 

printf(“%d”, &p); will display the address of the pointer variable p. printf(“%d”,x); will display the value of x 123.

 

printf(“%d”, &x); will display the address of the variable x.

 

 

Note: It is always a good practice to assign pointer to a variable rather than 0 or NULL.

 

Pointer Assignments:

 

We can use a pointer on the right-hand side of an assignment to assign its value to another variable.

 

 

Example: int main()

 

{

 

int var=50; int *p1, *p2; p1=&var; p2=p1;

}

 

Chain of pointers/Pointer to Pointer:

 

A pointer can point to the address of another pointer. Consider the following example. int x=456, *p1, **p2; //[pointer-to-pointer];

 

p1 = &x;

 

p2 = &p1;

 

printf(“%d”, *p1); will display value of x 456.

 

printf(“%d”, *p2); will also display value of x 456. This is because p2 point p1, and p1 points x. Therefore p2 reads the value of x through pointer p1. Since one pointer is points towards another pointer it is called chain pointer. Chain pointer must be declared with ** as in **p2.

 

 

Manipulation of Pointers

 

We can manipulate a pointer with the indirection operator „*‟, which is known as dereference operator. With this operator, we can indirectly access the data variable content.

 

 

Syntax: *ptr_var;

 

 

Example:

 

#include<stdio.h> void main()

{

 

int a=10, *ptr; ptr=&a;

 

printf(”\n The value of a is ”,a);

 

*ptr=(*ptr)/2;

 

printf(”The value of a is.”,(*ptr));

 

}

 

 

Output:

 

The value of a is: 10

 

The value of a is: 5

 

2 Pointer Expression & Pointer Arithmetic

 

C allows pointer to perform the following arithmetic operations:

 

A pointer can be incremented / decremented.

 

Any integer can be added to or subtracted from the pointer.

 

 

A pointer can be incremented / decremented.

 

In 16 bit machine, size of all types[data type] of pointer always 2 bytes. Eg: int a;

 

int *p; p++;

 

Each time that a pointer p is incremented, the pointer p will points to the memory location of the next element of its base type. Each time that a pointer p is decremented, the pointer p will points to the memory location of the previous element of its base type.

 

 

int a,*p1, *p2, *p3; p1=&a;

 

p2=p1++;

 

p3=++p1;

 

 

printf(“Address of p where it points to %u”, p1);         1000

 

printf(“After incrementing Address of p where it points to %u”, p1);        1002

printf(“After assigning and incrementing p %u”, p2);    1000

printf(“After incrementing and assigning p %u”, p3);    1002

In 32 bit machine, size of all types of pointer is always 4 bytes.

 

 

The pointer variable p refers to the base address of the variable a. We can increment the pointer variable,

 

p++ or ++p

 

This statement moves the pointer to the next memory address. let p be an integer pointer with a current value of 2,000 (that is, it contains the address 2,000). Assuming 32-bit integers, after the expression

 

p++;

 

 

the contents of p will be 2,004, not 2,001! Each time p is incremented, it will point to the next integer. The same is true of decrements. For example,

 

p--;  will cause p to have the value 1,996, assuming that it previously was 2,000. Here is why: Each time that a pointer is incremented, it will point to the memory location of the next element of its base type. Each time it is decremented, it will point to the location of the previous element of its base type.

 

 

 

 

Any integer can be added to or subtracted from the pointer.

 

Like other variables pointer variables can be used in expressions. For example if p1 and p2 are properly declared and initialized pointers, then the following statements are valid.

 

 

y=*p1**p2;

 

sum=sum+*p1; z= 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<conio.h> void main()

{

 

int ptr1,ptr2; int a,b,x,y,z; a=30;b=6; ptr1=&a; ptr2=&b;

 

x=*ptr1+ *ptr2 –6; y=6*- *ptr1/ *ptr2 +30;

 

printf(“\nAddress of a + %u”,ptr1); printf(“\nAddress of b %u”, ptr2); printf(“\na=%d, b=%d”, a, b); printf(“\nx=%d,y=%d”, x, y); ptr1=ptr1 + 70;

 

ptr2= ptr2;

 

printf(“\na=%d, b=%d”, a, b);

 

}

 

 

 

 

/* Sum of two integers using pointers*/

 

 

#include <stdio.h> int main()

{

 

int first, second, *p, *q, sum; printf("Enter two integers to add\n"); scanf("%d%d", &first, &second);

 

p = &first;

 

q = &second;

 

sum = *p + *q;

 

printf("Sum of entered numbers = %d\n",sum); return 0;

 

}

 

 

 

 

3 Pointers and Arrays

 

 

Array name is a constant pointer that points to the base address of the array[i.e the first element of the array]. Elements of the array are stored in contiguous memory locations. They can be efficiently accessed by using pointers.

 

Pointer variable can be assigned to an array. The address of each element is increased by one factor depending upon the type of data type. The factor depends on the type of pointer variable defined. If it is integer the factor is increased by 2. Consider the following example:

 

 

 

int x[5]={11,22,33,44,55}, *p;

 

p = x;      //p=&x; // p = &x[0];

 

 

 

 

Remember, earlier the pointer variable is assigned with address (&) operator. When working with array the pointer variable can be assigned as above or as shown below:

 

 

Therefore the address operator is required only when assigning the array with element. Assume the address on x[0] is 1000 then the address of other elements will be as follows

 

x[1] = 1002

x[2] = 1004

x[3] = 1006

x[4] = 1008

The address of each element increase by factor of 2. Since the size of the integer is 2 bytes the memory address is increased by 2 bytes, therefore if it is float it will be increase 4 bytes, and for double by 8 bytes. This uniform increase is called scale factor.

 

p = &x[0];

 

Now the value of pointer variable p is 1000 which is the address of array element x[0]. To find the address of the array element x[1] just write the following statement.

 

p = p + 1;

 

Now the value of the pointer variable p is 1002 not 1001 because since p is pointer variable the increment of will increase to the scale factor of the variable, since it is integer it increases by 2.

The p = p + 1; can be written using increment or decrement operator ++p; The values in the array element can be read using increment or decrement operator in the pointer variable using scale factor.

 

Consider the above example.

 

printf(“%d”, *(p+0)); will display value of array element x[0] which is 11. printf(“%d”, *(p+1)); will display value of array element x[1] which is 22. printf(“%d”, *(p+2)); will display value of array element x[2] which is 33. printf(“%d”, *(p+3)); will display value of array element x[3] which is 44. printf(“%d”, *(p+4)); will display value of array element x[4] which is 55.

 

 

 

 

/*Displaying the values and address of the elements in the array*/

 

 

#include<stdio.h>

 

void main()

 

{

 

int a[6]={10, 20, 30, 40, 50, 60};

 

int *p;

 

int i;

 

p=a;

for(i=0;i<6;i++)

 

{

 

printf(“%d”, *p); //value of elements of array printf(“%u”,p); //Address of array

 

}

 

getch();

 

}

 

 

 

 

/* Sum of elements in the Array*/

 

 

#include<stdio.h>

 

#include<conio.h> void main()

{

 

int a[10]; int i,sum=0; int *ptr;

 

 

printf("Enter 10 elements:n");

 

 

for(i=0;i<10;i++)

 

scanf("%d",&a[i]);

 

ptr = a;      /* a=&a[0] */

 

for(i=0;i<10;i++)

 

{

 

sum = sum + *ptr; //*p=content pointed by 'ptr' ptr++;

 

}

 

printf("The sum of array elements is %d",sum);

 

}

 

/*Sort the elements of array using pointers*/

 

#include<stdio.h> int main(){

int  i,j, temp1,temp2;

 

int arr[8]={5,3,0,2,12,1,33,2}; int *ptr;

 

for(i=0;i<7;i++){ for(j=0;j<7-i;j++){

 

if(*(arr+j)>*(arr+j+1)){

 

ptr=arr+j;

 

temp1=*ptr++;

 

temp2=*ptr; *ptr--=temp1; *ptr=temp2;

}}}

 

for(i=0;i<8;i++){

 

printf(" %d",arr[i]); } }

 

 

 

 

 

 

4 Pointers and Multi-dimensional Arrays

 

The array name itself points to the base address of the array.

 

Example:

 

int a[2][3];

 

int *p[2];

 

p=a;   //p points to a[0][0]

/*Displaying the values in the 2-d array*/

 

#include<stdio.h> void main()

{

 

int a[2][2]={{10, 20},{30, 40}}; int *p[2];

 

int i,j; p=a;

 

for(i=0;i<2;i++)

 

{

 

for(j=0;j<2;j++)

 

{

 

printf(“%d”, *(*(p+i)+j));  //value of elements of array

 

}

 

}

 

getch();

 

}

 

5 Dynamic Memory Allocation

 

 

The process of allocating memory during program execution is called dynamic memory

 

allocation.

 

 

 

 

Dynamic memory allocation functions





Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Computer Programming : C Pointer- C Programming |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.