-
Given the following code, determine the datatype for each expression:
int x;
int* ptr;
Expression |
Datatype |
Possible Values |
x |
|
|
ptr |
|
|
*ptr |
|
|
&ptr |
|
|
&x |
|
|
- Add code as directed by the comments to pointersExercise.cpp and execute to see values update:
int main(){
int number = -1;
int* intPtr1;
int* intPtr2;
return 0;
}
- Diagram the elements on the stack and write the output of the following (adapted from HERE):
#include <iostream>
int main (){
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for( int n = 0; n < 5; n++){
std::cout << numbers[n] << ", ";
}
return 0;
}
-
- Declare ex0 as a pointer to a char
- Declare ex1 as a pointer to a pointer to a char
- Declare ex2 as an array of 10 elements
- Declare ex3 as an array of 10 elements that are pointers to chars
- Declare ex4 as a pointer to an array of 30 char elements
- Declare ex5 as an array of 10 pointers to arrays of 500 char elements
- Declare ex7 as a pointer to const int
- Write a function ex8 that takes a char pointer named pc, increments the address that it points to and returns that value
- Write a function ex9 that takes an int pointer named num, and prints out what num points to and a message saying if what num points to is odd or even
- Add code as directed by the comments:
#include <iostream>
#include <string>
using std::string;
using std::cout;
struct Car{
float price;
string make;
string model;
};
int main(){
return 0;
}