For each block of code, determine if it is syntactically correct and if it is le
ID: 3885945 • Letter: F
Question
For each block of code, determine if it is syntactically correct and if it is legal code (e.g., no dereferencing of invalid pointers). If it is both syntactically correct and legal, answer the question about the value(s) of certain expressions and/or variables at the end of the code.
1.
int x=1, y=2;
int *pOne, *pTwo;
pOne = x;
pTwo = y;
*pOne = *pTwo;
What are x and y?
2.double c=3.0, d=4.5;
double *pOne, *pTwo;
pOne = &c;
pTwo = &d;
*pTwo = c;
*pOne –= 1.0;
d += 2.2;
What are c, d, *pOne, and *pTwo?
3.
double x=2.1, y=3.5;
double *pOne, *pTwo;
y –= x;
*pOne = 4.0;
*pTwo = *pOne * 2.0;
What are x, y, *pOne, and *pTwo?
4. double x=2.1, y=3.5, *pOne;
pOne = &y;
*&x += 1.9;
*pOne += x;
What are x, y, and *pOne?
5. int x=1, y=2;
int *pOne, pTwo, pThree;
pOne = &x;
pTwo = &y;
pThree = pOne;
pOne = pTwo;
*pThree = *pOne;
What are x, y, *pOne, *pTwo, and *pThree?
6. int y=7, z=11;
int *pOne = &y, *pTwo = &z;
pOne += 2;
z += *pOne;
What are y, z, *pOne, and *pTwo?
Pointers and Functions
1. void getPowers(int x, int xSqr, int xCube){
xSqr = x * x;
xCube = x * xSqr;
}
int main(){
int x, xSqr, xCube;
x = 3;
getPowers(x, xSqr, xCube);
What are x, xSqr, xCube?
2.
void getPowers(int x, int *pxSqr, int *pxCube){
*pxSqr = x * x;
*pxCube = x * *pxSqr;
}
int main(){
int x, xSqr, xCube;
x = 3;
getPowers(x, xSqr, xCube);
What are x, xSqr, xCube?
3.
void getPowers(int x, int *pxSqr, int *pxCube){
*pxSqr = x * x;
*pxCube = *pxSqr * x;
}
int main(){
int x, *pxSqr, *pxCube;
x = 3;
getPowers(x, pxSqr, pxCube);
What are x, *pxSqr, *pxCube?
4.
void getPowers(int x, int *pxSqr, int *pxCube){
*pxSqr = x * x;
*pxCube = *pxSqr * x;
}
int main(){
int x, xSqr, xCube;
x = 3;
getPowers(x, &xSqr, &xCube);
What are x, xSqr, xCube
Explanation / Answer
1.
int x=1, y=2;
int *pOne, *pTwo;
pOne = x;
pTwo = y;
*pOne = *pTwo;
Answer: The above code is not valid. Before knowing the error let us first know the difference between normal variables and pointer variables. Normal variables store some value where as the pointer variables store the address of a variable.
Now, coming to the above code, in lines 3 and 4 we used pOne = x and pTwo = y which means we are trying to assign the values of x and y which is illegal, if we want pOne anf pTwo to have the values of a and y we shoe make the pointer variables pOne and pTwo point to the address of x and y
that is the statement should be
pOne = &x;
pTwo = &y;
If we change the lines 3 and 4 as above then the code will work and the value of y will be assigned to value of x resulting the values of x and y to be 2 and 2 respectively.
2.
double c=3.0, d=4.5;
double *pOne, *pTwo;
pOne = &c;
pTwo = &d;
*pTwo = c;
*pOne –= 1.0;
d += 2.2;
Answer: The above code is correct.
After the execution of the first statement c will be 3 and d will be 4.5
Now we made pOne to point to the address of c and pTwo point to the address of d
hence c will be 3, d will be 4.5, *pOne will be 3, and *pTwo will be 4.5
Now we have *pTwo = c, as *pTwo points to the address of d, the statement modifies the value of d to value of c,
hence now c will be 3, d will be 3, *pOne will be 3, and *pTwo will be 3
Now we have *pOne -=1.0 which means we are decrementing the value of c and assigning it back to *pOne
hence now c will be 2, d will be 3, *pOne will be 2, and *pTwo will be 3
Now the final statement d += 2.2 which mean we are incrementing the value of d by 2.2 which is 3 + 2.2 = 5.2
hence now c will be 2, d will be 5.2, *pOne will be 2, and *pTwo will be 5.2
3.
double x=2.1, y=3.5;
double *pOne, *pTwo;
y -= x;
*pOne = 4.0;
*pTwo = *pOne * 2.0;
Answer: The above code is illegal, as discussed above we cannot assign a value to a pointer variable we can only assign a address of a variable to a pointer variable. If we observe the above code we used the statement *pOne = 4.0, that is we are trying to assign a value 4.0 to the pointer variable which is illegal.
4.
double x=2.1, y=3.5, *pOne;
pOne = &y;
*&x += 1.9;
*pOne += x;
Answer: The code is correct.
We have x = 2.1 and y = 3.5
Next we are pointng pOne to y, which means *pOne will be 3.5
Next we have *&x += 1.9, here &x means the address of x, *&x means pointing to the address of x ,
hence *&x += 1.9 means adding 1.9 to x and assigning it back to x, so x will be 4.
Finally we have *pOne += x, which means adding 4 to *pOne(which is value of y) and assigning it back to *pOne, so *pOne wiil be 4+3.5 = 7.5
Hence after the execution of the code x will be 4, y will be 7.5 and *pOne will be 7.5
5.
int x=1, y=2;
int *pOne, pTwo, pThree;
pOne = &x;
pTwo = &y;
pThree = pOne;
pOne = pTwo;
*pThree = *pOne;
Answer: The above is not a valid code. If we observe carefully only pOne is a pointer variable and the rest of the variables used in the code are normal variables and in the code we assigning the address of &y to pTwo which is illegal as pTwo is a normal int variable also the rest of the statements are illegal as pTwo and pThree are normal int variables.
6.
int y=7, z=11;
int *pOne = &y, *pTwo = &z;
pOne += 2;
z += *pOne;
Answer: The above code works fine, but in the third statement we are using pOne += 2 which means we are modifying the address to which pOne is pointing to leading pOne to have a default value.
As per the code we have y = 7 and z = 11
next we are assigning *pOne = &y, so *pOne will be 7
and *pTwo = &z, so *pTwo will be 11
Now we have pOne +=2 which means we want to add 2 to the address that pOne is holding, which means we are changing the address to which pOne is pointing to hence *pOne will now be 0 by default.
Next we have z+ = *pOne as *pOne is 0 the value of z remains the same after the execution of the statement.
Finally y will be 7, z will be 11, *pOne will be 0 and *pTwo will be 11
Pointers and Functions
1.
void getPowers(int x, int xSqr, int xCube){
xSqr = x * x;
xCube = x * xSqr;
}
int main(){
int x, xSqr, xCube;
x = 3;
getPowers(x, xSqr, xCube);
Answer:
The above code runs without any error but will not give us desired results. In the above code the fuction getPowers calculates the square and cube of x.
While calling the function we are passing the variables x, xSqr and xCube as parameters but not their reference and hence the after the execution of the above code the values of x, xSqr and xCube are not changed.
Also not that x is assigned 3 and xSqr and xCube are not assigned any values and hence they have their default values either 0 or 1.
Hence after the code excution x will be 3, xSqr will be 1 and xCube will be 0.
2.
void getPowers(int x, int *pxSqr, int *pxCube){
*pxSqr = x * x;
*pxCube = x * *pxSqr;
}
int main(){
int x, xSqr, xCube;
x = 3;
getPowers(x, xSqr, xCube);
}
Answer: The above shows an error, the function getPowers takes one integer variable and two pointer variables as parameters, but while calling the function from the main we are passing the variables x, xSqr and xCube as parameters. As the function accepts pointer variables and we are sending normal variables instead of refrence variable the code show an error.
3.
void getPowers(int x, int *pxSqr, int *pxCube){
*pxSqr = x * x;
*pxCube = *pxSqr * x;
}
int main(){
int x, *pxSqr, *pxCube;
x = 3;
getPowers(x, pxSqr, pxCube);
Answer: The above code shows error. The function getPowers takes one integer variable and two pointer variables as parameters , though while we are calling getPowers from the main function by passing one integer and two pointer varaibles as parameters, we have no where specified in the main function to which variables the pointer variables *pxSqr and *pxCube refer hence, which means pxSqr and pxCube are null pointers and hence accessing them in the function will throw an error.
4.
void getPowers(int x, int *pxSqr, int *pxCube){
*pxSqr = x * x;
*pxCube = *pxSqr * x;
}
int main(){
int x, xSqr, xCube;
x = 3;
getPowers(x, &xSqr, &xCube);
Answer: The above code works fine and produces desired output.
The getPowers accepts one integer variable and two pointer variables as parameters. Inside the function we are calculating the square and cube and the pointer variables are made to point to the square and cube of x. While calling the getPowers function from the main function we are passing x and the reference of xSqr and xCube as parameters and hence the values modified in the function reflect the values in the varaibles xSqr and xCube.
Hence x will be 3, xSqr will be 9 and xCube will be 27
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.