小编Neo*_*eoR的帖子

指向指针和strcpy的函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Get_Text(char *string);
void Get_Text_Double(char **string);
void String_Copy(char *string);

int main(void) {
    char *name = malloc(10 * sizeof(char));
    char *name2 = malloc(10 * sizeof(char));
    char *name3 = malloc(10 * sizeof(char));

    Get_Text(name);
    printf("\n%s\n", name);

    Get_Text_Double(&name2);
    printf("\n%s\n", name2);

    String_Copy(name3);
    printf("\n%s\n", name3);

    return 0;
}

void Get_Text(char *string) {
    string = "test";
}

void Get_Text_Double(char **string) {
    *string = "test2";
}

void String_Copy(char *string) {
    strcpy(string, "test3");
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,为什么Get_Text_DoubleString_Copy函数有效,而Get_Text函数却没有?

另外为什么String_Copy …

c pointers function

2
推荐指数
1
解决办法
233
查看次数

联盟和内存管理

我在接受采访时给了我以下代码: -

union mix
{
    int i;
    char c[2];
};

int main()
{
    union mix key;
    key.i=512;
    printf("\n %d,%d,%d",key.i,key.c[0],key.c[1]);
    return 0;   
}
Run Code Online (Sandbox Code Playgroud)

当我回到家并在系统上运行时,我得到了输出: - 512,0,2.

任何人都可以向我解释它是如何工作的或为什么c [1] = 2

编辑: - 我所知道的是它与内存中的位存储有关.

c bits unions

0
推荐指数
1
解决办法
107
查看次数

标签 统计

c ×2

bits ×1

function ×1

pointers ×1

unions ×1