Question

1 a) Explain the difference between a constant pointer and a pointer to a constant. b)...

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?

Homework Answers

Answer #1

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  

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1) Explain the difference between: Array of characters like char c[] = {'A', 'B', 'C'}; AND...
1) Explain the difference between: Array of characters like char c[] = {'A', 'B', 'C'}; AND c-string like string = "ABC"; 2) We can have two pointers that point to one memory location such as: int *intPtr1, *intPtr2; intPtr1 = &x; intPtr2 = &x; a) What method we have to make sure only one pointer points to a variable? b) Give an example. 3) a) Can a pointer to a constant receive an address of a constant item and/or non-constant...
In this lab, we want to get some practice with pointers -- specifically, the basic syntax...
In this lab, we want to get some practice with pointers -- specifically, the basic syntax of declaring them, storing memory addresses into them, dereferencing them, and seeing how they work with pointer arithmetic in the context of built-in, C-style arrays. Unless specified otherwise, you can use any valid variable names you wish. The requirements for this program are as follows: Part A Declare a pointer-to-int, initialized to nullptr. Declare an int variable, initialized to some value of your choice....
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted linked list of ints that ends in a null pointer. //=============================================================== class Cell { friend class UList; private: int data; Cell* next; Cell( int dt, Cell* nx=nullptr ) : data(dt), next(nx) {} }; //=============================================================== class UList { private: Cell* head = nullptr;    // stationary head pointer. Cell* scan = nullptr;          // for walking down the List. Cell* follow = nullptr; public: void find( int...
In C++ what is the difference between pointer variable and reference variable? Please give an example.
In C++ what is the difference between pointer variable and reference variable? Please give an example.
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
Question: Explain the difference between a permutation and a combination. Give an example of each in...
Question: Explain the difference between a permutation and a combination. Give an example of each in your explanation.
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
For questions 1 and 2 declare the array size as name constant first then use it...
For questions 1 and 2 declare the array size as name constant first then use it to declare the array 1. Declare an array named scores of type double and size 30. 2. Declare an array named names of string objects and size 15 3. Given the following array definition: int values[] = {2, 5, 8, 11}; What does each of the following display? cout << values[1];         B) cout << value[3]+values[0];   Define a three element array named letters, initialize it...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999,...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime = System.currentTimeMillis(); long...