0

I'm reading a book on C++ (A tour of C++, by Bjarne Stroustrup, second edition) and there is an example of code:

int count_x(char* p,char x)
{
    if (p==nullptr) return 0;
    int count = 0;
    for(;*p!=0;++p)
        if (*p==x)
            ++count;
    return count;
}

In this book, it is explained that the pointer p of the function have to point to an array of char (i.e a string ?).

So I tried this code in main:

string a = "Pouet";
string* p = &a;
int count = count_x(p, a);

But count_x needs char not string so It does not compile. So I tried:

char a[5] {'P','o','u','e','t'};
char* p = &a;
int count = count_x(p, a);

But of course it won't work since the pointer alone can't point to a full array. So, lastly I tried to make an array of pointer:

 char a[5] {'P','o','u','e','t'};
 char* p[5] {&a[0],&a[1],&a[2],&a[3],&a[4]};
 int count = count_x(p, a);

But the function wouldn't accept arrays since it wasn't only a char.

So, I have no idea on how to use the count_x function (which is supposed count the number of x in p.

Could you please give me an example of working code which uses this function?

dandan78
  • 13,328
  • 13
  • 64
  • 78
M-Gregoire
  • 808
  • 9
  • 22
  • 1
    Nope, an array of char isn't the same as a string object, and I would start with some C++ basics before Stroustrup's books. – Marco A. Oct 29 '14 at 10:31
  • "The definition of count_x() assumes that the char* is a C-style string" (From "A tour of C++", Stroustrup 2013). – molbdnilo Oct 29 '14 at 11:11

2 Answers2

1

The sample function count_x counts the number of times a character 'x' appears in the 'a' array of char. Thus, you have to pass to count_x function two parameters: the array, and the character:

char a[5] {'P','o','u','e','t'};
char* p = &a;
int count = count_x(p, a);  //This does not work because 'a' is not a character, but an array of characters.

//This is wrong because you are passing an array of pointers to chars, not an array of chars.
char a[5] {'P','o','u','e','t'};
char* p[5] {&a[0],&a[1],&a[2],&a[3],&a[4]};
int count = count_x(p, a);`

The correct way would be:

char a[5] {'P','o','u','e','t'};
char* p = a; //IT IS NOT &a, since 'a' can is already an array of chars. &a would be a pointer to array of chars
int count = count_x(p, 'o');  //Passing p, an array of chars, and one character to search

Or

char a[5] {'P','o','u','e','t'};
int count = count_x(a, 'o');  //Passing a, an array of chars, and one character to search

Or

char a[5] {'P','o','u','e','t'};
char c='u';
int count = count_x(a, c);  //Passing a, an array of chars, and one character to search
LoPiTaL
  • 2,495
  • 16
  • 23
  • I don't understand why is `char a[5] {'P','o','u','e','t'}; int count = count_x(a, 'o'); //Passing a, an array of chars, and one character to search` working since the function wan't a pointer and you give it an array of char EDIT: I don't understand either why `char *p = "MySentence"` is working ! – M-Gregoire Oct 29 '14 at 10:38
  • Maybe worth to mention: The fist case can be used like this: `count_x(a.c_str(),'o')` if the function `count_x` is changed to `int count_x(const char* p,char x)` – tgmath Oct 29 '14 at 10:46
  • Good idea ! Thanks. For my questions above, the problem was about array decaying ! – M-Gregoire Oct 29 '14 at 10:49
1

The count_x function counts the number of occurrences of the given character in the input char array.

In order to call it correctly, you need to supply to it a char pointer pointing to a null-terminated char array and a character.

In your first example, you're trying to pass a string object as a char pointer, this is wrong since they're two totally unrelated types although they might contain characters at the end of the day.

string a = "Pouet";
string* p = &a;
int count = count_x(p, a); // Both arguments are wrong

Your second attempt fails too:

char a[5] {'P', 'o', 'u', 'e', 't'}; // Lacks zero terminator
char* p = &a; // Invalid, address of array assigned to char pointer
int count = count_x(p, a); // Second argument is wrong, it wants a char

and so does the third:

char a[5] {'P', 'o', 'u', 'e', 't'}; // Ditto as above
char* p[5] {&a[0], &a[1], &a[2], &a[3], &a[4]}; // Array of char pointers
int count = count_x(p, a); // Both totally wrong

The correct way of doing this is to remember array decaying and pass a null-terminated char array through a pointer to the first element:

char a[6] = "Pouet"; // Notice the '6' that accounts for '\0' terminator
char* p = a; // Array decays into a pointer
int count = count_x(p, 'o');
Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246