Pointers Definition | Declaration and Initialization in c programming msbte
Pointers Definition | Declaration and Initialization in c programming msbte |
Definition Of Pointer
- Pointer is a variable which stores memory address of other variable.
- Pointer in C is a user defined data type. It is build by using primary data types. Variables hold values and pointers hold their memory addresses where these variables are located in memory.
- A pointer variable is therefore nothing but, a variable that holds address of another variable in memory.Memory address is a positive number so its type is unsigned integer. Since pointer is a variable it is also stored at another location in memory and the values stored in it are of unsigned integer type.
- The values whose addresses are stored in a pointer variable can be of type char, int, float, double, etc.Let's see how to declare the variables and initialize address of variable to it.
Declaration Of Pointer
- In the program pointers are declared at same location where we declare variables i.e. in declaration section.Declaring a pointer is somewhat similar to declaring a variable.
- To indicate which type of value is stored in a variable we use data type while declaring a variable. e.g. to hold integer type of value the data type of variable should be integer, to hold a character the data type of variable should be char and to hold real number the data type of variable should be float or double.
- Similarly while declaring a pointer; we should know the data type of the value to which it is going to point, so that we can declare a pointer of that data type. This means suppose a pointer stores address of a variable of type integer then we declare pointer as integer.
- Syntax
- The syntax of pointer declaration is as follows
- where specifies that it is pointer variable.
data_type *pointer_name;
- Example of integer
int *ptr;
- Example of character
- Another examples of pointers are as follow.
- Here char *cptr specifies that cptr points to character type of value. This cptr pointer will be stored at a specific memory location.
char *cptr; float *fptr;
float *fptr;
Initialization Of Pointer
- After declaring the pointer, the next step is to assign the address of certain variable to it .This is called as pointer initialization. It connects variables and pointers.
- Syntax
- Syntax for initializing a pointer.
pointer_name = &variable_name;
int *ptr;
int a;
ptr = &a;
Fig 1
- Suppose the variable a stores at location 2000 and the pointer variable ptr is located at location 5000.
- Here ptr=&a; is used to give the address of a to ptr variable so in the memory at location 5000 ,the 2000 is copied.
- The dashed line says that the ptr points the value located at location 2000.
- As variable declaration and initialization can be combined in single statement, in the same way the declaration of pointer and initialization of pointer can be combined in one statement as follows:
int *b = &a;
char ch='A';
char *q;
q=&ch;
Fig 2
Post a Comment