Thread: trouble with strcpy

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    12

    trouble with strcpy

    Hi, I'm having trouble with the strcpy function:

    In my program, I'm trying to see if the last character of the a character string, called charstring, is a backslash.

    Code:
        char ch = charstring[strlen(charstring)-1];
        
        if (strcmp(ch, "/") == 0) {
        	cout << "ok" << endl;
        }
    I get this error when I compile this:

    invalid conversion from `char' to `const char*'

    I've tried creating pointers, but it still outputs the same error.
    Does anyone have any ideas why this is happening?

    Thanks

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    ch is a character, only one, not a string. If you want to check if it is a backslash, you should do this

    if(ch == '\\')
    /* do stuff */

    and this is a c forum, not c++.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    since the strcmp takes two parameters which are "const char *", so u cant pass the normal characters to it.

    bye
    Praveen

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Originally posted by praveen_23
    since the strcmp takes two parameters which are "const char *", so u cant pass the normal characters to it.

    bye
    Praveen
    you can compare individual characters just like comparing numbers (since that's all it is).

    Code:
    char foo = 'x';
    
    if (foo == 'x') {
    	printf("Golly, foo equals 'x'\n");
    } else {
    	printf("Something wrong here.\n");
    }
    or even better:
    Code:
    char bar[] = "Hello\\";
    
    if (bar[strlen(bar) - 1] == '\\') {
    	printf("Bar ends in a backslash\n");
    }
    Last edited by Brian; 11-26-2003 at 03:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  4. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM
  5. HELP!! strcpy
    By abbynormal87 in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2002, 07:34 AM