0% found this document useful (0 votes)
34 views8 pages

Pointer With Examples

Pointer With Examples

Uploaded by

Swaraj Kumar
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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views8 pages

Pointer With Examples

Pointer With Examples

Uploaded by

Swaraj Kumar
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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Pointer with examples

Defn

A pointer in c is a special variable which can store the memory address of other variable. data type for pointer is same as data type of variable whose address will be represented.
Syntax--<data type>*<ptr-name>;

Examples
(only pseudo code-for concept)

1.
int a=10; int b; b=a; b=&a; wrong---- variable doesnt hold address,so we need special to hold address of variable called pointer.
So Weve-----

variable

int *b; b=&a;

2.

Int a=10; Int *p; p is a pointer which points an integer type variable or,p is pointer to an integer. * is indirection operator P=&a; p is pointing as value. Or, p stores address of a or,value stored at p=address of a. & is address operator. Note----a is direct addressing *p is indirect addressing(*p=value at address stored at p)

10

1000

p
*p

Indirect addressing works when only address is given,no variable name given,so pointer used to access. * Is polymorphic in c i>multiplication operator ii>indirection operator *p means dereferencing pointer P=&a means p referenced to a Commonly pointer holds 2 byte(dos based ide) of memory but 4 bytes (in windows,linux based ide).

3.
int a=10; char *p; p=&a;wrong

in c its warning(suspicious pointer conversion) in c++ its error(cant convert int * to char *) because data type of pointer is same as data type of variablewhose address will be represented.so,we need to typecast this as-----

4.
Int a=10; Char *P; P=(char*)&a; Printf(%d,&p);

Although p changed in char type but still o/p is not relevant,so not recommended to do so. Thus always give data type of pointer as of variables data typewhom it representing.

5.
Only address stored in pointer ,not values
Int a=10; Int *p; P=a;wrong(although true in c compiler considering value as address)

Wild pointer concept

6.
Int a=10; Int *p; *p=a;(wrongsometm run time error)
10 6058

6058 overwritten by 10(as value) {if 6058 is free,otherwise code crash} When pointer is dereferenced without any address as above ie. Int *p=a

it is called wild pointer,any there would be chance of crashing code,so must supply some address ie. Int *p=some address or, int *p=null; or,int *p=\0; so, to avoid run time error in above code,weve-----

7.
Int a=10; Int *p=null;(0th address in memory is reserved in o/s as null) *p=a;//dereferencing Note-although this is not 100% correct but safe almost. So,dereference only when addressed while declaring..

Providing address to a value

8.
int *p; p=(int *)7000; //a no. is made like address by typecasting

but problem may occur if address 7000 is engaged somewhere becoz, by default a value cant be address.

9.
Int *p; P=7000;

//run in c not in c++(a value is not address)

10.
Int *p;
P=&7000;

//this will never run/execute in c/c++

11.
Pointer never points itself Int *p;
P=&p;(wrong)

12.
Int a=10; Int *p; Int *q; p=&a; q=&p;(wrong)

it can be corrected as--------Int a=10; Int *p; Int **q;(termed as pointer to pointer of q) p=&a; q=&p;(true now)

notepointer is backbone of c. because of this c is still more popular. Most of the work associated to virus/antivirus were performed with the help of pointers.(advance c)

(compiled by manish mishra-self written notes) Guided bySachin kapoor Sca,Bhopal. For more [email protected]

You might also like