Thread: Simple problem with string... Please take a look

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    45

    Simple problem with string... Please take a look

    Im using the following scratch code to take a string (stream/buffer, w.e) and run it through this function to replace whateer char i give it, with another char i give it...
    Code:
    #include <string.h>
    #include <stdio.h>
    
    extern int Replacing(const char* replacing, char replacement, char* string)
    {
      char *string_ptr;
      while((string_ptr=strpbrk(string,replacing))!=NULL)
        *string_ptr=replacement;
    return *string_ptr;
    }
    
    int main() {
               char *buffer;
               int string;
               char test[] = "This is a testing string";
               *buffer = Replacing(" ", '-', test);
               string = &buffer;
               printf("\nAfter:\n%s", string);
               while(1) {  
                       }
    return 0;
    }
    It should read the string, and replace all ' ' AKA spaces, with '-' or minus signs...

    The error im getting when using the Bloodshed Dev-C++ MingGW Complier is:
    Code:
    assignment makes integer from pointer without a cast
    This obviously is the problem as it is the only error (warning really)...

    Im sure its a simple answer, Im 14, so bear with me... Started programming about 2 days a go, do you think im making good progress?
    Last edited by SG57; 06-02-2006 at 11:20 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If you are replacing one char with another char, then your function prototype should look something like:

    void replace_char(char *str, char cOld, char cNew);

    Why are you passing two strings into your function? Think about what you are trying to accomplish.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    ok... Ill look it over now... its just the ' ' that im hving trouble with... I cant quite find how to pass a char into it legally... Ive seen it done somewhere... In an unsinged short array though I think... but ah well, im gonna wing it til i get an answer that will point me EXACTLy to the problem... Thanks for the advice!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You pass char values with single quotes. If you were to use the function prototype I made, it would look like:

    replace_char(someStr, 'a', 'b');

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    No... here is my code now:
    Code:
     1 : #include <string.h>
     2 : #include <stdio.h>
     3 :
     4 : extern int Replacing(char *string, char replacing, char replacement)
     5 : {
     6 :
     7 :   char * string_ptr;
     8 :
     9 :   while((string_ptr=strpbrk(string,replacing))!=NULL)
    10 :
    11 :     *string_ptr=replacement;
    12 :
    13 : return * string_ptr;
    14 : }
    15 :
    16 : int main() {
    17 :
    18 :            char string[255] = "This is testing...";
    19 :
    20 :            char * buffer;
    21 :
    22 :            printf("Before:\n%s", string);
    23 :
    24 :            *buffer = Replacing(string, ' ', '-');
    25 :            printf("\nAfter:\n");
    26 :            printf("%c", *buffer);
    27 :
    28 :            getch();
    29 :
    30 : return 0;
    31 :
    32 : }
    The error im getting when complying is:
    Code:
    in function 'Replacing':
    [Warning] passing arg 2 of `strpbrk' makes pointer from integer without a cast
    If i make the 2nd argument prototype have a * in it, it complys but doesnt work...

    P.S. The line numbering was done by my other C examples ive made so far Seems like it works?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you sure your compiler even has strpbrk? It's not an ANSI standard function.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Why else would the including of the headers work with out an unkown file error?

    Oh adn its that MingGW (something like that) in the Bloodshed Dev-C++...

    Can you test this to see if its working?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try this
    Code:
    #include <string.h>
    #include <stdio.h>
    
    char *Replacing(const char* replacing, char replacement, char* string)
    {
      char *string_ptr;
      while((string_ptr=strpbrk(string,replacing))!=NULL  )
        *string_ptr=replacement;
      return string;
    }
    
    int main() {
      char *buffer;
      char test[] = "This is a testing string";
      buffer = Replacing(" ", '-', test);
      printf("\nAfter:\n%s", buffer);
      return 0;
    }
    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.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    ok... ill tell you soon...Yup it worked, tahnks alot... any idea why mine wasn't working? I have somewhat of an idea now... (but its like my 3rd day of first being introduced to programming so that's my excuse )
    Last edited by SG57; 06-03-2006 at 06:31 AM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You were using a single character, not a string.
    Code:
     4 : extern int Replacing(char *string, char replacing, char replacement)
    
     9 :   while((string_ptr=strpbrk(string,replacing))!=NULL  )
    Pay attention to what arguments functions expect:Quzah.
    Last edited by quzah; 06-03-2006 at 06:51 AM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. C/C++ String Problem.
    By Jaken Veina in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2005, 10:11 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM