LinuxQuestions.org
Review your favorite Linux distribution.
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 02-05-2004, 06:04 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
simple c++ question (pointers)


im trying to take each word untill end of file and put it in 1 element of a vector...

i know that char *words[1000] are pointers, so, when i want to read in the words, i need to somehow create a new space for each new word, right now it is just overriding the previous word and so the words vector points to the same word... here is a simple code example...

Code:
int wordSze =0;
char *words[1000];

int main(){
	char input[101];
	cin >> setw(100) >> input;
	while(!cin.eof()){
		words[wordSize]=text;
		wordSize++;
		cin >> setw(100)>>input;
	}
	
	for(int i=0; i<wordSize; i++)
		cout <<words[i]<<endl;
	return 0;
}


so, this is what the output would look like

>countwords
hello, how are you (ctrl-d)
you
you
you
you
>
 
Old 02-05-2004, 06:34 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,336

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
"i need to somehow create a new space for each new word,"

You pick up the new memory with the malloc function. You store the address of the memory provided by malloc into the array element.

___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html

Steve Stites
 
  


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
question about pointers vijeesh_ep Programming 1 09-13-2004 03:13 PM
Another C pointers question from a newbie chris.hicks Programming 6 11-11-2003 05:14 AM
question on pointers h/w Programming 1 10-26-2003 02:54 PM
simple problem with pointers Jo_Nak Programming 2 06-17-2003 04:46 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM

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

All times are GMT -5. The time now is 10:01 PM.

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