The Langage is C++ 1. Use “pointer to” (*) and “address of” (&) operators to wri
ID: 3664041 • Letter: T
Question
The Langage is C++
1. Use “pointer to” (*) and “address of” (&) operators to write the following declarations. Initialize all variables to an appropriate value on the same line as they are declared. To help you keep these all straight, give each variable a descriptive name for what it represents. For example, name the variable for “a pointer to a pointer to a character” something like ptrPtrChar.
a character value. Code: char chrVal = ‘a’; //character value
a pointer to a character
an array of 3 characters
a pointer to an array of characters
a pointer to a pointer to a character
an integer
a pointer to an integer called ptrInt
a reference to an integer
Declare but don’t initialize a constant integer named CONINT
On the next line, initialize CONINT to some value.
a pointer to a constant integer
a constant pointer to an integer
a constant pointer to a constant integer
an array of integers called intArr.
an array of 5 integers initialized to numbers from 1 to 5
a reference to an array of 5 integers
2. The "using" statement creates an alias or nickname for a previously defined type. Dene an alias for each type in Step 1.
3. Given two ints pointing into intArr, nd and output the number of ints between the two pointed-to ints (zero if they point to the same element).
4. Write a function that takes a pointer to a pointer to an int as one argument and a second integer argument passed by reference. Add the integers and return a pointer to an int as the result. Use the aliases you created in Step 2 whenever possible.
Explanation / Answer
1.
char *ptrChar = &myChar;
char charArr[3] = {'a','b','c'};
char *ptrCharArr = charArr;
char ptrPtrChar = &ptrChar;
int a = 10;
int *ptrInt = &a;
int& refInt = a;
const int CONINT;
CONINT = 10;
const int* ptr = ∮
int * const ptrConst = &a;
int * const ptrConstInt = ∮
int intArr[] = {1,2,3};
int intArr2[] = {1,2,3,4,5};
int& intArrRef = intArr2
3.
int noOfInts(int intArr[], int i, int j) {
if(i == j) return 0;
else return j-i+1;
}
4.
int* sumInt(int **x, int& y) {
int sum = *(*x) + y;
return ∑
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.