Help with Cast

This is a discussion on Help with Cast within the Linux Programming forums, part of the Platform Specific Boards category; Hi everyone, I'm getting a warning when I try to do the (char *) cast on &buf, What is the ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Hi everyone,

    I'm getting a warning when I try to do the (char *) cast on &buf, What is the correct cast for this?
    The warning I get is : use of old-style cast

    Thanks,
    Ed.

    Code:
            char buf[3][20];
    	inline void readInRow() {
    		currentData.clear();
    		mIn.read((char *) &buf, sizeof(buf));
    		for (int i=0; i < 3; ++i)
    			currentData.push_back(string(buf[i]));
    		printout(currentData);
    		cout << endl;
    	}

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Why can't you just pass it buf? I also dont think you can cast an array to a pointer...but if you could i believe the syntax would be (char **) for 2-d arrays.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think istream::read() expects a char *, but you probably don't want to use &buf, but rather buf. And since this is C++, you should use
    Code:
    mIn.read(reinterpret_cast<char *>(buf), sizeof(buf));
    --
    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.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not just pass buf[0] instead of forcing a 2D array to look like a 1D array?
    Then you shouldn't need any casts.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    Thanks matsp, that worked beautifully.

    Why not just pass buf[0] instead of forcing a 2D array to look like a 1D array?
    Because with that matrix I want to represent 3 words of 20 characters max. I don't know how I would do that with a single array. I'm newbie sorry

    Ed.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You got it right here:
    Code:
    currentData.push_back(string(buf[i]));
    It's the same idea.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by afflictedd2 View Post
    Thanks matsp, that worked beautifully.



    Because with that matrix I want to represent 3 words of 20 characters max. I don't know how I would do that with a single array. I'm newbie sorry

    Ed.
    If you are reading strings from a text-file, then istream::read will not do the right thing for you - you'd have to loop and use getline or >> to read the individual words.

    --
    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.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by afflictedd2 View Post
    Thanks matsp, that worked beautifully.



    Because with that matrix I want to represent 3 words of 20 characters max. I don't know how I would do that with a single array. I'm newbie sorry

    Ed.
    But then wouldn't this work?
    Code:
    for ( int x = 0; x < 3; ++x )
        mIn.read( buf[x], sizeof(buf[x]) );

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    52

    Thumbs up

    Quote Originally Posted by matsp View Post
    If you are reading strings from a text-file, then istream::read will not do the right thing for you - you'd have to loop and use getline or >> to read the individual words.

    --
    Mats
    It works. It's from a binary file not a text file.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by afflictedd2 View Post
    It works. It's from a binary file not a text file.
    That's fine then - it's just that I've seen quite a few posts where someone is trying to read a text-file using read (or write it using write) and then complaining that the code doesn't work right - usually pointing at some completely different portion of code [such as the output or calculations]. I was just making sure that you are doing things the way they are meant to be done.

    --
    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. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 03:46 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM

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