C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-04-2008, 08:13 AM   #1
Registered User
 
Join Date: Jul 2008
Posts: 44
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;
	}
afflictedd2 is offline   Reply With Quote
Old 09-04-2008, 08:19 AM   #2
Registered User
 
valaris's Avatar
 
Join Date: Jun 2008
Location: RING 0
Posts: 468
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.
valaris is offline   Reply With Quote
Old 09-04-2008, 08:31 AM   #3
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-04-2008, 08:43 AM   #4
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,282
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.
cpjust is offline   Reply With Quote
Old 09-04-2008, 09:27 AM   #5
Registered User
 
Join Date: Jul 2008
Posts: 44
Thanks matsp, that worked beautifully.

Quote:
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.
afflictedd2 is offline   Reply With Quote
Old 09-04-2008, 09:30 AM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,836
You got it right here:
Code:
currentData.push_back(string(buf[i]));
It's the same idea.
tabstop is online now   Reply With Quote
Old 09-04-2008, 09:39 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-04-2008, 09:50 AM   #8
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,282
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]) );
cpjust is offline   Reply With Quote
Old 09-07-2008, 02:31 PM   #9
Registered User
 
Join Date: Jul 2008
Posts: 44
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.
afflictedd2 is offline   Reply With Quote
Old 09-08-2008, 03:32 AM   #10
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Including The Right DLLs bumfluff Game Programming 8 12-28-2006 03:32 AM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
Converting Double to Float thetinman C++ Programming 7 06-17-2006 02:46 PM
errors in class(urgent ) ayesha C++ Programming 1 11-10-2001 10:14 PM
errors in class(urgent) ayesha C++ Programming 2 11-10-2001 06:51 PM


All times are GMT -6. The time now is 10:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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