[RESOLVED] Pointer Increment
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Pointer Increment

  1. #1
    Join Date
    Jan 2011
    Posts
    3

    [RESOLVED] Pointer Increment

    To whom it may concern,
    I just noticed something odd while looking at a piece of example code.

    I believe I am misunderstanding an aspect of how C++ handles type casting and the incrementing of pointers.

    Here's an excerpt from main():
    Code:
    int n[5] = {1, 2, 3, 4, 5};
    
    ofstream out("test", ios::out | ios::binary);
    
    out.write((char *) &n, sizeof n);
    What I do not understand is how the write() function can output a four byte integer value ('n') after casting it to a one byte char pointer.

    The declaration for the write() function is as follows:
    ostream& write (const char* s , streamsize len);

    To access the next value, presumably 's' would be incremented by one byte as any other char pointer. However, since each value of the 'n' array is four bytes long it would seem that 's' would now point to the second byte of the first item in the 'n' array. Yet, this program appears to function as if, after being incremented, 's' points to the second value in the 'n' array.

    What am I missing?
    Thanks for any clarification,
    Ben

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,893

    Re: Pointer Increment

    There is no "incrementing"-----the write() function does not care that it is being handed an array. It is given a pointer and told to write out the next 20 bytes (because sizeof(n) is sizeof(int)*5, you can check this).

  3. #3
    Join Date
    Jan 2011
    Posts
    3

    Re: Pointer Increment

    Aha! I was misinterpreting the sizeof command. It all fits together now.
    Thank you very much!

  4. #4
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Pointer Increment

    you can also use fstream to write to files , both text and binary , reading and writing happens the same way.
    Code:
    fstream mystream("myfile" ,std::ios::binary|std:ios:in); // as in reading a files
    mystream.read((char *) myarray ,sizeof(myarray));
    
    // if you are reading  header of any file then you need to know the  structure used to write //that header
    // so instead of array  define the structure  and then read into that structure
    // same for the writing. std::ios::out)
    // don't forget to flush and close the steam when you are done with it.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: Pointer Increment

    Quote Originally Posted by fractalgeometric View Post
    Aha! I was misinterpreting the sizeof command. It all fits together now.
    Thank you very much!
    Be VERY VERY VERY (VERY) careful when calling sizeof on arrays. Arrays have a tendency to decay into simple pointers, at which point the sizeof will not be what you think it is...
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jan 2011
    Posts
    3

    Re: Pointer Increment

    Thanks for the suggestions and advisories; it's good to see other options and know what to watch out for.

    -Ben

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center