Results 1 to 7 of 7

Thread: Can anybody explain me this 'kind of casting'¿?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Can anybody explain me this 'kind of casting'¿?

    I have found this code to write data:
    inv is an 'inventory' array.
    I dont undertad he line that writes data, it makes some casting but ....
    Can anybody help me ? ( I dont understand (const char*) neither & for inv.
    thanks

    Qt Code:
    1. struct inventory {
    2. char item[20];
    3. int quantity;
    4. double cost;
    5. };
    6.  
    7.  
    8. for(int i=0; i<3; i++)
    9. fout.write((const char *) &inv[i], sizeof(inventory));
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can anybody explain me this 'kind of casting'¿?

    It all about basic C++. Read about explicit casting and pointers/references.

  3. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can anybody explain me this 'kind of casting'¿?

    and arrays too.

  4. #4
    Join Date
    Oct 2010
    Posts
    37
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can anybody explain me this 'kind of casting'¿?

    Isn't that pretty bad code since struct padding might change the binary file between systems? I think you should not use that code.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can anybody explain me this 'kind of casting'¿?

    Its true that struct padding might change the structure size, but if your only interested in one system (or the file is only written/read on one system, eg. an internal data file) then its fine. It just means more work later on if you want the make the files portable between different architectures/systems.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can anybody explain me this 'kind of casting'¿?

    And possibly some garbage (possibly containing some sensitive data) written to the file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can anybody explain me this 'kind of casting'¿?

    Always pack your structs if they are going to leave the program...
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. please explain QUrl::isValid() to me
    By momesana in forum Qt Programming
    Replies: 5
    Last Post: 19th May 2010, 01:19
  2. Can someone explain implict sharing for me?
    By MorrisLiang in forum Qt Programming
    Replies: 7
    Last Post: 2nd May 2010, 13:25
  3. Can't explain these errors
    By Petr_Kropotkin in forum Newbie
    Replies: 4
    Last Post: 23rd January 2010, 19:06
  4. Explain Qt library sizes, please
    By bnilsson in forum Installation and Deployment
    Replies: 15
    Last Post: 25th July 2009, 10:01
  5. could somebody please explain const to me?
    By mikro in forum General Programming
    Replies: 4
    Last Post: 29th September 2006, 14:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.