You misunderstand something about pointers.
"temp" is an array of 80 characters. There is now space reserved in memory for 80 characters. The name "temp" by itself is a pointer to the first character in the array.
"array" is an array of 255 pointers to characters. There is now space reserved in memory for 255 pointers (not characters). The name "array" by itself is a pointer to the first pointer in the array. (BTW, don't name things "array".)
So, if you say strcpy( temp, "Hello" ); then the temp[] array contains the string "Hello".
If you then say strcpy( temp, "world" ); then the temp[] array contains the string "world". The string "Hello" is overwritten, and no longer exists.
In your loop, you assign array[ n ] (a pointer to a character) the address of the first char in the temp array. So,
strcpy( temp, "Hello" ); // temp[] contains "Hello"
array[ 0 ] = temp;
// array[ 0 ] points to "Hello" in temp[]
strcpy( temp, "world" ); // temp[] contains "world"
// array[ 0 ] still points to temp[], but temp[] now
// contains "world"
Your loop is failing because "i" starts at one when it should start at zero. In other words, when you increment "i" you are saying that there is one more item in the "array" than there really is.
Hope this helps.
[EDIT] You know, it really is difficult to help when you change things after posting.
I'll check again after you've made up your mind.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
char string[80]; only creates an array of 80 characters. The array is named "string".
There is a problem with the order in which you are doing things. You get another string from the user before you copy "bill" to the "string" array.
The weird character is because you did not initialize your "string" array properly. You are lucky the program didn't crash.
Somewhere at the top, say line 11 or so, say string[0] = '\0';
Hope this helps. :)
PS. Don't name arrays "string" either. Come up with a real name.
[EDIT] Oh yeah, strcat_s() is non-standard MS crap. If you are only going to use the MS compiler, then fine, else stick with strcat().
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
You mean another different question? ;) (Kind of like "Can I ask a question?" heh heh heh...)
I don't mind personally, but I'm not sure what the convention is here...
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
:)
What was that post? A bump? Never bump your messages. Someone will get to you when they log in.
string == strcat(string," ");
string == strcat(string,temp);
What do you think these lines are doing? I'll bet it's not what you expect....$ is a weird character that show up. i don't know what it is.
It's called a dollar sign. Hex value 24, decimal 36.
Be more careful with your formatting
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Use an istringstream to parse the line into words delimited by whitespace. I suppose you could use strok() or similar protocol as well if you wanted.
Here's one version using STL strings as opposed to C style strings.
WARNING: code snippets posted below not tested
string inputString = "This is a test";
vector<string> words;
istringstream iss(inputString);
string temp;
while(iss >> temp)
words.push_back(temp);
You can change the appropriate syntax to use C style strings while retaining the basic protocol. Using C Style strings it might look something like this:
char inputString[80];
char words[80][80]; //this is essentially the same of char *words[80]; and can be used whereever you wanted to use char *[];
int index = 0;
istringstream iss(inputString);
while (iss >> words[index])
++index;
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396