I cannot GET THIS TO WORK.. (Saving a STRING into an array...)

All I want to do is ask the user to enter words, then I bubble sort them alphabetically, then print them back out. THE PROBLEM IS.. When I try to store "str" into the words array, idk what's happening... When I print them out, it's only saving the LAST word I typed into every array location..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


#include "stdafx.h"
#include <stdio.h>
#include <string.h>

#define MAXLENGTH 15
#define MAXWORDS 3

int _tmain(int argc, _TCHAR* argv[])
{

char str[80];
char *words[25];
char *hold;
int j;
int i;
int wordcount=0;

while (wordcount<MAXWORDS){

	  printf("Enter a word: ");
		 fgets(str, MAXLENGTH, stdin);

  /* remove newline, if present */
  i = strlen(str)-1;
  if( str[i] == '\n') 
      str[i] = '\0';

  printf("%s\n",str);

  words[wordcount]=str;

	wordcount++;

}

printf("%d words.\n",wordcount);

for(i=1;i<wordcount;++i)
	for(j=wordcount-1;j>=i;--j)
		if (strcmp(words[j-1],words[j])>0){
		hold=words[j-1];
		words[j-1]=words[j];
		words[j]=hold;
		}


for(i=0;i<wordcount;i++)
	printf("%s \n",words[i]);

	return 0;
}
char*s are not strings. They are pointers.

A pointer does not contain string data. It points to the start of existing string data.

In this case, you are assigning your words[] array so that all entries point to the same string data: 'str'.

If you want to make a copy of the string data you need to do 2 things:

1) you need to make your words[] array actually be able to hold string data (not just a pointer to existing data)

2) you need to strcpy() the string data, not just assign a pointer.

1
2
3
4
5
6
char str[80];
char words[25][80];  // <-

//...

strcpy( words[wordcount], str );
If I do that, then none of my other stuff works... I don't understand why it's so hard to save a string into an array.. C is very difficult compared to other languages...

All I want to do is ask the user to enter strings 1 line at a time, and have them saved in an array.

Then sort the strings alphabetically in the array.

Then print it.

That's all!!.. I have the sorting working, I just don't have the storing the strings from the user into the array!
You have the same problem with 'hold'. Once you fix that problem with 'words' and 'hold', your program works just fine for me.

I don't understand why it's so hard to save a string into an array


If you were using strings, you wouldn't be having the problem. But you're not using strings, you're using pointers. Remember that pointers are not strings. A char* is not a string, it's a pointer.

The confusion here seems to be coming from a misunderstanding of how pointers work.

C is very difficult compared to other languages


I don't disagree.

I have the sorting working


You actually don't, because your sorting is incorrectly using pointers instead of strings.
char is not string, it is c_string. If you want to use real string you use
string words[25];
in this case you can store to 25 words
Last edited on
Well it looks like he's using C, not C++. So std::string is not an option.

But yes that would be easier.
Topic archived. No new replies allowed.