Thread: I little basic question

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    I little basic question

    Hi! This is my code:

    Code:
    ...
    char tmpchar[255];
    ...
    printf("Enter the filename: ");
    fgets(tmpchar,MAXFILES,stdin);
    strcpy(tmpchar,ws2_());
    printf("%s\n",tmpchar);
    ....
    And this is my WhiteSpace2_ function:
    Code:
    char *ws2_(void) {
    char *ws2_tmpchar[255];
    int i;
    for(i = 0;i<=strlen(tmpchar);i++)
    {
        if(strcmp(tmpchar[i]," ") == 0)
              strcat(ws2_tmpchar[i],"_");
        else
              strcat(ws2_tmpchar[i],tmpchar[i]);
    }
    return ws2_tmpchar;
    Ok, afaik this should work. But it doesn't and gcc gives me warnings:
    cast to pointer from integer of different size
    passing arg 1 of 'strcmp' makes pointer from integer without a cast
    return from incompatible pointer type


    Thats not good . First: Why does tmpchar[i] does not work ? And why is the returning of ws2_tmpchar incompatible ?
    Thx for attention, Demonus

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *ws2_tmpchar[255];
    is an array of pointers, not an array of chars. Try
    >>char ws2_tmpchar[255];

    You're returning a pointer to a local variable, which will go out of scope after the function ends.

    strcmp() compares strings. Lookup isspace() for testing a char for being a space, tab, newline etc.

    There are other problems, see how you get on and post again when you have troubles.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    34
    Originally posted by Hammer
    >>char *ws2_tmpchar[255];
    is an array of pointers, not an array of chars. Try
    >>char ws2_tmpchar[255];

    You're returning a pointer to a local variable, which will go out of scope after the function ends.

    strcmp() compares strings. Lookup isspace() for testing a char for being a space, tab, newline etc.

    There are other problems, see how you get on and post again when you have troubles.

    Ok, nice thx, but thats not the problem. the problem is that
    Code:
    char tmpchar[255];
    char ws2_tmpchar[255];
    cannot be used in
    Code:
    strcat(ws2_tmpchar[i],tmpchar[i]);
    My real question was: why ? but thx for pointing to the other errors

    demonus

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Because strcat() is for strings (arrays of chars), not for single chars, which is what tmpchar[i] is.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Ok, nice thx, but thats not the problem.

    Apparently it is a problem, because you go on to say:

    >> char tmpchar[255];
    >> char ws2_tmpchar[255];

    The next problem is you can't return 'ws2_tmpchar' from the function. Why? When the function exits, that piece of memory becomes invalid. So either pass an array *into* the function or else make the array 'static' (or global).

    Another problem:

    >> char tmpchar[255];
    >> fgets(tmpchar,MAXFILES,stdin);

    If MAXFILES > 255, you will have an access violation in your program, and it will crash.

    >> for(i = 0;i<=strlen(tmpchar);i++)

    Here you are calling strlen() over and over, though the length of the string doesn't change. Call it once, and store that in a variable.

    Here's an easy, reusable way to achieve your goal:

    Code:
    char *
     replace(char str[], char find, char exchange)
    {
     int index, length = strlen(str);
    
         for(index = 0; index < length; ++index)
        {
             if(str[index] == find)
              str[index] = exchange;
        }
    
     return str;
    }

    Code:
    char *
     ws2_(char str[])
    {
     return replace(str, ' ', '_');
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM