Ok so I have written a little function to convert any capitals in a string into the lower case just for an exercise out of a book that I am learning C from.
Everything works fine other than assigning the value into a 'char' through a pointer.
Here is the code and everything compiles correctly but I get this runtime error "Unkown pseudo relocation protocol version %d." this is why I try and print the char that had the value changed through the pointer.
#include <stdlib.h>
#include <stdio.h>
/*
----------------------------------------------
CONVERTS UPPERCASE CHARACTERS TO LOWERCASE
----------------------------------------------
*/
void lowercase(char * address, char text2){
// used in the for loop
int inc;
// used as an index for text2Copy
int inctwo = 0;
// used in the for loop
int length = strlen(text2);
//used to copy the active character in text2
char text2Copy[length];
for(inc = 0; inc <= length; inc++){
//basicaly if character is a capital leter
if(text2[inc] >= 'A' && text2[inc] <= 'Z'){
//I plus 32 because each letter is 32 numbers away in 'ASCII'
//therefore converting capital to lowercase
text2Copy[inctwo] = text2[inc] + 32;
//add one to help with indexing
inctwo++;
}
//if the character is not a capital leter
else{
text2Copy[inctwo] = text2[inc];
inctwo++;
}
}
//*address = "sdafsdf"; //<-- THIS WORKS!!!
*address = text2Copy;//WHY DOESN"T THIS WORK?
}
int main(){
//just the string I will be using.
char * text = "'CONVERT capitals TO lower CASE'";
//print the string to show the original
printf("%s\n",text);
lowercase(&text,text);
//This is where I want the value from the function to print out
printf("%s\n",text);
return 0;
}
If you could help me I would greatly appreciate it I am really confused and a bit annoyed at why this won't work. if you need me to explain it better just request it I hope I did enough already tho.
Thanks, Jake.
//////////////////////////////////////////////////////edit//////////////////////////////////////////////////////////
Okay I have used all of your suggestions thank you :D
and now it currently works other than a weird bug that I don't know how to fix.
everything other than the first char gets turned into a lower case character.
what happens now -> "+onvert capitals to lower case" I don't know why the first character is doing that? anythoughts?
Here is the new code.
#include <stdlib.h>
#include <stdio.h>
/*
----------------------------------------------
CONVERTS UPPERCASE CHARACTERS TO LOWERCASE
----------------------------------------------
*/
void lowercase(char * address, char text2[]){
// used in the for loop
int inc;
// used in the for loop
int length = strlen(text2);
for(inc = 0; inc <= length; inc++){
//basicaly if character is a capital leter
if(text2[inc] >= 'A' && text2[inc] <= 'Z'){
//I plus 32 because each letter is 32 numbers away in 'ASCII'
//therefore converting capital to lowercase
text2[inc] += 32;
//add one to help with indexing
inctwo++;
}
//if the character is not a capital leter
else{
inctwo++;
}
}
*address = text2;
}
int main(){
//just the string I will be using.
char text[] = "cONVERT capitals TO lower CASE";
//print the string to show the original
printf("%s\n",text);
lowercase(&text,text);
//This is where I want the value from the function to print out
printf("%s\n",text);
return 0;
}
You have several problems. The first is that that your program should not even compile as you pass wrong types to your function. The second is that you try to modify a literal (and therefore constant) string.
For the second part, you could solve it very easily, by using an array instead:
char text[] = "CONVERT capitals TO lower CASE";
You also attempt to "return" a pointer to a local variable, and that will lead to undefined behavior as local variables are, well, local. Once a function returns the memory they occupied will be reused by other functions.
For the actual conversion function, it can be made much more simple than your attempt:
void lowercase(char *text)
{
while (*text != '\0')
*text = tolower(*text);
}