Thread: Problems passing string to function

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    21

    Problems passing string to function

    Hi,

    I'm doing a simple dll which has a function that receives a string, makes some calculations, and returns one. Its of the type:

    Code:
    extern "C" __declspec(dllexport) char *read_c(char* c)
    {
    
        static char res[2048];
    
            //etc
    
        strcat(res, c);
    
        return res;
    }
    If I give it a string like "Hi world", I get in return "H" (the first character).
    I thought the function was wrong in the return, so I changed the line:

    strcat(res, c);

    to

    strcat(res, "Hi world");

    and got "Hi world". So the problem is in the passing the string c.

    Can someone help me out here?

    Kind regards,

    JKepler

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Are you sure you don't change the contents of 'c' somewhere in there? If your code's too long to simply browse through it, make 'c' 'const' and see where the compiler complains.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    21
    Hi,

    No...don't have a clue so far...

    Kind regards

    JKepler

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you believe that the problem is with passing the string, then test with this code:
    Code:
    extern "C" __declspec(dllexport) char *read_c(char* c)
    {
        static char res[2048];
    
        res[0] = '\0';
        strcat(res, c);
    
        return res;
    }
    Any difference? Notice that I removed the comment because this is literally the entire implementation of read_c. Nothing else, no other code that you have not shown us. Do you still have the problem?

    EDIT:
    Oh, you should do more: #include <assert.h> and #include <string.h> and do a pre-condition check:
    Code:
    extern "C" __declspec(dllexport) char *read_c(char* c)
    {
        static char res[2048];
    
        assert(c && strlen(c) < sizeof(res));
    
        res[0] = '\0';
        strcat(res, c);
    
        return res;
    }
    But given that you're testing with "Hi world" this probably doesn't matter right now.
    Last edited by laserlight; 06-02-2017 at 06:27 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    21
    Quote Originally Posted by laserlight View Post
    If you believe that the problem is with passing the string, then test with this code:
    Code:
    extern "C" __declspec(dllexport) char *read_c(char* c)
    {
        static char res[2048];
    
        res[0] = '\0';
        strcat(res, c);
    
        return res;
    }
    Any difference? Notice that I removed the comment because this is literally the entire implementation of read_c. Nothing else, no other code that you have not shown us. Do you still have the problem?

    EDIT:
    Oh, you should do more: #include <assert.h> and #include <string.h> and do a pre-condition check:
    Code:
    extern "C" __declspec(dllexport) char *read_c(char* c)
    {
        static char res[2048];
    
        assert(c && strlen(c) < sizeof(res));
    
        res[0] = '\0';
        strcat(res, c);
    
        return res;
    }
    But given that you're testing with "Hi world" this probably doesn't matter right now.

    Good morning,

    It worked very well laserlight Thanks.

    Have a nice weekend.

    Kind regards,

    JKepler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in passing 2-D array to a function
    By boral in forum C Programming
    Replies: 4
    Last Post: 09-23-2014, 01:45 AM
  2. Replies: 5
    Last Post: 10-24-2011, 12:35 PM
  3. Problems passing parameter to function
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 10-21-2008, 02:26 PM
  4. Passing address to a function, Problems !
    By WarDoGG in forum C Programming
    Replies: 4
    Last Post: 07-10-2008, 03:58 PM
  5. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 08:14 AM

Tags for this Thread