Problems printting pointer address

I have trhe following code, I am trying to print the pointer address using "cout" but is not working.

I put both, printf and cout , so you people can see the differences.

I hope someone can help me.


Thanks.


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>
#include <stdio.h>
#include <vector>

using namespace std;

int main()
{
    vector<int> vint_vector(10,99);
    vint_vector[0] = 1;
    vint_vector[1] = 2;
    vint_vector[2] = 3;
    vint_vector[3] = 4;


    cout << "BEGIN = " << &vint_vector.begin() << endl;          //---> prints a wrong address
    printf("BEGIN = %p \n\r",vint_vector.begin());
    for (int i = 0; i <= 10; i++)
    {
        cout << "i = " << i << " direccion vint_vector[i] = " << &vint_vector[i] << endl;
    }
    printf("END = %p \n\r", vint_vector.end());
    cout << "END = " << &vint_vector.end() << endl;             //---> prints a wrong address

    return 0;
}
why do you use & with cout? if hint_vector.begin() is the pointer at hint_vector.at(0), why did you care which value has the address of the pointer? removing the & from cout << "BEGIN = " << &vint_vector.begin() << endl; and cout << "END = " << &vint_vector.end() << endl; should print the right address
Last edited on
If I remove the "&" the compiler thoughts me the following messages:

C:\Documents and Settings\u545777\Mis documentos\bin\codeblocks\CodeBlocksPortable\Projects\STLtest1\main.cpp||In function 'int main()':|

C:\Documents and Settings\u545777\Mis documentos\bin\codeblocks\CodeBlocksPortable\Projects\STLtest1\main.cpp|16|error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char>&)(& std::cout)), ((const char*)"BEGIN = ")) << vint_vector.std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = int*]()'|


....

By the way , I use MingW with CodeBlocks
some person do mistake because they are think the iterator is "exactly like" pointer but its not ! if you want take the address of the them you must use "at" or "operator []" with "&" . however this code is wrong :

vector<char> v(10);
strcpy(v.begin(), "asdf");

you must correct this with

vector<char> v(10);
strcpy(&v[0], "asdf");
Last edited on
Thanks for the replies, but I am still in doubt with this issue.

How come that this 2 instructions return different values ?

1) cout << "BEGIN = " << &vint_vector.begin() << endl;

2) printf("BEGIN = %p \n\r",vint_vector.begin());

The second one, is the one thats returning the same value as &vint_vector[0] , so "printf" is doing the right thing , when I pass vint_vector.begin() . Using the same iterator with "cout" I dont get the same value as &vint_vector[0].

Aparently, Its not posible to use "cout" with an iterator, .. BUT I can with "printf".

I must use "printf" then, or "at" or "[0]"
because whenever you call begin its take a new location , exam :

cout << "BEGIN = " << &vint_vector.begin() << endl;
cout << "BEGIN = " << &vint_vector.begin() << endl;

and see your output.
I would think that vint_vector.begin() is a method ... therefore you are getting the address of the method within your class. So it's doing what is supposed to do, you just need to specify the first and last value more precisely.
ahura24 (132) Nov 16, 2011 at 1:19pm
because whenever you call begin its take a new location , exam :

cout << "BEGIN = " << &vint_vector.begin() << endl;
cout << "BEGIN = " << &vint_vector.begin() << endl;

and see your output.


Yes thats right, I am getting different values .... why ?

cppabuser (10) Nov 16, 2011 at 1:19pm
I would think that vint_vector.begin() is a method ... therefore you are getting the address of the method within your class. So it's doing what is supposed to do, you just need to specify the first and last value more precisely.
ico


I thought that too , but, ..... (here is my point)

cout << "BEGIN = " << &vint_vector.begin() << endl; ---> is returning a value

printf("BEGIN = %p \n\r",vint_vector.begin()); ----> is returning another value

The thing is that the one with "cout" != &vint_vector[0]

and

the one with "printf" == &vint_vector[0]


So .... if it works with printf , its should work with "cout" , maybe thats incorrect.

I dont care about using &vint_vector[0] instead of vint_vector.begin() to work with "cout", but .... its something weird a?

Thanks a lot for all the replies by the way.


another thing, besides that the same works withs "printf" but no with "cout", is that when I do

cout << "BEGIN = " << &vint_vector.begin() << endl;
cout << "BEGIN = " << &vint_vector.begin() << endl;

I get different locations, ... so , ..... where are we pointing ????
One could be getting the address of where the class itself starts, while the other could be getting the address of the method within that class. Try printf with %x modifier and &vint_vector.begin() just to see what happens.

Either way, neither of them are picking up the address of your data and using &vint_vector[0] would be the correct way to get that element's address.
Topic archived. No new replies allowed.