Хорошо, поэтому я написал небольшую функцию для преобразования любых заглавных букв в строке в нижний регистр просто для упражнения из книги, по которой я изучаю C.
Все работает нормально, за исключением присвоения значения символу с помощью указателя.
Вот код, и все компилируется правильно, но я получаю эту ошибку времени выполнения "Неизвестная версия протокола псевдоперемещения %d". вот почему я пытаюсь напечатать символ, значение которого изменилось с помощью указателя.
#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;
}
Если бы вы могли мне помочь, я был бы вам очень признателен. Я действительно смущен и немного раздражен тем, почему это не сработает. если вам нужно, чтобы я объяснил это лучше, просто попросите об этом. Надеюсь, я уже сделал достаточно.
Спасибо, Джейк.
////////////////////////////////////////////////// ////редактировать///////////////////////////////////////////// /////////////
Хорошо, я воспользовался всеми вашими предложениями, спасибо :D
и теперь это работает, за исключением странной ошибки, которую я не знаю, как исправить. .
все, кроме первого символа, преобразуется в символ нижнего регистра.
что теперь происходит -> "+преобразовать заглавные буквы в нижний регистр" Я не знаю, почему первый символ делает это? Есть предположения?
Вот новый код.
#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;
}
char text2Copy[length];
. Измените это наchar text2Copy[20];
просто для тестирования и посмотрите, что вы получите? - person Hammad Khan   schedule 22.06.2013char text[] = "'CONVERT capitals TO lower CASE'";
состоит в том, чтобы сделать его массивом, чтобы вы могли изменять содержимое. - person Daniel Fischer   schedule 22.06.2013