Code Segment Question
-
Consider the following section of code: int a = 10; int* b; Which of the following statements places the address of a in b. Remember, more than one of these options may be correct. In your response, explain which ones work and why, and also explain why the incorrect answers will not work. I want more than a listing of the syntax errors! b = a; b = &a; b = *a; *b = a; &b = &a; Loli10
-
Consider the following section of code: int a = 10; int* b; Which of the following statements places the address of a in b. Remember, more than one of these options may be correct. In your response, explain which ones work and why, and also explain why the incorrect answers will not work. I want more than a listing of the syntax errors! b = a; b = &a; b = *a; *b = a; &b = &a; Loli10
Is it your homework? Tomasz Sowinski -- http://www.shooltz.com
-
Is it your homework? Tomasz Sowinski -- http://www.shooltz.com
-
Consider the following section of code: int a = 10; int* b; Which of the following statements places the address of a in b. Remember, more than one of these options may be correct. In your response, explain which ones work and why, and also explain why the incorrect answers will not work. I want more than a listing of the syntax errors! b = a; b = &a; b = *a; *b = a; &b = &a; Loli10
Loli10: b=a; will give compile time error becasue it is not legal to assign the int value from a to the pointer value in b. b=&a; this moves the address of a to the pointer variable b....the answer you were looking for. b=*a; this is not legal because *a means get contents pointed to by a and yet a is not a pointer variable. *b = a; this is a legal statement IF b has a valid pointer in it. If so then the value of a is moved to the integer pointed to by b. &b=&a; this is not legal becasue it does not make logical sense to use the & address of operator on the left side of an = sign. Mike
-
Loli10: b=a; will give compile time error becasue it is not legal to assign the int value from a to the pointer value in b. b=&a; this moves the address of a to the pointer variable b....the answer you were looking for. b=*a; this is not legal because *a means get contents pointed to by a and yet a is not a pointer variable. *b = a; this is a legal statement IF b has a valid pointer in it. If so then the value of a is moved to the integer pointed to by b. &b=&a; this is not legal becasue it does not make logical sense to use the & address of operator on the left side of an = sign. Mike