简体   繁体   English

更改传递给void函数的String的值

[英]Changing value of String passed to void function

I am writing a method to remove whitespaces from a String passed as a pointer to a void function as follows - 我正在编写一种方法,以从作为指针传递给void函数的字符串中删除空格,如下所示-

char *  mystrcpy(char * dest, char * str) {
    dest = malloc(sizeof(str));
    char * tep = dest;
    while(*dest++ = *str++) {

    }
    return tep;
}

void trimWhiteSpace(char * str){

    int counter = 0;
    int i = 0;

    char * res = malloc(sizeof(str));
    for(i = 0; str[i]; i++) {
        if(str[i] != ' ')
            res[counter++] = str[i];
    }
    res[counter] = '\0';


    mystrcpy(str, res);
    printf("I got %s\n", str);
}

However, when I try to test my method using the main - 但是,当我尝试使用main来测试我的方法时-

int main() {
    char * myStr = (char*)malloc(sizeof(char) * 50);
    myStr = "Hello    World   !  ";
    trimWhiteSpace(myStr);
    printf("%s", myStr);
    return 0;
}

the program prints 程序打印

"HelloWorld!" “你好,世界!”

inside of the void trimWhiteSpace(char * str) method but that is not the case with the main which prints the unmodified string. void trimWhiteSpace(char * str)方法的内部,但对于打印未修改的字符串的main而言并非如此。

How do I make sure that the main method prints the same string as printed by the trimWhiteSpace method? 如何确保main方法打印的字符串与trimWhiteSpace方法打印的字符串相同?

Your program starts with a memory leak.:) 您的程序从内存泄漏开始。:)

int main() {
    char * myStr = (char*)malloc(sizeof(char) * 50);
    myStr = "Hello    World   !  ";
    //...

At first you allocated memory and assigned its address to pointer myStr 首先,您分配了内存并将其地址分配给指针myStr

    char * myStr = (char*)malloc(sizeof(char) * 50);

and in the next statement you reassigned the pointer with the address of a string literal 在下一条语句中,您将字符串文字的地址重新分配给指针

    myStr = "Hello    World   !  ";

Take into account that string literals are immutable in C and C++. 考虑到字符串文字在C和C ++中是不可变的。 Any attempt to modify a string literal results in undefined behaviour. 尝试修改字符串文字会导致未定义的行为。

You should use standard C function strcpy that to copy the string literal in the allocated memory. 您应该使用标准的C函数strcpy将字符串文字复制到分配的内存中。

strcpy( myStr, "Hello    World   !  " );

Or another error. 或另一个错误。 You are using operator sizeof applied to a pointer 您正在将运算符sizeof应用于指针

char *  mystrcpy(char * dest, char * str) {
    dest = malloc(sizeof(str));
                  ^^^^^^^^^^^

Usually sizeof( char * ) is equal to 4 or 8 depending on the environment where the program was compiled. 通常, sizeof( char * )等于4或8,具体取决于编译程序的环境。

You should use another standard C function strlen . 您应该使用另一个标准的C函数strlen For example 例如

char *  mystrcpy(char * dest, char * str) {
    dest = malloc( strlen( str ) + 1);

There are other errors in the program. 程序中还有其他错误。

Nevertheless the function that removes blanks could be written much simpler. 但是,删除空格的功能可以编写得更加简单。

Here is a demonstrative program 这是一个示范节目

#include <stdio.h>

char * trimWhiteSpace( char * str )
{ 
    char *q = str; 
    char c = ' ';

    while ( *q && *q != c ) ++q; 

    char *p = q; 

    while ( *q ) 
    { 
        if ( *++q != c ) *p++ = *q; 
    } 

    return str; 
} 

int main( void ) 
{
    char myStr[] = "Hello    World   !  ";

    puts( myStr );
    puts( trimWhiteSpace( myStr ) );
}    

Its output is 它的输出是

Hello    World   !  
HelloWorld!

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

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