Comparing Strings using Pointers HELP!!

This is a discussion on Comparing Strings using Pointers HELP!! within the C++ Programming forums, part of the General Programming Boards category; Hi, im passing a pointer containing a string to a function im then using int isNumberString(char *pNumberStringPtr) { if(strcmp( *pNumberStringPtr, ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    34

    Comparing Strings using Pointers HELP!!

    Hi, im passing a pointer containing a string to a function im then using


    int isNumberString(char *pNumberStringPtr)
    {
    if(strcmp( *pNumberStringPtr, "ONE" ) != 0 )
    {
    return 0
    }
    else if (strcmp( *pNumberStringPtr, "ONE" ) = 0 )
    {

    return 1;
    }
    }

    this is a cut down version

    it come sup with this error


    error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'

    why is that, i dont understand

    any help would be really appreciated

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > if(strcmp( *pNumberStringPtr, "ONE" ) != 0 )
    Should be
    if(strcmp( pNumberStringPtr, "ONE" ) != 0 )

    pNumberStringPtr is already a char* (which is what strcmp expects). By putting a * in there, you dereference the pointer to the char it points at (*pNumberStringPtr is a char).

    Hence the error message
    cannot convert parameter 1 from 'char'
    > what *pNumberStringPtr is
    to 'const char *'
    > what strcmp expects (and what pNumberStringPtr is)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    ah so you mean change *pNumberStringPtr back to just pNumberString

    thanks alot ill give it a try, you helped me out again

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    if i change it back to pNumberString it tells me unknown identifier pNumberString as it is ohnly declared locally in the main function and not the one where i have sent it too, if you know what i mean

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    sorry i miss read your answer ill try again

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    You should also change:

    else if (strcmp( pNumberStringPtr, "ONE" ) = 0 )

    to

    else if (strcmp( pNumberStringPtr, "ONE" ) ==0 )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  2. Noob trouble: returning strings (pointers to char)
    By SgtMuffles in forum C Programming
    Replies: 4
    Last Post: 02-28-2005, 07:48 PM
  3. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 12:10 PM
  4. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 10:18 AM
  5. joining strings with pointers?
    By sworc66 in forum C Programming
    Replies: 6
    Last Post: 09-03-2002, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21