Search code examples
iphonecxcode4srand

srandom(time(NULL)) giving warning - pointer to integer without a cast


In iPhone (Xcode 4), using the function,

srandom(time(NULL));

both srand and srandom is giving this warning. But when running its working fine. Why I am getting the warning in one of my class file? I have used that in other files, but no warning.

Warning: passing argument 1 of 'srand' makes integer from pointer 
without a cast

However, using arc4random() can solve this problem. But in most example srand() is used in this way and nobody complains. Thats why I am confused.


Solution

  • Because srand is expecting an integer and time() is returning a pointer (from the looks of your particular error). Casting explicitly to an int will make it go away. Or perhaps reading the pointer to get the actual time value might be what you are looking for instead. Not 100% certain of time's return value here, but I'd double check to make sure it is indeed returning a tics value instead of a pointer to a time_t object that will remain mostly the same over time.

    According to what I just read, it's supposed to return a time_t value, which when cast as an int, is the number of seconds elapsed since 1972ish. So not a pointer usually, but in your case it may be. Either way, add either a dereference and a cast, or just a cast if you can get it to return the time_t directly.