Problems with strings in C

TheCaptain22's Avatar, Join Date: May 2007
Newbie Member
Here is some code I have been working on but I can get the results im looking for. I cant manipulate the strings right. I have put comments as best I can so you can follow what im trying to do. Please help me fix this.
Code:
void Compress(char *str, Header *dict, int numb) {
   int j;
   char buffer[strlen(str)];  //I made a buffer to set the length
   char* w = buffer;  //Initialized W
   char k; //Made a character k
   char tmpw[strlen(str)]; //Made a temporary W
   strcpy(w, ""); //Here I want to set W to blank, im not sure if this is the right way so it might need fixed
   int i = 0; //Set i to 0
   while (i < strlen(str)) //Continue while i < the length of the incoming string
   {
     k = str[i]; // Here I want k to equal the ith character in the incoming string, i think this will work
     for(j = 0; j < strlen(w); j++)  //This loop is here to copy the contents of W to tmpw
     {                                          //There has to be a better way but everytime I tried  
       tmpw[j] = w[j];                  //somthing else when I changed W later it also 
     }                                          //changed tempw. I didnt know how to fix this
     strcat(w, &k); //Here I was to put the character k onto the end of the w string. I dont think this works but I dont know how to do it. This line needs fixed
     if(dict[hash(w)].next != 0) {
       //If WK is in the dictionary W = WK
       //I didnt do anything in here because I already put W and K together into W
     } else {
       //Otherwise it adds WK to the dictionary and outputs the code to W
       HashVal* newNode;
       newNode = (HashVal*) malloc (sizeof(HashVal));
       newNode->val = w;
       newNode->num = numb;
       newNode->next = NULL;
       numb++;
       dict[hash(w)].next = newNode;
       newNode = dict[hash(tmpw)].next; //Here is where I use temp W which should be the value of W
       strcpy(w, &k); //This also needs to set W to equal K. I dont think this is the right way but I dont know how to do it.  This needs fixed
     }
     i++;
   }
}
0
aVague's Avatar, Join Date: May 2007
Go4Expert Member
brrrrrr, thats not the best way for explaining , what do you want
tell then, which results you looking for,what do you need:
to know lenght of string, to do the massive of strings, or what?

to work on strings is enought to see theory about them :
1) string is a massive(note that!) of symbols(char) , with last element '\0'
2) there are some functions allows you to copy , unite , compare strings(leght and identity)...
0
TheCaptain22's Avatar, Join Date: May 2007
Newbie Member
I will go through a few problems in specific.
I guess first do the declarations look right? Should I change something?

On this line im trying to set w to a blank string. Will this work?
strcpy(w, "");

Here I want to set the character k to the ith value of a string. The string is passed in and declared like this "char *str"
k = str[i];

In this little bit of code im just trying to store the value of w into a temporarry value. The problem i was having is all the other ways i tried if i changes w later in the code it would also change the temporary variable. I know there has to be a better way to do this
for(j = 0; j < strlen(w); j++)
{
tmpw[j] = w[j];
}

Here I want to put the character k onto the end of the string w. I dont think this works the way I did it but I dont know how to do it.
strcat(w, &k);

And finally here I want to set the value of the string w to the character k. I also dont think this is the right way to do this but i not sure how to.
strcpy(w, &k);
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
A C string is merely an array of char terminated with the nul character, '\0'. strcpy (w, "") will work, but you may as well just write w [0] = '\0';

char *str; does not define a string. It defines a pointer to a string. The pointer is useless, indeed dangerous, if you don't assign it a value that points to an existing string. k=str [i] is only effective if str is an initialized pointer, or the name of an actual char array.

For your next example, if changing w later is changing the temp value, then your snippet is not complete. The implication is that the two are referring to the same array. Rather than write your own loop, why not just use strcpy?

For your next example, if you want to put the 'k' on the end of a string, just use strcat (w, "k");. W must, of course, be long enough to hold the extra character.

Similarly, for your last example, use strcpy (w, "k");

&k is not the character OR the string, "k", it is the address of a variable named k.
0
TheCaptain22's Avatar, Join Date: May 2007
Newbie Member
Well thats the thing I have a variable k that I want to put on the end. But its declared as a char. The problem im having with strcat is that is takes a const char* and I have just a char. Same for strcpy, I want to set w to the char k and once again it takes a const char*.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
A char is not a string. Consequently, you cannot manipulate it with functions designed to take strings. Furthermore, you can't just tack it on with myString [strlen (myString)] = myChar, because you have to follow it with the '\0' char. Either take both those steps, or make your variable a string instead of a char.

The thing is, you have to deal with things as they are designed to work, not as you WISH they worked. That means reading the function docs carefully as well as learning the differences between chars and strings and pointers and arrays.