Bodhak c pointers tutorial in this chapter we will discuss Introduction to Pointers, Advantages, and Disadvantages of pointers, Declaring pointer, Initializing pointer.
The pointer is one of the most important topics in c. In this chapter, we are giving an introduction to the concepts of pointers.
the rest of topics are covered in this tutorial under a title of advanced pointers. Before referring the advanced pointers get good knowledge on the pointers basics.
c pointers tutorial:-
A pointer is a derived datatype.
A variable of type pointer is called pointer variable.
A pointer variable holds an address of another variable or memory location.
Pointers supports the Data Structures and File Structures
Pointer supports the Dynamic Memory Allocation.
Pointer can handle the External File Manipulation
Advantages and disadvantages of pointers:
1) The function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.
2) In the case of arrays, we can decide the size of the array at runtime by allocating the necessary space.
1) If sufficient memory is not available during runtime for the storage of pointers, the program may crash (least possible)
2) If the programmer is not careful and consistent with the use of pointers, the program may crash (very possible)
Declaration of pointers:
The interpretation of pointer declaration is somewhat different, however, then the interpretation of other variable declarations. When a pointer variable is declared, an asterisk must precede the variable name (*).
Syntax: data type *variable name;
Ex: int *ptr;
Assigning the variable address to pointer variable:
ptr = &x;
- To get the address of “x” we will use the unary operator ampersand – “&”
- “&x” will give us the location of “x”. This is also termed as the address operator. Since the address operator returns the address of a variable.
- to store this address in another variable. (ptr=&x) This variable ptr is termed as a pointer since it ‘points’ to the location where “x” is stored in memory.
- A pointer can be defined as a variable, which contains the address of another variable. Therefore, ptr refers to X’s address and not its value.
c pointers tutorial