简体   繁体   中英

Explanation of some code

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 (ie 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?

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM