Pointers in C programming
Theory:
Pointer – Pointers are variables that hold a memory location.
Pointer is a variable that contain the address of another variable.Every Location has a number associated with it, it is called as an address.
The Symbol '&' Ampersand is used to represent the address.
The Symbol '*' is used to represent a pointer. It is called the dereferencing or indirect operator.
int *p; here p is a pointer variable. Its size is 4 byte and it accept the address of integer variable.
For float , char etc. we can type.char *ch;float *ft;double *db;Each one of them will get same size of the memory i.e. 4 bytes.
Harold Lawson is credited with the invention of the pointer, PL/I the first Procedure Oriented Language(POL) supported pointer in 1964.
Program snapshot:
Pointer – Pointers are variables that hold a memory location.
Pointer is a variable that contain the address of another variable.Every Location has a number associated with it, it is called as an address.
The Symbol '&' Ampersand is used to represent the address.
The Symbol '*' is used to represent a pointer. It is called the dereferencing or indirect operator.
int *p; here p is a pointer variable. Its size is 4 byte and it accept the address of integer variable.
For float , char etc. we can type.char *ch;float *ft;double *db;Each one of them will get same size of the memory i.e. 4 bytes.
Harold Lawson is credited with the invention of the pointer, PL/I the first Procedure Oriented Language(POL) supported pointer in 1964.
Program snapshot:
Declaration of a Pointer :
Declaration of a pointer is nothing but
assigning a memory location. And Letting our compiler known about the variable,
what is its data type , its value , and address of memory location.
Pointer with printf() Statement
=================================
printf(“\n%u\n”,*p);
The above statement will print the value at the address stored
in P pointer.
As we know P is a pointer and according to the definition, it
contain the address of a variable.
Working of printf(); :
printf() statement require
1)
Format Specifier e.g. %d , %u, %f etc.
2)
Data to print. E.g. “Hello World! ”, 10 , -123 etc.
If we write
printf(“\n%d\n”,P);
This is a valid statement, but the content of P will be echoed on the
console , and content of P is the address of variable n.
*P -> will give the Value stored at the address in the
pointer.
Pointer with scanf()
Statement
================================
scanf() statement is used to accept the input from the user via
i/o devices.
scanf() function require:
1)
Format Specifier.
2)
Address of the memory where value to be stored.
FORMAT
scanf(“%d”,p); -> Here p will provide the content. i.e the
address of n.
so we can directly type P in scanf() function.
Lets see other options.
1)
*p
2)
&p
3)
P
1) *p : It will give the value at the address it content. i.e 10
So the statement will be
scanf(“%d”,10); | as p -> address of n , and n content 10;
So when the scanf() function encounter it will hang.
2) &p : It will give the address of variable p.
As P is declared as pointer, so we can’t store any value in p
except the address of other location.
So this will give error.
3) P : p will give the its content, which is nothing but the
address of the other local variable i.e. n.
So this is valid statement.
scanf(“%d”,1654200); This will store the user entered value at
the address 1654200 , i.e. in n.
No comments:
Post a Comment