What are pointers?
Pointers are the special variable that stores the variable address, instead of storing the variable value. Pointer is a powerful feature of a programming language. A pointer is mostly used to save memory space and improve execution time.
Syntax: pointer = & variable;
Pointer operators
- * operator declares the pointer and it is used to refer to the value of the variable referenced by the pointer.
- & operator returns the variable's address which is pointed to by the pointer.
Example
CODE
/* program for working of pointer variable */
#include <stdio.h>
int main()
{
int * pb, b; //declare variable and pointer variable as integer data type
b = 31; //initialize b variable
printf( “n address of b variable: %p n”, &b); //displays the address of b
printf( “n b value: %d n”, b); //display 31
pb = &b; //address of b is stored in pb pointer variable
printf( “n pointer address: %p n”, pb); //displays the pointer variable pb
printf( “n value of pointer: %d n”, *pb); //display 31
b = 3; //initialize variable
printf( “n pointer address: %p n”, pb); //displays the pb value
printf( “n value of the pointer: %d n”, *pb); //display 3
*pb = 10;
printf( “n address of b variable: %p n”, &b); //displays the address of b
printf( “n b value: %d n”, b); //display 10
return 0;
}
OUTPUT
address of b variable: 2689678
b value: 31
pointer address: 2689678
value of pointer: 31
pointer address: 2689678
value of the pointer: 3
address of b variable: 2689678
b value: 10
Code explanation
In the above code, b is a variable of data type integer which is pointed to by pointer variable pb. The variable b is assigned a value 31. Its value and its address are displayed. The pointer variable pb is made to reference b. Its value and the value of the location referenced are displayed. Reinitialize b. Display the address of b and the value stored in b using pb. Reinitialize b using pb. Display the value and address of b.
Function Pointers
Just like pointers to data, pointers can also be made to point to functions. The syntax for declaring a function pointer is given below.
returntype (*ptr) (argtype) = &functionName;
To invoke the function using the function pointer, use the syntax:
(*ptr)(arg);
Pointers as function argument
Passing pointer as a function parameter will store the argument’s address, which is passed during the function call and this is known as call by reference. When the function is called by reference, changes made to the formal parameters are reflected in the calling function.
Example
CODE
#include <stdio.h>
void fun1(int *a); // passing pointer as argument
int main()
{
int b=10;
printf(“ n Example for passing address of variable ”);
printf(”n The value of b before function call: ”,b);
fun1(&b); //address of b is passed
printf(”n The value of b after function call: ”,b);
return 0;
}
void fun1(int *a) //function definition
{
*a = 20;
}
OUTPUT
Example for passing address of variable
The value of b before function call: 10
The value of b after function call: 20
Code explanation
In the above code, address of variable is passed as argument which is stored in the pointer parameter. The value of the variable is modified using the pointer passed to the function. The changes made to the variable's value is reflected in the calling function. Therefore, the above code uses call be reference.
Applications of pointer parameter
It is used to swap two variables easily as shown below.
Example
CODE
#include <stdio.h>
void swapValues(int *c, int *d); // passing pointers as arguments
int main()
{
int a=10, b=20;
printf(“ n Application - Swapping values using pointers”);
printf(”n The value of variables a and b before function call: %d %d”,a,b);
swapValues(&a,&b); //address of b is passed
printf(”n The value of variables a and b after function call: %d %d”,a,b);
return 0;
}
void swapValues(int *c, int *d) //function definition
{
int temp;
temp = *c;
*c = *d;
*d = temp;
}
OUTPUT
Application - Swapping values using pointers
The value of variables a and b before function call: 10 20
The value of variables a and b before function call: 20 10
Code explanation
In the above code, address of variables a and b are passed as argument which are stored in the pointer parameters c and d. The values pointed by locations c and d are swapped using a temporary variable inside the function. The changes made to the variable's value is reflected in the calling function. Thus, the value of the variables a and b are swapped using pointer parameters.
Benefits of using pointers
- Reduces the execution time of a program.
- Function can return more than one value.
- Dynamic allocation of memory.
- Simplifies the code by reducing the number of lines of code.
- Helps to construct various data structures such as linked list, stack, queue and so forth.
Drawbacks of using pointers
- The program might crash, when there is no additional memory.
- Memory leakage occurs if dynamically allocated memory is not freed explicitly.
- Segment fault might occur due to uninitailzed pointers.
Context and applications
This topic is important for postgraduate and undergraduate courses, particularly for, Bachelors in Computer Science Engineering, and Associate of Science in Computer Science.
Practice Problems
Question 1: Select the correct syntax for pointer declaration.
- variable=* pointer
- pointer=& variable
- pointer=* variable
- variable=& pointer
Answer: Option b is correct.
Explanation: Pointers will store the variable address and the syntax to declare the pointer is pointer=& variable.
Question 2: In pointer, ____ operator will return the variable address as its result.
- *
- +
- %
- &
Answer: Option d is correct.
Explanation: Pointer is the powerful feature programming language and the operator & will return the variable address as its result.
Question 3: Select the correct syntax for the function pointer.
- return type (* function name) (argument list)
- return type (function name) (argument list)
- return type (*function name)
- return type (argument list)
Answer: Option a is correct.
Explanation: Function pointer stores the function’s address and the correct syntax for function pointer is return type (* function name) (argument list)
Question 4: __________ has the capability to point to a function.
- Pointer
- Integer
- Function pointer
- Constant pointer
Answer: Option c is correct.
Explanation: The function pointer stores the function’s address. Thus, the function pointer has the capability to point to a function.
Question 5: When the variables are passed by ________, modifications made to the variables in the called function is reflected in the calling function.
- value
- reference
- array
- enum
Answer: Option b is correct.
Explanation: Passing the variables by reference involves passing address of the variables rather than value of the variables. Since modifications are made to the actual location of the variables rather than their copy, changes made in the called function are reflected in the calling function.
Want more help with your computer science homework?
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.
Concept of Pointer Parameter Homework Questions from Fellow Students
Browse our recently answered Concept of Pointer Parameter homework questions.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.