Thread: Passing character array into function

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Passing character array into function

    Hi I am new. I hope this is not asked recently. Is it possible to pass the character array by itself instead of as a pointer? I want to get the size of array in the function the array was passed in. Can you do that? The following code only returns the size of pointer, not the size of buffer. Please help.

    Code:
    void Test()
    {
       char testStr[128];
       int testSize = GetSize(testStr);
       std::cout << testSize << std::endl;
    }
    
    int GetSize(char testStr[])
    {
       return sizeof(testStr);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Add another parameter to the function so as to pass the array size. Alternatively, use a std::tr1::array<char, 128>.
    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

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    the answer is yes, but you must already know the size, so what you want to do with it is moot.

    Code:
    void func(char(&arr)[128])
    {
    //this passes a reference to a static array of char[128]
    }

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    I am trying to find a way for the function to figure out the size, so passing the size defeats the purpose. Is there any way to do this without passing the hardcoded size? Thanks.
    Last edited by cactuar; 10-22-2009 at 02:41 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suppose you could use a variant of what m37h0d suggested:
    Code:
    template<typename T, std::size_t Size>
    inline std::size_t GetSize(T(&arr)[Size])
    {
        return Size;
    }
    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

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    if it's a static array, you have to know the size at compile time.

    if it's a dynamic array, you have to know the size at allocation time.

    either way, you should never find yourself in the position of having an array and not knowing its length.

    sounds like you should use vector.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by m37h0d
    either way, you should never find yourself in the position of having an array and not knowing the size of it's contents.

    sounds like you should use vector.
    Yes, that is something to consider over a std::tr1::array since your array size can vary. Of course, we're assuming that this GetSize() function will no longer exist, since it presumably existed so that you can conveniently pass the size to other functions, but now other functions can get the size through the vector.

    Yet another option is to just pick a suitable container and then have your other function takes iterators instead as arguments. This way you are not so restricted to just a vector... but it may be overkill for what you are trying to do.
    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

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Okay, originally I was trying to check for buffer overrun. I get the character array from someone, and my function would make sure it has enough buffer size to copy the data or do whatever that I want to do with it. How do you do this normally? Do you always ask the size of array when passing a character array? I was hoping there is more elegant way.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cactuar View Post
    Okay, originally I was trying to check for buffer overrun. I get the character array from someone, and my function would make sure it has enough buffer size to copy the data or do whatever that I want to do with it. How do you do this normally? Do you always ask the size of array when passing a character array? I was hoping there is more elegant way.
    If you mean what do, say, library functions do, the answer is "assume the person passing the array in knows what they're doing". The standard functions like strcat, strcpy, etc will happily overwrite into invalid memory. The ever-popular "_s" functions from Microsoft (just as an example), or fgets, do require you to pass in the size.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cactuar View Post
    Okay, originally I was trying to check for buffer overrun. I get the character array from someone, and my function would make sure it has enough buffer size to copy the data or do whatever that I want to do with it. How do you do this normally? Do you always ask the size of array when passing a character array? I was hoping there is more elegant way.
    There's a reason so many Win32 API functions require the buffer length to be passed in. It's because it needs this information and that is the easiest way to get it. There is nothing more elegant.
    Take a look at this:
    GetShortPathName Function (Windows)
    The idea used here is that passing zero gives you back the size that you need to allocate.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM