NORMAL VARIABLE AND POINTER VARIABLE
POINTER
| |
1. Pointer is a variable that holds address of another variable.
| |
2. The general form of an ordinary variable declaration is :
Syntax : Datatype Identifier;
Example : int x;
|
2. The general form of a pointer variable declaration is :
Syntax : Datatype *PtrVaribale;
Example : int *ptr;
|
3. The general form of initializing a variable is:
Syntax : Variable = Value;
Example : int x;
x = 10;
|
3. The general form of initializing a pointer variable is:
Syntax : PtrVariable = &Variable;
Example: int x, *p;
p = &x;
|
4. For ordinary variables compiler allocates memory based on their data types.
Example: int x; char y;
For x compiler allocates 2 Bytes and for y compiler allocates 1 Byte memory.
|
4. For any type pointer variables compiler allocates only 2 bytes of memory.
Example: int *p; char *q;
For p compiler allocates 2 Bytes and for q also compiler allocates 2 Bytes memory.
|
5. For ordinary variables only static memory allocation is possible.
|
5. For pointer variables dynamic memory allocation is possible.
|
6. /* Example program for Variables */
#include<stdio.h>
#include<conio.h>
main()
{
int x = 40;
clrscr();
printf(“\nResult = %d”,x);
}
|
6. /* Example program for Pointer */
#include<stdio.h>
#include<conio.h>
main()
{
int x = 40, *p=&x;
clrscr();
printf(“\nResult = %d”,*p);
}
|
No comments:
Post a Comment