Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
121 views5 pages

Pointers

1. Pointers are variables that store memory addresses of other variables. They allow passing variables as arguments to functions and returning structures from functions. 2. Pointer arithmetic allows incrementing, decrementing, adding, and subtracting pointers and the memory addresses they point to. 3. Examples demonstrate declaring and using pointers to pass arguments, return values, dynamically allocate memory, and perform pointer arithmetic.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views5 pages

Pointers

1. Pointers are variables that store memory addresses of other variables. They allow passing variables as arguments to functions and returning structures from functions. 2. Pointer arithmetic allows incrementing, decrementing, adding, and subtracting pointers and the memory addresses they point to. 3. Examples demonstrate declaring and using pointers to pass arguments, return values, dynamically allocate memory, and perform pointer arithmetic.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

PONTERs--- pointer is a variable which can assign the address of another variabl e... 1..

it allows to pass the variables,,array,,functions,,strings and structures as function arguments... 2...a pointer allows to return struture variables from functions.. 3...it provide functions which can modify their calling arguments.. 4...it supports dynamic allocation and deallocation of memory segments... 5.... with the help of pointer variables we can swap without physicall movement of them.. POINTER OPERATOR int *ptr; FOLLOWING ARE VALID POINTER DECLARATION float *fpointer ; double *dpoint; char *mpoint1; POINTER EXPRESSIONS int x,y; int *ptr1, *ptr2; 1.. ptr1=&x; 2.. y=ptr1; 3.. ptr1=&x; ptr2=ptr1; SOME INVALID POINTER DECLARATION 1.. intx; int x_pointer; x_pointer = &x; ERROR-pointer declaration must have the prefix of * (unary operator) 2.. float y; float *y_pointer; y_pointer = y; ERROR-while assigning variable to the pointer variable the adress operator "&" m ust be used along with the variable y.. 3.. int x; char *c_pointer; c_pointer = &x; ERROR--mixed data type is not allowed.. EXAMPLE 1 /** a program to display the contents of the pointer... #include<iostream.h> void main(void) { int x; int *ptr;

x=10; ptr=&x; cout<<"x="<< x <<" and ptr =" << ptr << end1; cout<< "x=" <<" and *ptr =" << *ptr << end1; } OUTPUT-x=10 and ptr + 0x24ccfff4 x=10 and *ptr = 10 EXAMPLE 2 /** a program to assign a charecter variable to the pointer #include<iostream.h> void main(void) { char x,y; char *pointer; x = 'c'; pointer= &x; y=*pointer; cout<<"value of x = " << x << end1; cout << " pointer value = " << y << end1; } OUTPUT-value of x=c pointer value = c POINTER ARITHMETIC addition + substraction incrementation ++ decrementation -EXAMPLE 3 /**a program to display the memory adress of a variable using pointer before inc rementation and after incrementation.. #include<iostream.h> void main(void) { int value; int *ptr; value = 120; ptr = &value; cout<< "memory address before incrementation = "; cout<<ptr <<end1; ptr++; cout<< "memory address after incrementation ="; cout<<ptr <<end1; } OUTPUT-memory adrees before incrementation = 0x24c8fff4 memory adress after incrementation = 0x24c8fff6 EXAMPLE 4 /***a program to display the memory adress of a variable using pointer before de crementation and after decrementation.. #include<iostream.h>

void main(void) { float value; float *ptr; value = 120.00; ptr + &value; cout<<"memory address = " << ptr << end1; ptr--; cout<< " memory address after decrementation =" cout<< ptr<<end1; } OUT PUT-memory adress = 0x28dbfff2 memory adress after decrementation = 0x28dbffee... EXAMPLE 5 /**a program to display the adress and content of a pointer variable, subtract w ith an integer quantity and to display the adress and the content the pointer va riable. #include<iostream.h> void main(void) { int x; int *ptr,*ptr2; x=10; ptr1 = &x; ptr2 = ptr1 - 2; cout<< "values of x ="<< x <<end1; cout<< "contents of ptr1 ="<<*ptr1<<end1; cout<< "address of ptr1="<<ptr1<<end1; cout<< "address of ptr2=(ptr1-2)="<<ptr2<<end1; cout<< "contents of ptr2 =" << *ptr2<<end1; } OUTPUTvalue of x=10 contents of ptr1=10 address of ptr1=0x24d0fff4 address of ptr2=(ptr1-2)=0x24d0fff0 contents of ptr2=1126 EXAMPLE 6 /***a program to display the contents and the address of a pointer variable usin g different types of incrementation... #include<iostream.h> void main(void) { int x,y; int *ptr,*ptr2; x=10; ptr=&x; cout<<"content of pointer= "<< *ptr <<end1; *ptr=*ptr+1; y=*ptr; cout<<"value of y="<<y<<'\t'; cout<<"and pointer *ptr = *ptr+1="<<*ptr<<end1; *ptr++=1;

y=*ptr; cout<<"value of y="<<y<<'\t'; cout<<"and pointer *ptr +=1="<<*ptr<<end1; (*ptr)++; y=*ptr; cout<<"value of y="<<y<<'\t'; cout<<"and pointer (*ptr)++ ="<<*ptr<<end1; ++ *ptr; y=*ptr; cout<<"value of y="<<y<<'\t'; cout<<"and pointer (++ *ptr) ="<< *ptr << end1; ++ *ptr; ptr2 = ptr; cout<< "pointer1 ="<< *ptr<<'\t'; cout<<"and pointer2 =" << *ptr2<<end1; } OUTPUT-content of pointer=10 value of y=11 and pointer *ptr = *ptr +1 =11 value of y=12 and pointr *ptr+=1 = 12 value of y=13 and pointer (*ptr)++ =13 value of y=14 and pointer (++ *ptr) =14 pointer1 = 15 and pointer2 = 15 EXAMPLE 6 /***a program to display the content of a pointer variable using an ordinary and pointer arithmetics.... #include<iostream.h> void main(void) { int x,y; int *xpointer; int temp; temp=3; x=5* (temp+5); xpointer = &temp; y=5* (*xpointer+5); cout<<"x=" <<x<< end1; cout<<"y="<<y<<end1; } OUTPUT-x=40 y=40 EXPLANATION-the above program consists of two expressions one ordinary arithmetic expression and the other a pointer expression. x=5*(temp +5) where temp =3 x=5*(3+5) x=5*(8) x=40... EXAMPLE 7 /*****************

You might also like