Thread: [VERY Beginner question] Strings + functions + ...pointers?!

  1. #1
    Registered User arthurhess's Avatar
    Join Date
    Nov 2009
    Posts
    4

    [VERY Beginner question] Strings + functions + ...pointers?!

    Please, I'm sorry for such a noob question. I'm learning. The book is introducing me to char arrays without passing them as arguments...

    Then, I tried ...by myself ... Would be pretty nice if someone just ..explain me, "why should I mess with pointers", here...

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int stringCount(char);
    int main(int nNumberofArgs, char *pszArgs[])
    {
        char szInput[1024] = {0};
        cout << "Enter a word and press RETURN:\n";
        cin >> szInput;
        cout << stringCount(szInput);
    //    for (int nCounter = stringCount(szInput); nCounter >= 0; nCounter--) cout << szInput[nCounter];
        cout << endl;
        system("pause");
        return 0;
    }
    int stringCount(char szValue[])
    {
        for (int nCounter = 0; szValue[nCounter]; nCounter++)
        return nCounter;
    }
    And the compiler message:

    In function `int main(int, char**)':
    13 invalid conversion from `char*' to `char'
    13 initializing argument 1 of `int stringCount(char)'


    Don't blame me... I know about some of the useless stuff (main() arguments, maybe some libs that I included, or the namespace line... I'm learning about that)

    And sorry for my bad english... I'm a foreign speaker

    Thank you very much,
    Arthur

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Your prototype and function definition don't agree:
    Code:
    int stringCount(char);
    ...
    int stringCount(char szValue[])
    The two should generally be the same, except for name of the parameter and the semicolon at the end of the prototype. In this case you minimally need to add square brackets.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Prototype:
    Code:
    int stringCount(char);
    Definition:
    Code:
    int stringCount(char szValue[]);
    Those don't match. If you have a char[] in the definition, you need one in the prototype, too.

    It's too bad your book is teaching you C style strings. C++ strings are much easier to work with. As to why you should mess with pointers, the answer is too long to type in one post. (There have been many detailed and interesting threads on this forum about it if you want to search.) The reason they're used here is that you're using an array, and passing an array to a function and dealing with a array involves pointers.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Oh Yeah.

    Code:
        char szInput[1024] = {0};
        cin >> szInput;
    Never do this.

    If you must, you can do this:
    Code:
        char szInput[1024] = {0};
        cin >> setw(1024) >> szInput;
    As a rule you should make sure that nothing the user can do should crash your program. In the original version, if the user typed a string of non whitespace characters longer than 1023 characters long, the program could crash.

    This problem would not occur with C++ std::string objects.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User arthurhess's Avatar
    Join Date
    Nov 2009
    Posts
    4
    Hey, I'm glad now. Thanks for your answers, King Mir and Daved (EDIT: and since!). Really!

    I'll study harder. Btw, I heard about the strings lib. I just tried with arrays because....oh, I don't really know why I tried that way... I was thinking about, well.. having some fun Looks like it's too much for me right now lol

    Thank you very much for the answers, again...

    Arthur
    Last edited by arthurhess; 12-08-2009 at 02:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 02:21 PM
  2. Reg pointers to functions
    By sowmi in forum C Programming
    Replies: 1
    Last Post: 06-22-2007, 12:08 AM
  3. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. Pointers to pointers to strings
    By Natase in forum C Programming
    Replies: 2
    Last Post: 09-17-2001, 11:30 PM