concatenate 2 const char * problem

This is a discussion on concatenate 2 const char * problem within the C Programming forums, part of the General Programming Boards category; This works: Code: void myfunction( const char *file,const char *function, int index, int value ) { // code } #define ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    47

    concatenate 2 const char * problem

    This works:
    Code:
    void myfunction( const char *file,const char *function, int index, int value )
    {
    // code
    }
    
    #define myfunction(...) myfunction( __FILE__, __func__, __VA_ARGS__ )
    This does not, I get seg fault
    Code:
    void myfunction( const char *info, int index, int value )
    {
    // code
    }
    
    #define myfunction(...) myfunction( strncat(__FILE__, __func__, strlen(__func__)), __VA_ARGS__ )
    I'm trying to create one string with information about the file and hide this fact from the callers using my function. This is so if i'm debugging i can trace a problem faster.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    __FILE__ and __func__ are string literals so you can't modify them (which strncat does).

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    47
    Actually you are mistaken. __FILE__ is a string literal and can be concatenated, as I have done so with __FILE__ and __LINE__ by stringifying __LINE__. __FUNCTION__ aka __func__ is a treated as a variable now in an attempts at standardization and is a string constant and cannot be modified. I am simply trying to take all of this information and representing it as one string, can you help me do this or not? Although __func__ cannot be concatenated onto __FILE__, i merely used it as an example of what I was trying to accomplish.
    Last edited by tempster09; 12-20-2009 at 04:13 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You cannot change "hello" to "hello world" using strcat:
    Code:
    strcat( "hello", " world" );
    Both are string literals, and it will attempt to modify the first literal--which is undefined behavior. Modifying string literals is not something you should ever try to do, because it is not guaranteed to work, because string literals may be stored in read only memory. You can't modify read only memory. That's why it's called read only.

    Do it in two calls to a buffer of your choosing:
    Code:
    strcat( mybuf, __FILE__ );
    strcat( mybuf, __func__ );

    Quzah.
    Last edited by quzah; 12-20-2009 at 05:44 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Wouldn't something like sprintf(bigBuf, "%s%s", constCharPtr1, constCharPtr2); do what you need?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    47
    No, essentially what's going on is this

    Code:
    main.c
    int main()
    {
       //call function in external file extern.c
       extern(__FILE__,__LINE__,__func__)
    }
    
    extern.c
    
    int extern(const char*file, int line, const char *func)
    {
        //print file, line, func
        //print this file's file, line, func
    }
    The goal is to macro the extern() function call so it only has ONE parameter, not known to the caller for debugging purposes, i.e. extern( char *mssg), but the caller would only need to know extern() to use it. Concatenating __FILE__ and __LINE__ is easy within a macro, but no matter what I try I can't get __func__ to work properly. If i create my own function to concatenate strings, it complains i'm returning a local variable which is a char [], otherwise i must allocate memory for a char * which i guess could work. I'm not sure you guys clearly understand what i'm trying to do, everything is seperate from eachother, everything is modular, but i need to know where a call came from, and where it ended up. This concatenation needs to take place when the original calling functions calls the external code, because this is the first time the __FILE__ __LINE__ and __func__ needs to be grouped together into a single paramter and passed to the external function.
    Last edited by tempster09; 12-20-2009 at 06:33 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    __func__ is not a string literal. It's a "block scoped variable" that supplies the name of the enclosing function.
    Quote Originally Posted by C: A Reference Manual, 5th edition, page 51
    The C99 identifier __func__ (Section 2.6.1) is similar in purpose to __LINE__, but is actually a block-scope variable, not a macro. It supplies the name of the enclosing function.

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

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    47
    Thanks for enlightening me as opposed to helping me, i'll come up with something I guess. Thanks anyways. I mean why is it so difficult to give me a straight answer about grouping all three of __LINE__ __FILE__ __func__ into a single string which is what i've asked a dozen ttimes. I just did it the long, ugly way since nobody felt like helping me out, unfortunately yet again im stuck using a solution i am unhappy with because nobody wants to tell me another way to do it.
    Last edited by tempster09; 12-20-2009 at 06:51 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tempster09 View Post
    Thanks for enlightening me as opposed to helping me, i'll come up with something I guess. Thanks anyways. I mean why is it so difficult to give me a straight answer about grouping all three of __LINE__ __FILE__ __func__ into a single string which is what i've asked a dozen ttimes. I just did it the long, ugly way since nobody felt like helping me out, unfortunately yet again im stuck using a solution i am unhappy with because nobody wants to tell me another way to do it.
    Don't be such a baby. We already told you how to do it, two different ways. You can't just concatenate in one lump without doing what we've told you. Read! We've been telling you what you have to do. It's not a macro! It's not a string literal! You cannot modify string literals and have it be portable!

    Just because you want something to work some way, doesn't mean it does and will. If you can't understand this, programming isn't for you. Quit now.


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

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    47
    Well i should have mentioned I exhausted all of the simple options already, and can't use certain options. I would like to avoid the overhead of a function which allocates memory for a string, performs the concatenation, than making me keep track of the memory and cleaning it up. Im used to working with scripting languages, not soo much C

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It really wouldn't matter if you mentioned that or not. You can't concatenate a string literal and a string without allocating space for the string + literal + nul. You just cant do it, as we've said multiple times. We're not just withholding information from you to be mean. You can't do what you want to do, the way you want to do it.


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

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by tempster09 View Post
    I'm trying to create one string with information about the file and hide this fact from the callers using my function. This is so if i'm debugging i can trace a problem faster.
    The usual way to do this is with a DEBUG macro embedded in your code and compiling with the -DDEBUG switch, as in
    Code:
    #ifdef DEBUG
         ...
    #endif
    
    cc -DDEBUG <source_file>

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by tempster09 View Post
    Thanks for enlightening me as opposed to helping me, i'll come up with something I guess. Thanks anyways. I mean why is it so difficult to give me a straight answer about grouping all three of __LINE__ __FILE__ __func__ into a single string which is what i've asked a dozen ttimes. I just did it the long, ugly way since nobody felt like helping me out, unfortunately yet again im stuck using a solution i am unhappy with because nobody wants to tell me another way to do it.
    Several posters have given viable solutions but you choose to ignore 'em all.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by tempster09 View Post
    Well i should have mentioned I exhausted all of the simple options already, and can't use certain options.
    Perhaps you haven't exhausted all the simple ones yet.
    Quote Originally Posted by tempster09 View Post
    I would like to avoid the overhead of a function which allocates memory for a string, performs the concatenation, than making me keep track of the memory and cleaning it up. Im used to working with scripting languages, not soo much C
    How about just a macro for debugging, totally avoiding the overhead of a function call. Besides if storage is allocated on the stack then you don't have to worry about keeping track of the memory and cleaning it up. Just my 2c though.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by tempster09 View Post
    I would like to avoid the overhead of a function which allocates memory for a string, performs the concatenation, than making me keep track of the memory and cleaning it up. Im used to working with scripting languages, not soo much C
    This is not a scripting language. This is C.
    C is not like scripting languages. C is made to be a blazing fast, low-level (well, sort of) language. You cannot just do things in C as you can in scripting languages.
    Deal with it, or don't use it. The end.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  5. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM

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