1
a) Explain the difference between a constant pointer and a pointer to a constant.
b) Give example of both with explanation.
2
int *intPtr1, *intPtr2 = nullptr;
a) What value is stored in intPtr1?
b) What value stored in intPtr2?
3
When searching for an element in an array:
a) What do you return if the element is found?
b) What do you return if the element is not found?
Question 1:
a) Constant pointer:
In this we cannot change the pointer p but we can change the value pointed by ptr. " char *const ptr" is a constant pointer to non-constant character. If we try to change the pointer value we will get the compiler error.
Syntax : datatype *Const pointer_name;
Pointer to a constant:
In this we can not change the value pointed by ptr but we can change the pointer itself. "const char" is a pointer to a const char.
Syntax: Const datatype *pointer name;
b)
Example:
#include<stdio.h>
int main()
{
int n1=10 ,n2=20;
int *const ptr= &n1;
printf("%d",*ptr);
*ptr=n2;
printf("%d",*ptr);
return 0;
}
//pointer to a constant
#include<stdio.h>
int main()
{
int n1=10 , n2=20;
const int *ptr= &n1;
ptr=&n2;
printf("%d",*ptr);
return 0;
}
Question 2:
a) Here int *intPtr1; It is Uninitialized pointers which is also called invalid pointer.
b) Here int *intPtr2 = nullptr does not make any sense . If *intPtr2=NULL then the value stored in intPtr2 is zero .
Question 3:
a) Searching for an element in an array when the search element is found then the index number of the element is return whose value is matches with the target value.
b) Searching for an element in an array when the search element is not found then the -1 value is return which means search element is not present in the array
Get Answers For Free
Most questions answered within 1 hours.