C,通过函数中的指针赋值的问题

标签 c pointers letter

好的,所以我写了一个小函数来将字符串中的任何大写字母转换为小写字母,只是为了练习我正在学习 C 的一本书。

除了通过指针将值赋给“char”之外,一切正常。
这是代码,所有内容都可以正确编译,但我收到此运行时错误“Unkown pseudo relocation protocol version %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;
}

如果您能帮助我,我将不胜感激 我真的很困惑,并且对为什么这行不通感到有些恼火。如果您需要我更好地解释它,请提出要求,我希望我已经做得够多了。

谢谢, jack 。

///////////////////////////////////////////////////编辑/////////////////////////////////////////////////////////
好的,我已经采纳了你所有的建议,谢谢 :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 text[] = "CONVERT capitals TO lower CASE";

您还试图“返回”一个指向局部变量的指针,这将导致未定义的行为,因为局部变量是局部变量。一旦一个函数返回它们占用的内存将被其他函数重用。

对于实际的转换功能,它可以比您的尝试简单:

void lowercase(char *text)
{
    while (*text != '\0')
        *text = tolower(*text);
}

关于C,通过函数中的指针赋值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252349/

相关文章:

c - 使用按值传递更新实际值

c - 将 linux.words (/usr/share/dict/words) 中的前 10 个单词写入数组

excel - 将数字转换为相应的 Excel 列

c - 如何使用 C 中的函数生成随机 float 数组

c - C 中函数指针声明中带括号的三元表达式返回函数名称的基本原理

比较输入中的字符串

JavaScript 递归计算重复字母

c - 计算机上字符表示的底层细节

c - 初始化嵌套结构

c++ - "typedef"类型名称可以像 "struct"定义那样声明指向结构的指针吗?