C function only works with string literal

This is a discussion on C function only works with string literal within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use a C function created by someone else. here's it's declaration: Code: static void MDString(char *); The ...

  1. #1
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question C function only works with string literal

    I'm trying to use a C function created by someone else.

    here's it's declaration:
    Code:
    static void MDString(char *);
    The code works just fine if I call the function like this:
    Code:
    MDString ("someString");
    but it won't work if I call it with a c string from the console:
    Code:
      char *input;
      cout<< endl << "Enter a string to be hashed: ";
      cin>>input;
      MDString(input);
    I had been working on this problem earlier today and I got it to work ... but then I tried to clean up the code a few hours later and it no longer works

    It compiles but crashes as soon as I call the function

    I can't remember What I did, only that I only changed the code you see above. What are String Literals terminated with? Are they terminated at all? I think the answer might have something to do with that.
    Last edited by A10; 02-04-2008 at 09:44 PM. Reason: forgot to state the problem

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does >> leave the \n at the end of the input? (I'm asking because I don't know.) If it does, that may lead to a different result.

    Edit: Or, to miss the obvious, do you ever allocate space for this string? You have a pointer, but you don't own the memory it points to (at least in the snippet you posted).
    Last edited by tabstop; 02-04-2008 at 09:49 PM.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    to call in such way
    Code:
    MDString ("someString");
    you should declare it as
    Code:
    static void MDString(const char *);
    in this case, to avoid messing with allocations you can write your snipplet as
    Code:
      std::string input;
      cout<< endl << "Enter a string to be hashed: ";
      cin>>input;
      MDString(input.c_str());
    But I do not understand 1 thing - if your functon does not modify the original string and does not return value, what it DOES do?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Smile

    Quote Originally Posted by vart View Post
    to call in such way

    Code:
      std::string input;
      cout<< endl << "Enter a string to be hashed: ";
      cin>>input;
      MDString(input.c_str());
    That's exactly what I did and with one little modification:
    Code:
    MDString((char*)input.c_str());
    It works
    Quote Originally Posted by vart View Post
    But I do not understand 1 thing - if your functon does not modify the original string and does not return value, what it DOES do?
    It's an implementation of the MD4 algorithm. I took it from the 1992 memo and Spent today getting rid of there compiler specific macros so that I could actually use it.

    It prints out the hashed string within it's self which is why it's not apparent. I'm gonna change that to get it to do what I want.

    Thanks vart

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    MDString((char*)input.c_str());
    You tell the function that the passed string is modifiable. But it is not. So your asking for trouble. Better change the function prototype to accept const char*.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Cool

    You tell the function that the passed string is modifiable. But it is not. So your asking for trouble. Better change the function prototype to accept const char*.
    Will do. The value doesn't get modified in the function but just in case I mess some part of it up I'll do that. C doesn't have a const keyword does it?


    Thanks for your help

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > C doesn't have a const keyword does it?
    Why wouldn't it?

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by A10 View Post
    C doesn't have a const keyword does it?
    Only if you use some stone-age compiler
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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