代码之家  ›  专栏  ›  技术社区  ›  JuMoGar

在函数之间传递char指针-两种方式是否相等?-C

  •  1
  • JuMoGar  · 技术社区  · 8 年前

    我已经完成了一个将char指针传递到其他函数的MCVE代码。如果传递char指针参数的两种方式相等(如何 str1 str2 已传递到 passingCharPointer1 passingCharPointer2 体面地)。

    此外,我还将注释包含在代码中,包括自由/空函数的行为及其行为(如果也阅读它们,我将不胜感激)。

    代码为:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_LENGTH 50
    
    void passingCharPointer1(char *str) {
        strcpy(str, "Hi-1!");
    }
    
    void passingCharPointer2(char **str) {
        strcpy(*str, "Hi-2!");
    }
    
    
    
    int main() {
        // Init char pointers
        char *str1 = malloc((MAX_LENGTH +1)*sizeof(char));
        char *str2 = malloc((MAX_LENGTH +1)*sizeof(char));
    
        // Gets their values
        printf("Input string 1: ");
        fgets(str1, MAX_LENGTH , stdin);
        printf("Input string 2: ");
        fgets(str2, MAX_LENGTH , stdin);
        printf("\n");
    
        // Remove '\n' character
        str1[strcspn(str1, "\n")] = '\0';
        str2[strcspn(str2, "\n")] = '\0';
    
        // Print their values
        printf("BEFORE - Function 1: %s\n", str1);
        printf("BEFORE - Function 2: %s\n", str2);
    
        // Pass to function in two ways - ARE BOTH WAYS EQUAL?
        passingCharPointer1(str1);
        passingCharPointer2(&str2);
    
        // Print their values
        printf("AFTER - Function 1: %s\n", str1);
        printf("AFTER - Function 2: %s\n", str2);
    
        // Freeing pointers
        free(str1);
        free(str2);
    
        // Print their values after freeing
        printf("\nAFTER FREE 1: %s\n", str1); // Show rare characters (I supposse it is normal behaviour after free)
        printf("AFTER FREE 2: %s\n", str2); // Continue having its content (I supposse it is not normal behaviour after free)
    
        // Nulling pointers
        str1 = NULL;
        str2 = NULL;
    
        // Print their values after nulling
        printf("\nAFTER NULL 1: %s\n", str1); // Normal behaviour
        printf("AFTER NULL 2: %s\n", str2); // Normal behaviour
    
        // Exit success
        return 0;
    }
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Vlad from Moscow    8 年前

    一般来说,这些功能是不等价的。第一个函数通过值接受指针,而第二个函数通过引用接受指针。因此,第二个函数可以更改表达式中用作参数的原始指针。

    考虑以下演示程序

    #include <stdio.h>
    
    void passingCharPointer1( char *s ) 
    {
        s = "Bye";
    }
    
    void passingCharPointer2( char **s )
    {
        *s = "Bye";
    }
    
    int main(void) 
    {
        char *s1 = "Hello";
        char *s2 = "Hello";
    
        printf( "Before function calls: %s %s\n", s1, s2 );
    
        passingCharPointer1( s1 );
        passingCharPointer2( &s2 );
    
        printf( "After function  calls: %s %s\n", s1, s2 );
    
        return 0;
    }
    

    其输出为

    Before function calls: Hello Hello
    After function  calls: Hello Bye
    

    释放内存后访问内存会调用未定义的行为。

        2
  •  1
  •   fearless_fool    8 年前

    如果你的问题是真的:

    传递参数的两种方式相等(function1和function2)

    (并且忽略中的所有代码 main() ),那么是的,这两个函数本质上是等价的。

    不同之处在于,function1使用指向char的指针(在C语言中,char是字符串的习惯用法),function2使用指向char的指针。

    但正如莫斯科的@Vlad所指出的,function2允许您修改传递给函数的指针。