test question I was given (I wanna know the answer and am not sure I was right)

Hey guys, my professor had a question on my test that basically tested my understand of pointers. (I won't write the program in its entirety just the main piece.

*q=22
p=q
*p=33
cout << *q << *p << endl;

What's the output?

At first I thought it was the addresses of p and q but since the pointers themselves were being initialized I said the output was 2233.
Was I right? If not, what's it suppose to be?

I posted this question because I've had it on the brain ever since.
If you've had it on your brain, perhaps you should try it out in code.
1
2
3
4
5

*q = 22     // assign the value 22 to the area of memory pointed to by q.
p = q         //  p points to the same area of memory as q.
*p = 33     // assign the value 33 to the area of memory pointed to by p and q.
                 // because p and q point to the same area of memory. 


The output would be 3333.
cire wrote:
// p points to the same area of memory as q.
wouldn't be only if the statement read *p=*q?
No.

*p means the value pointed at by p.
*q means the value pointed at by q.

So *p = *q means the value pointed at by p is set to the value pointed at by q.

p and q are pointers, i.e. variables that hold addresses.

p=q means set the address held by p equal to the address held by q.

I tried to test that code in codeblocks, The output should be 3333, oddly enough when I tried to use the line (cout<<*p<<*q<<endl;) my debugger threw several strange errors about *q not being initialized. was anyone able to test this code? it seamed to run fine after I separated it into two separate cout statements (output was 3333) but could anyone run this in a single cout?

I also tried the code in visual basic C++ and that I couldn't get to run at all.
Last edited on
cire, science man and newbieg,
all of you guys are being silly :D

The output would be 3333.
the output was 2233.


How the heck output could be 3333 or 2233 ?

What kind of computer are you using? lol



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
	int	// define initial values
		holder1 = 33, 
		holder2 = 22;

	int* // initialize pointers 
		p = &holder1, 
		*q = &holder2;

	// assign them now!
	p = q;

	// show me result
	cout << *p << endl << *q;

	// pause the program
	cin.ignore();

	return 0;
}


the output is:
22
22

or 2222 if you skip std::endl
Last edited on
You're program doesn't do the operations in the same order as the snippet given. Try:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {
    int *p;
    int *q = new int;
    *q = 22;
    p = q;
    *p = 33;
    std::cout << *p << *q << std::endl;
    delete q;
    return 0;
}

I tried to test that code in codeblocks, The output should be 3333, oddly enough when I tried to use the line (cout<<*p<<*q<<endl;) my debugger threw several strange errors about *q not being initialized. was anyone able to test this code? it seamed to run fine after I separated it into two separate cout statements (output was 3333) but could anyone run this in a single cout?


Well, these were snippets. In order to "test" the code without running into undefined behavior you would have to add a few things:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

void display_pointers( int const* q, int const* p)
{
	std::cout << "q=" << q << "     *q=" << *q << "\np=" << p << "     *p=" << *p << '\n' ;
}

int main()
{
	int initial_q_storage = 0xdeadbeef;
	int initial_p_storage = 0xbadf00d;

	int *q = &initial_q_storage ;
	int *p = &initial_p_storage ;

	display_pointers(q,p) ;

	std::cout << "\n\n*q = 22\n" ;
	*q = 22 ;
	display_pointers(q,p) ;

	std::cout << "\n\np = q\n" ;
	p = q ;
	display_pointers(q,p) ;

	std::cout << "\n\n*p = 33\n" ;
	*p = 33 ;
	display_pointers(q,p) ;
}
q=0020F868     *q=-559038737
p=0020F85C     *p=195948557


*q = 22
q=0020F868     *q=22
p=0020F85C     *p=195948557


p = q
q=0020F868     *q=22
p=0020F868     *p=22


*p = 33
q=0020F868     *q=33
p=0020F868     *p=33

> I tried to test that code in codeblocks, The output should be 3333, oddly
> enough when I tried to use the line (cout<<*p<<*q<<endl;) my debugger
> threw several strange errors about *q not being initialized.
> was anyone able to test this code?

Yes. I tried it. And got the output:
hello world hello world hello


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

struct A
{
    A& operator= (int) { std::cout << "hello " ; return *this ; }
    operator const char* () const
    { static int i = 1 ; return ++i%2 ? "world " : "hello " ; }
};

struct B
{
    B& operator= ( const B& ) { std::cout << "world " ; return *this ; }
    A operator* () const { return A() ; }
};

int main()
{
    B p, q ;
    using namespace std ;

    *q = 22 ;
    p = q ;
    *p = 33 ;
    cout << *q << *p << endl ;
}
cire wrote:
No.


No.

*p means the value pointed at by p.
*q means the value pointed at by q.

So *p = *q means the value pointed at by p is set to the value pointed at by q.

p and q are pointers, i.e. variables that hold addresses.

p=q means set the address held by p equal to the address held by q.



Oh ok Gottcha. I had the idea of how to treat p vs *p reversed.
Topic archived. No new replies allowed.