reading a string from a pointer

How do you go about reading a string from memory when you only have the pointer. For example &ptr is 0x7ffeb7ec2328. How can I read from this pointer location to the end of the string?
1
2
3
4
5
6
#include <cstdio>
int main()
{
    char const *ptr = "Hello, World!\n"; 
    while (std::putc(*ptr++, stdout) > 0) ;
}

Last edited on
But how do you read from a pointer when you only have the reference to it?
What's your actual problem? This way I don't have to guess any more.

Are you saying you have the address of a pointer to char?
…I will take a guess.

char *duh; //a character pointer.
duh = new char[100];
sprint(duh, "I am a string");
&duh; // a pointer to a pointer. This isn't a reference, its the address of the pointer, which is the same as char ** … you can't do anything useful with this.

you may actually have a reference.. you can ignore that as far as accessing it goes:
void foo(char *&wtf)
cout << wtf << endl; //the & just lets you modify the pointer itself, which might be useful if you wanted to destroy the old one and make a new one with length 200 instead.

char* IS a string.
you can write it:
cout << duh << endl;

you can read it
cin >> duh;

you can play with its characters individually
for(I = 0; I < strlen(duh); I++)
duh[I] = 'x';

and so on.

Does any of that get you where you wanted to be?



Topic archived. No new replies allowed.