Help. Pointers problem. How to show addresses of each element of an char,string array

Hello,

Problem: i think i do not grasp all the tricks of a pointer. So, i need some help please. I want to see the address of char type and string type data, please see example 2 of what i mean(it is not working,it is wrong ). How can i do it? I want to see the physical address of each element of my array and i want to use only the name: ptr (pointer) not the name of the char array(case1), string array(case2).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// example 1 - it works for int,float,double, long double data type.

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{  
    int myname[3]={1,2,3};
    int *ptr;
    ptr = &myname[0]; // it is the same as: ptr= myname
         for (int i=0; i<=2; i++)
            cout << " " << ptr++ << endl;
    return 0;
}
// the above code will show me the addresses
//of each element of the int data type array: myname. 


Here come the purpose of my thread now...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//example2 - for string arrays and char arrays, how can i see
//with the cout command  the address of myname array? 
//I want to use only ptr variable to see the address 
//of each element of the array.

//case 1- char array (code problem, i need help to understand...)
    char myname[3];
    char *ptr;
    ptr = myname; //// it is the same as: ptr= &myname[0]
         for (int i=0;i<=2;i++)
            cout << " " << ptr++ << endl;

//case 2 - string array (code problem, i need help to understand...)

    string myname[]="this is a sentence .";
    string *ptr;
    ptr = &myname[0]; //// it is the same as: ptr= myname
         for (int i=0;i!='\0';i++)
            cout << " " << ptr++ << endl;


thank you for your kind help, if any :D



[]some bla bla bla below[]
I read the pointers tutorial on this forum, i submitted an example(it is example 1) to show you that i understand the basic idea of pointers. I know that the values of pointer variable = memory addresses. Then, i can have access to other variables and their values through the pointers. Also, i remember that arrays can be seen as pointers...
Last edited on
Operator<< is overloaded. The char* version prints a C-string.
1
2
char * ptr = ...
cout << " " << static_cast<void*>(ptr++) << endl;


However, your case 1&2 code fragments make no sense, because you don't set the ptr ...
However, your case 1&2 code fragments make no sense, because you don't set the ptr ...
He sets it at lines 9 and 17.

Keskiverto is right, but there' are other problems with your second case. First of all, the condition in the for loop is wrong. it basically says i!=0 but it's using constant character 0 instead of constant integer 0. As a result the loop never executes.

And consider string myname[]="this is a sentence ."; How many elements are in that array? And what are their values? Hmm. Let's find out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int
main()
{
    string myname[]="this is a sentence .";
    string *ptr;
    int elements = sizeof(myname)/ sizeof(myname[0]);
    ptr = &myname[0]; //// it is the same as: ptr= myname
    for (int i=0;i<elements;i++)
        cout << myname[i] << " at " << static_cast<void*>(ptr++) << '\n';
    return 0;
}

$ ./foo
this is a sentence . at 0x22ac1c
this is a sentence . at 0x22ac20
this is a sentence . at 0x22ac24
this is a sentence . at 0x22ac28
this is a sentence . at 0x22ac2c
this is a sentence . at 0x22ac30
this is a sentence . at 0x22ac34
this is a sentence . at 0x22ac38
this is a sentence . at 0x22ac3c
this is a sentence . at 0x22ac40
this is a sentence . at 0x22ac44
this is a sentence . at 0x22ac48
this is a sentence . at 0x22ac4c
this is a sentence . at 0x22ac50
this is a sentence . at 0x22ac54
this is a sentence . at 0x22ac58
this is a sentence . at 0x22ac5c
this is a sentence . at 0x22ac60
this is a sentence . at 0x22ac64
this is a sentence . at 0x22ac68
this is a sentence . at 0x22ac6c

What the heck?!? It's possible that I've stumbled on a bug in MinGW. Let's look at that declaration again:
string myname[]="this is a sentence .";
"this is a sentence ." is an array of const chars that is 21 elements long. That explains why myname has 21 elements. But I don't know why they all have the same value. It seems like a bug to me. Maybe someone else can comment.

The whole concept of using plain array of strings is very strange.

That array declaration seems to create an array with 21 std::string objects and initialize each of them with C-string "this is a sentence ."

If you would use
string myname[] = { "this is a sentence ." };
The array would have only one element, one string, which stores the "this is a sentence ."

C++ has many rules about initialization.


PS. one does not need to cast a pointer to string into void pointer, because that conversion is implicit and thus the address of the string object will print. Only the char pointer is special.
Topic archived. No new replies allowed.