New at Pointers!

Hi I am new here and new to the whole concept of OOP. SO I decided to take these online lessons. I reached pointers and I have a basic understanding but when given a task I am not sure whether I am correct or not..Maybe YOu guys could hel guide me? Please and Thank you!

SO I was given this class

1
2
3
4
5
6
7
8
9
10
11
  class SnipablePerson {
private:
string name;

public:
//constructor initialises a snipable person's name
SnipablePerson(string);
//this will print that the person has been sniped
void snipe();

};


It then goes on saying

We can instantiate objects of type SnipablePerson, each of which represents a potential target for
a sniper in a game. We can also declare a variable to represent a sniper, which will be of type
SnipablePerson * (i.e. a pointer to a SnipablePerson object), to represent the fact that a sniper
points to a snipable person.
Consider the following line-numbered code snippet from a main function:

1 SnipablePerson target( "Evil Dictator'');
2 SnipablePerson* sniper;
3 sniper = ⌖
4 *sniper.snipe();

OK so now for the questions...

One of the 4 lines contains a syntax error.
(a) Which line is it? Give only the line number.

ANSWER I said line 4

(b) Why is the line incorrect? Explain why in terms of the order of operations.

ANSWER I said because instead of a dot operator you should use an arrow operator

(c) Give two equivalent and correct versions of the erroneous line.
ANSWER Going on my last answer I could only give 1

*Sniper->Snipe

I really suck at pointers! So any help would be appreciated. Also Learners can also view this thread for their own interest!
Last edited on
AFAIK the line four is incorrect because of operator precedence.
*sniper is the deferenced object to which the pointer sniper points.

*sniper.snipe() is equivalent to *( sniper.snipe() ) which is wrong

Hence (*sniper).snipe(); would be valid syntax.
Also, sniper->snipe(); is valid.

However (*sniper)->snipe() is not OK because *sniper is the object target and you use a dot operator with objects.

You can go through the section on pointers on this website, it is very clear and concise.
line 1: wrong quotes

line 4 is correct: in this case * dereferences the pointer to the object.

[EDIT]due to the precedence line 4 isn't correct either: (*sniper).snipe(); // this is correct
Last edited on
Ok i see, I went through the pointers and I understand the theory behind them just not very clearly, Oh and is it fine If I post questions like this, or is that frowned upon?
Oh Sorry bout that coder777 was a typo :P
I get now why they ask to explain "in terms of the order of operations"
Ok then theres one more question also concerning pointers, this one I actually compiled and ran. However I get the same 'return' values for both programs. I do know they represent the same thing but the question wants it otherwise.

John is tasked with writing a function, called swap, that will take two pointers to integers as parameters
and swap the values at the memory locations they point to. Somewhat baed by the request, he asks
his friend Joan for help. She gives him one of her old homework assignments, where the task was
simply to swap two integer values. Her answer looked as follows:
1
2
3
4
5
void swap(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}

John adjusts Joan's function to match his own speci cations by changing every instance of int to int *. The resultant function is:
1
2
3
4
5
void swap(int *&a, int *&b) {
int *tmp = a;
a = b;
b = tmp;
}


Then the questions

(a) What e ffect will John's code actually have? Contrast the real behaviour with the expected behaviour.

I said It does swap a with b??


(b) Re-write the body of the function so that it will work correctly. Leave the function signature as

(c) After John has correctly implemented the body of the function, does he still need to pass the parameters by reference? In other words, should he leave the formal parameter list as (int *&a, int *&b), or can he remove the ampersands now? Only say whether reference passing is necessary.

(d) Explain your answer to (c). To do this, rst explain why Joan's function required passing by reference. Then explain whether this applies to John's code, and how the purpose of passing by reference is similar or di erent in John's function.


GUYS I really appreciate It... And at the same time learning the correct jargon that goes with it











The existing function does the following:

1
2
3
4
5
void swap(int *&a, int *&b) {  // function swap takes two input parameters where each is a reference of pointer to int.
int *tmp = a;  // tmp is pointer to int and it is now pointing to where a is pointing
a = b;             // a is now pointing to where b is pointing
b = tmp;         // b is now pointing to where tmp is pointing (where a was pointing earlier)
}


Hence in the above code, we are just swapping the pointers and not the values to which they were pointing. If 'a' was pointing to a variable 'x' and 'b' was pointing to a variable 'y', then after this function executes, a will point to variable y and b will point to variable x. However, the values of x and y shall remain unchanged.

1
2
3
4
5
void swap(int *a, int *b) {  // function swap takes two input parameters where each is a pointer to int.
int tmp = *a;   // tmp is an int that has the value at address a (value of x)
*a = *b;          // value at address a is now changed to value at address b
*b = tmp;        // value at address b is now changed to value of temp, i.e., the earlier value of address of a.
}


In Joan's function however, the input parameters taken reference was necessary because without the reference, variables a and b would just be new copies of the original variables. Changing their values would not change the original calling variables. Hence taking input as reference was necessary so that the original values get changed.

EDIT: Post corrected as suggested.
Last edited on
Should it not look something like this instead?

1
2
3
*a = *b;        
*b = tmp;       
}
Last edited on

abhishekm71 wrote:
1
2
void swap(int *&a, int *&b) {  // function swap takes two input parameters where each is a pointer to reference of int.
                  // AFAIK pointer to reference of int is simply a pointer to an int and you can remove the & sign. 


Not quite. a and b are references to a pointer to int. The function swaps the pointers instead of what is pointed to.

http://ideone.com/yRDb1w
Last edited on
sorry for the goof-up guys... Thanks for pointing out..
I have corrected my post.
Topic archived. No new replies allowed.