Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i don't really understand why this isn't working:

Code:
int randomNumber = (random() % 25000);
NSString *pathName = (@"/tmp/picture%i.tiff", randomNumber);

Warning: Initialization Makes Pointer From Integer Without Cast

any ideas?
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
fixed

asked too soon... as usual...

Code:
int randomNumber = (random() % 25000);
NSString *pathName = [NSString stringWithFormat:@"/tmp/LightSwatch%i.tiff", randomNumber];

although i'm still a touch hazy on the whole casting issue... i mean, i'm sorta/kinda know what it means, but it's not clear. i *think* i was receiving the first error because the program was trying to produce an NSString using an Int, but since they are different data types, the compiler issued the warning (and of course it didn't work)... that's what i think that warning meant anyway. and if i'm right, then i still don't understand why stringWithFormat worked... unless that's just the way to do it.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Another thing you can do if you need a temp file path is to use the function tempnam() to generate the C-string of the path.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
i don't really understand why this isn't working:

Code:
int randomNumber = (random() % 25000);
NSString *pathName = (@"/tmp/picture%i.tiff", randomNumber);

Warning: Initialization Makes Pointer From Integer Without Cast

Look at what you assign to pathName: You have two expressions separated by comma in parentheses; the first one is @"/tmp/picture%i/tiff", which is Objective-C for a constant NSString*, and the second is randomNumber, which is an int. Two expressions separated by comma are a "comma-expression": The compiler evaluates the first expression, then the second expression, and the result is the second expression. So what you did is the same as assigning NSString* pathName = randomNumber.

Now I personally don't like the warning message; I would have written "attempt to assign an integer to a pointer", and it should most definitely be an error, not just a warning.

The "without cast" is a red herring: Casting that expression to NSString* will get rid of the warning, but it won't fix the problem. It will take randomNumber, which is lets say 9999, cast it to an NSString* which means you will get a rubbish pointer, and when you try to use pathName you will most likely get a crash. So in this case adding a cast is the worst thing that you can do.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
The "without cast" is a red herring: Casting that expression to NSString* will get rid of the warning, but it won't fix the problem. It will take randomNumber, which is lets say 9999, cast it to an NSString* which means you will get a rubbish pointer, and when you try to use pathName you will most likely get a crash. So in this case adding a cast is the worst thing that you can do.

so is my fix not ok?

Code:
int randomNumber = (random() % 25000);
NSString *pathName = [NSString stringWithFormat:@"/tmp/picture%i.tiff", randomNumber];

it writes the files fine and seems to work... is there a better, less problematic way of producing this result?
 

ChrisA

macrumors G5
Jan 5, 2006
12,919
2,173
Redondo Beach, California
Another thing you can do if you need a temp file path is to use the function tempnam() to generate the C-string of the path.

I have to second the above idea. Random filenames are a very bad idea. The above function will always give you a "good" filename that is not already in use. It will follow the file name rules of whatever system it is on.

You ask "what are the chances of a random dom function giving two identical filenames?" It WILL happen if you program is used by enough people enough times
AND, think hard about how to set the deed for the random functin or yu will get the same filenames every time you run the program.
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
When doing this, I always use the date & time (accurate to the millisecond), plus a sequence of unlikely characters. The date & time ensures that part of the name will be unique (at least until the clock overflows).

And the sequence of chars is just in case someone else has the same bright idea and is putting files-named-using-date&time in the same folder. So, unless someone creates a file with the same unlikely sequence of characters in the same millisecond...

Even then, I check if the file already exists and if it does, go again.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
so is my fix not ok?

Code:
int randomNumber = (random() % 25000);
NSString *pathName = [NSString stringWithFormat:@"/tmp/picture%i.tiff", randomNumber];

it writes the files fine and seems to work... is there a better, less problematic way of producing this result?

That's a fine fix, which contains no casts.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.