M-West M-West - 6 months ago 47
C++ Question

Output position of Pointer in string C++

The code I have below at the moment outputs the letter H which is in position 0 but I want to know if there is a way to also output not just the letter H but also the pointer position of the letter H.


#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int main(){
const char* letterPointer = "Harry";
cout << letterPointer[0] << endl
return 0;


It's a common technique to cast to void * from char * for that purpose, as follows:

cout << static_cast<const void*>(&letterPointer[0]) << endl;

By the way, in C++, string does NOT mean char *, but rather std::string. So this include line is not necessary in your code.

#include <string> // You'd probably want to remove this line.