Thread: Arrays and Functions

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    Arrays and Functions

    I know that you can't directly return an array from a function, but I'm a little shaky on how to use pointers with arrays.

    I've tried using a function like this to read data from a file and assign it to an array via a function:
    Code:
    ifstream fin;
    char array[4];
    void readText(char *array)
    {  
         for(int i=0;i==sizeof array;i++)
                 *(array+i)=fin.get();
    }
    The .dat file is simply
    Code:
    TEST
    When I use cout to display the contents of the array that should have received the data from the file, I only receive 4 blank spaces. Apparently nothing is being written to the array, but I don't really understand what exactly is going on.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your problem is less about pointers and more about for loops, in that sizeof array is not zero and therefore the for loop never happens.

    However: the dimensions of an array do not get passed in to a function -- hence sizeof array is the sizeof any pointer on your system, usually four. You will need to pass in the dimensions separately using another parameter to your function.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from the issue of using "==" in the loop condition .....

    This is a case where the rule of thumb "a pointer is an array" fails .... a pointer is different from an array in this context.


    sizeof array in the function gives the size of a pointer (usually 4 on most 32 bit systems), not the number of elements available in the array.

    The size of the array needs to be passed separately to the function (eg a second argument).

    The approach "number = sizeof(array)/sizeof(array[0])" only works for arrays. It does not work for pointers. When arrays are passed to functions as an argument, the function only receives a pointer and does not know it is an array.
    Last edited by grumpy; 03-08-2008 at 07:49 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Alternatively, you could use a std::vector<char> to save the data into instead of an array.

    Code:
    #include <vector>
    #include <iostream>
    #include <fstream>
    #include <iterator>
    
    int main(void)
    {
        std::ifstream input("testfile.dat");
    
        std::vector<char> v((std::istream_iterator<char>(input)),
                std::istream_iterator<char>());
    
        copy(v.begin(), v.end(), std::ostream_iterator<char>(std::cout));
        std::cout << std::endl;
    }

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Thanks for the help. I managed to get everything sorted out.

    One last question:

    Is there a specific function that can return the size, in bytes, of a data file? I've searched around a bit, but I haven't been able to find anything.

    Thanks

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The only cross platform way is to open the file and read all the data to get the total size in bytes.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <fstream>
    #include <iomanip>
    #include <iostream>
    
    int main()
    {
    	std::ifstream f("c:\\1.bmp",std::ios::binary);
    	f.seekg(0,std::ios_base::end);
    	std::streampos y = f.tellg();
    	std::cout << y <<std::endl;
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The only cross platform way is to open the file and read all the data to get the total size in bytes.

    That might be the only completely portable way, but I believe cross-platform libraries have this ability (such as boost's filesystem).

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm sure vart's post will work on 99.99% of all systems that support the fstream class (to some reasonable completeness).

    There may be certain devices that don't support seek (such as tape-devices, consoles and serial ports), but for real files on a disk-like device, it should not be a problem to seek the end of the file to find out the size of the file. Just bear in mind that seek operates on the binary form of the file, so it's the size of the file on disk, not the size you'd get if you read the file using characters in text mode [on some machines that would indeed be the same, but on a Windows system, every newline consists of a CR and an LF, which when you read the file gets translated to newline (LF), so for every line in the file, the size is one byte bigger on disk than it is when you read it via the text-mode file-reading method].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM