【问题标题】:C, issue with asigning a value through a pointer in a functionC,通过函数中的指针赋值的问题
【发布时间】:2013-06-19 14:16:28
【问题描述】:

好的,所以我编写了一个小函数来将字符串中的任何大写字母转换为小写字母,只是为了在我正在学习 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;
}

【问题讨论】:

  • 您正在尝试修改字符串文字,这是未定义的行为,并且通常会导致段错误,因为字符串文字(通常)存储在只读内存中。是重复的,让我搜索一下。
  • 啊,好的,谢谢:D 有没有办法可以将它放入读/写内存中?
  • 我认为你可以在运行时创建动态字符串char text2Copy[length];。将此更改为 char text2Copy[20]; 只是为了测试,看看你得到了什么?
  • 而修复是char text[] = "'CONVERT capitals TO lower CASE'";使其成为一个数组,这样你就可以修改内容了。

标签: c pointers letter


【解决方案1】:

你有几个问题。首先是当你将错误的类型传递给你的函数时,你的程序甚至不应该编译。第二个是您尝试修改文字(因此是常量)字符串。

对于第二部分,您可以很容易地解决它,而是使用数组来代替:

char text[] = "CONVERT capitals TO lower CASE";

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

对于实际的转换函数,可以比你的尝试简单得多

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

【讨论】:

  • 转换部分应该重写'tolower()',这就是我没有使用它的原因。但也可以,我纠正了你的建议的问题,但字符串仍然没有改变。
  • @user2509366:你把char* text改成char text[]了吗?
  • '您还尝试“返回”指向局部变量的指针,这将导致未定义的行为,因为局部变量是本地变量。一旦一个函数返回它们占用的内存,其他函数将重用它们。但是我已经在 main() 中声明了 char 数组,因此在程序结束之前不会在内存中重用 char 数组,对吗?这就是为什么我将“文本”的地址传递给函数。
  • @user2509366 如果您使用ASCII alphabet(现在几乎所有计算机都使用它),您可能会注意到大小写字母之间存在一些模式。从大写字母到小写字母的转换是一个简单的加法。您可以使用简单的if 语句来检查字符是否为大写字母,并且仅在为大写字母时进行加法。
  • @user2509366 但是变量text2Copy 转换函数中的局部变量。你想“返回”一个指向它的指针。
猜你喜欢
  • 2013-01-23
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 2016-05-31
相关资源
最近更新 更多