-1

Hi everyone I have a simple task in C++:

-> writing a program that takes a string from user input and loops over the characters in the string via a pointer.

If I understand correctly, then a previously declared string name; variable can also be accessed via const char*, implying that I can declare a pointer in the following manner: const char *pName = &(name[0]);. When printing the pointer, however, not the memory address but the actual variable is displayed in the terminal (see my code below). This prevents me from incrementing the pointer (see for loop).

Filename: countchar.cpp

#include <iostream> 
using namespace std;

int main() {
    string name; 

    std::cout << "Provide a string." << endl;
    std::cin >> name;

    const char *pName = &(name[0]);
   

    cout << pName << endl;

    // further downstram implementation
    // int len = name.length();
    // for(int ii = 0; ii < len; ii++){
    //     std::cout << "iteration" << ii << "address" << pName << endl;
    //     std::cout << "Character:" << *pName << endl;
    //     (pName+1);
    // }

    return 0;
}

Terminal output:

$ g++ countchar.cpp -o count
$ ./count
$ Provide a string.
$ Test
$ Test 

As I am a quite a noob in regard to C++ help and an explanation are both highly appreciated (No material found online that solves my problem). Thanks in advance!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Why the string being displayed prevents you from incrementing the pointer? – MikeCAT Jul 22 '22 at 23:16
  • 1
    The reason why the loop is not "incrementing the pointer" is because nothing in the commented loop actually increments the pointer. Computing the address of the next character, and then completely throwing away and ignoring that address, is not incrementing anything. If you want to increment a pointer, well, increment it: `++pName;`. VTC as a typo. – Sam Varshavchik Jul 22 '22 at 23:17
  • You can cast pointers to `void*` to have `std::cout` print pointer values. – MikeCAT Jul 22 '22 at 23:17
  • 2
    `const char *pName = &(name[0]);` - I've been writing C++ code for years. Never this ... – Ted Lyngmo Jul 22 '22 at 23:18
  • See https://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value – Passer By Jul 22 '22 at 23:19
  • I'd recommend `name.data()` or `name.c_str()` over `&(name[0])` – ChrisMM Jul 22 '22 at 23:21

1 Answers1

1

The operator << overloaded for a pointer of the type char * such a way that it outputs the string pointed to by the pointer.

So according to the assignment instead of these statements

const char *pName = &(name[0]);


cout << pName << endl;

you need to use a loop like

for ( const char *pName = &name[0]; *pName != '\0'; ++pName )
{
    std::cout << *pName;
}
std::cout << '\n';
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Hi @Ted Lyngmo thanks for the help and the explanation. Maybe a stupid comment but what does "The operator << overloaded" mean in dummy terms... this the first C++ function that i wrote in my entire life... – lenskapopenska Jul 22 '22 at 23:27
  • @lenskapopenska I just edited a typo in Vlads answer so all thanks should go to him. – Ted Lyngmo Jul 22 '22 at 23:28
  • @lenskapopenska Under the hood the operator << used in this statement cout << pName << endl; is implemented as a function. The same operator can be used for objects of different types. So for each type the operator is implemented as functions with different parameters. – Vlad from Moscow Jul 22 '22 at 23:29
  • 1
    Oh apologies! Thanks Vlad! – lenskapopenska Jul 22 '22 at 23:29