Pointer programming exercises and solutions in C
Pointer programming exercises and solutions in C
Pointer is a variable that stores memory addresses. Unlike normal variables it does not store user given or processed value, instead it stores valid computer memory address.
Pointer allows various magical things to be performed in C.
- Pointers are more efficient in handling arrays and structures.
- Pointers are used to return multiple values from a function.
- Pointer allows dynamic memory allocation and deallocation (creation and deletion of variables at runtime) in C. Which undoubtedly is the biggest advantage of pointers.
- Pointer allows to refer and pass a function as a parameter to functions.
and many more...
For beginners pointers can be a bad dream if not practiced well. However, once mastered you can do anything you want to do in C programming language.
In this exercise I will cover most of the pointer related topics from a beginner level. Always feel free to drop your queries and suggestion down below in the comments section.

OR
- Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.
- Pointer Syntax : data_type *var_name; Example : int *p; char *p;
- Where, * is used to denote that “p” is pointer variable and not a normal variable.
KEY POINTS TO REMEMBER ABOUT POINTERS IN C:
- Normal variable stores the value whereas pointer variable stores the address of the variable.
- The content of the C pointer always be a whole number i.e. address.
- Always C pointer is initialized to null, i.e. int *p = null.
- The value of null pointer is 0.
- & symbol is used to get the address of the variable.
- * symbol is used to get the value of the variable that the pointer is pointing to.
- If a pointer in C is assigned to NULL, it means it is pointing to nothing.
- Two pointers can be subtracted to know how many elements are available between these two pointers.
- But, Pointer addition, multiplication, division are not allowed.
- The size of any pointer is 2 byte (for 16 bit compiler).
EXAMPLE PROGRAM FOR POINTERS IN C:
POINTERS IN C – PROGRAM OUTPUT:
50
|
Syntax to declare pointer variable
<data-type> * <variable-name>
<data-type> * <variable-name>
Example of pointer declaration
int * integer_pointer;
float * float_ptr
char * charPtr;
long * lptr;
int * integer_pointer;
float * float_ptr
char * charPtr;
long * lptr;
Required knowledge
Please go through above tutorials to get a good grasp of following examples.
List of pointer programming exercises
- Write a C program to create, initialize and use pointers.
- Write a C program to add two numbers using pointers.
- Write a C program to swap two numbers using pointers.
- Write a C program to input and print array elements using pointer.
- Write a C program to copy one array to another using pointers.
- Write a C program to swap two arrays using pointers.
- Write a C program to reverse an array using pointers.
- Write a C program to search an element in array using pointers.
- Write a C program to access two dimensional array using pointers.
- Write a C program to add two matrix using pointers.
- Write a C program to multiply two matrix using pointers.
- Write a C program to find length of string using pointers.
- Write a C program to copy one string to another using pointers.
- Write a C program to concatenate two strings using pointers.
- Write a C program to compare two strings using pointers.
- Write a C program to find reverse of a string using pointers.
- Write a C program to sort array using pointers.
- Write a C program to return multiple value from function using pointers.
Comments
Post a Comment