LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2003, 10:55 AM   #1
dragoon linux
LQ Newbie
 
Registered: Nov 2003
Posts: 24

Rep: Reputation: 15
newb c++ code question


Yes..... in the book I'm using to learn c++ I have this code that shows about random numbers:

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
time_t seconds;

time(&seconds);
srand((unsigned int) seconds);
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
return 0;
}

Now I'm wondering, because it doesnt tell me, why the & has to be right before the seconds in : time(&seconds)...... like what is it there for and what does it do? thank you for any help...
 
Old 12-24-2003, 11:31 AM   #2
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
its taking the argument which is a pointer, hence you pass it the address.
 
Old 12-24-2003, 11:39 AM   #3
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
time() takes an argument of type time_t * (pointer to time_t). The &-operator is called the address-operator, it gives the address of (pointer to) a variable.

This code is equivalent to your code (but less elegant):
Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
  time_t *seconds = new time_t;

  time(seconds);
  srand((unsigned int) *seconds);
  cout << rand() << endl;
  cout << rand() << endl;
  cout << rand() << endl;

  delete seconds;

  return 0;
}
Regards
Martin
 
Old 12-24-2003, 12:05 PM   #4
vinay_s_s
Member
 
Registered: Jul 2003
Posts: 659

Rep: Reputation: 30
& is the address of operator. U better learn basics rather than going directly to tyme and other things. U seem to lack the basics.

Better learn C or buy a book which has basics of C/C++ and then read it!!!
 
Old 12-24-2003, 12:11 PM   #5
dragoon linux
LQ Newbie
 
Registered: Nov 2003
Posts: 24

Original Poster
Rep: Reputation: 15
well I'm using a book my friend took from the comp lab at school O.o guess it's better to save it for later, anyone know any online books or such that can help/are good?
 
Old 12-25-2003, 04:10 AM   #6
nibbler
Member
 
Registered: Oct 2003
Location: Croatia
Distribution: Fedora 4
Posts: 46

Rep: Reputation: 15
http://www.bxteam.org/knjige.php
Teach yourself C/C++ in 21 days and Thinking in C++
 
Old 12-26-2003, 09:23 AM   #7
FriedMalt
LQ Newbie
 
Registered: Dec 2003
Location: Stalker. Quit following me.
Distribution: Fedora Core 1
Posts: 9

Rep: Reputation: 0
Quote:
Originally posted by nibbler
http://www.bxteam.org/knjige.php
Teach yourself C/C++ in 21 days and Thinking in C++
...I could kiss you for that link even though I wasn't the one asking for it, but I'll hold myself back. Thanks.
 
Old 12-26-2003, 11:15 PM   #8
laceupboots
Member
 
Registered: Dec 2003
Location: Houston
Distribution: Knoppix,lenova yoga 3, Samsung s6 -android
Posts: 307

Rep: Reputation: 30
I liked this link too.
 
Old 12-29-2003, 02:27 AM   #9
arrruken
Member
 
Registered: Apr 2003
Distribution: gentoo
Posts: 238

Rep: Reputation: 32
Ive gotten alot of use out of www.cprogramming.com. I use a book too, but its nice to have explanations on a webpage i switch back and forth from when im programming.
 
Old 12-29-2003, 12:55 PM   #10
laceupboots
Member
 
Registered: Dec 2003
Location: Houston
Distribution: Knoppix,lenova yoga 3, Samsung s6 -android
Posts: 307

Rep: Reputation: 30
Thanks arrruken, thats a great site too. I found some good compilers at this site. http://gcc.gnu.org/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newb question sicness Slackware 4 11-09-2005 03:40 AM
Very newb question m2azer Linux - Newbie 3 09-19-2004 06:58 PM
Newb question tombomb300 Linux - Newbie 1 01-28-2004 12:20 PM
Newb question zekko Linux - General 4 08-22-2003 05:26 PM
NEWB - Learning to code under GNU/Linux bkeating Linux - Newbie 5 10-09-2002 07:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration