Passing character array into function

This is a discussion on Passing character array into function within the C++ Programming forums, part of the General Programming Boards category; Hi I am new. I hope this is not asked recently. Is it possible to pass the character array by ...

  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
    22,114
    Add another parameter to the function so as to pass the array size. Alternatively, use a std::tr1::array<char, 128>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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 03:41 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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
    22,114
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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,308
    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, 09: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, 09: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, 10:57 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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