简体   繁体   English

大写字符串并返回局部变量

[英]Capitalize string and return local variable

I'm trying to make a procedure that will capitalize a string, but I get junk values and a warning from gcc that I'm returning the address of a local variable. 我正在尝试制作一个将字符串大写的过程,但是我收到了垃圾值和来自gcc的警告,我正在返回局部变量的地址。 Coming from Python, I'm confused by this issue. 来自Python,对此问题感到困惑。 (Don't want to use stuff in <string.h> ) (不想使用<string.h>

 2
 3
 4char* strcaps(const char *strlower)
 5{
 6  int len = 0;
 7  while (*strlower != '\0'){
 8    len++;
 9    strlower++;
10  }
11  char newStr[len + 1];
12  int i;
13  for (i = 0; i < len; i++)
14    newStr[i] = strlower[i] - 32;
    newStr[len + 1] = '\0';    //EDIT, just put this in.
15  return newStr;
16}
17
18int main(void)
19{
20  
21  
22  
23  printf("I expect capitalized version of edgar, I got %s\n", strcaps("edgar"));
24
25
26  return 0;
27}

I'm guessing I can make a global string and then mutable that, but I'd rather keep everything inside the function strcaps...is this a case when I should use malloc? 我猜想我可以创建一个全局字符串然后将其可变,但是我宁愿将所有内容保留在strcaps函数中...在我应该使用malloc的情况下是这种情况吗?

Thanks 谢谢

EDIT: Just realized that I never put a '\\0 ' at the end of the new string, although I'm not sure if that is related. 编辑:只是意识到我从来没有在新字符串的末尾放置'\\0 ',尽管我不确定这是否相关。

You have two options in C. 您在C中有两个选择。

1) you can pass your function an pointer to the string and function will modify it and return void 1)您可以向函数传递指向字符串的指针,函数将对其进行修改并返回void

In this case you can just use the original string 在这种情况下,您可以只使用原始字符串

2) you can pass your function const char* and return char * , but then you need to malloc inside the function 2)您可以传递函数const char*并返回char * ,但是随后您需要在函数内部进行malloc

If you do a malloc inside function, you can then use the string (print it or do whatever you want), at the and of the program you call free 如果在函数内部执行malloc,则可以在调用free的和处使用字符串(将其打印或执行所需的任何操作)

is this a case when I should use malloc? 这是我应该使用malloc的情况吗?

Yes, exactly. 对,就是这样。

In your case, you return pointer to local variable, which (the variable) will be destroyed after the execution of the function. 在您的情况下,您将返回指向局部变量的指针,该局部变量在执行函数后将被销毁。 This makes the pointer dangling. 这使指针悬空。

Using malloc will create the string in the heap, not stack, and it will remain valid after after the return of the function. 使用malloc将在堆而不是堆栈中创建字符串,并且在函数返回后仍将保持有效。

First of all, you should always put a null character at the end of a string, because the standard library uses this to denote the actual end of a string, to prevent from walking over memory. 首先,您应该始终在字符串的末尾放置一个空字符,因为标准库使用它来表示字符串的实际末尾,以防止在内存中游走。

This is an implementation. 这是一个实现。

/* Makes a copy of the string as to not modify the original buffer */
char* makeCopy(char* s){

    char* buff = (char*) malloc(len(s) + 1);
    strcpy(buff, s);
    return buff;
}

/* Converts the string to uppercase */
char* toUpperCase(char* s){

        char* q = makeCopy(s);
        int i;
        for(i = 0; i < len(q); i++){
                *(q + i) = toupper(*(q + i));
        }

        return q;

}

Now you can do: 现在您可以执行以下操作:

printf("I expect capitalized version of edgar, I got %s\n", toUpperCase("edgar"));

Now I understand that you don't want to touch any of the stuff in string.h , but it is useful, for without the functions there you may need to implement much of the stuff on your own. 现在,我了解到您不想触摸string.h任何内容,但这很有用,因为如果没有这些功能,您可能需要自己实现很多内容。

For example, the function toupper() comes from string.h . 例如,函数toupper()来自string.h This converts a character to its uppercase equivalent. 这会将字符转换为大写字母。 If you insist on reinventing the wheel you can hack a tiny function that handles it. 如果您坚持要重新发明轮子,则可以破解一个处理它的小函数。

char getUpperCaseChar(char lower){
    if(lower < 91)
        return lower;
    if(lower > 96)
        return (lower - 32);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM