繁体   English   中英

通过引用将结构的成员传递给函数

[英]Passing a member of a struct to a function by reference

如何通过引用传递struct MEMBER? 我不想将整个结构传递给函数。

我正在尝试创建一个函数addString,它动态地分配足够的内存来存储结构成员中的字符串。

看起来一切正常,直到函数addString完成后我想要打印结构的title成员,它仍然有垃圾。

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

struct program
{
        int check;
        char* title;
        char* channel;
};

char* addString(char* destination, char* string);

int main(void)
{
        struct program* programs = (struct program*) malloc(sizeof(struct program)); //memory for the struct
        addString(&(programs->title), "test"); //passes the adress of the struct member to the function
        printf("%s", programs->title); //and when I print this there is still garbage in it and not the actual string "test"
        return 0;
}

char* addString(char* destination, char* string)
{
        *destination = (char*) malloc(strlen(string) + 1); //dynamic memory alloction for the length of the string
        strcpy(destination, string); //string copy to the pointer returned from the malloc here above
        return 0;
}

您的编译器应该通过此代码向您发出警告。

red.c:17:15: warning: incompatible pointer types passing 'char **' to parameter of type 'char *'; remove & [-Wincompatible-pointer-types]
    addString(&(programs->title), "test"); //passes the adress of the struct member to the function
              ^~~~~~~~~~~~~~~~~~
red.c:12:23: note: passing argument to parameter 'destination' here
char* addString(char* destination, char* string);
                      ^
red.c:24:18: warning: incompatible pointer to integer conversion assigning to 'char' from 'char *'; dereference with * [-Wint-conversion]
    *destination = (char*) malloc(strlen(string) + 1); //dynamic memory alloction for the length of the string
                 ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                   *
2 warnings generated.

值得注意的是, &(programs->title)的类型为char ** ,而不是char *

适当的addString()函数可能如下所示:

void addString(char** destination, char* string) {
    *destination = malloc(strlen(string) + 1);
    strcpy(*destination, string);
}

以下是您的计划的更正版本。 addString方法的主要问题是它应该将指针传递给指针,而不仅仅是指针

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

struct program //this is the struct that is in the file program.h
{
        int check;
        char* title;
        char* channel;
};

char* addString(char** destination, char* string); //Replace char* with char**

int main(void)
{
        struct program* programs = (struct program*) malloc(sizeof(struct program)); //memory for the struct
        addString(&(programs->title), "test123"); //passes the adress of the struct member to the function
        printf("%s", programs->title); //and when I print this there is still garbage in it and not the actual string "test"
        return 0;
}

char* addString(char** destination, char* string)
{
        *destination = (char*) malloc(strlen(string) + 1); //dynamic memory alloction for the length of the string
        strcpy(*destination, string); //string copy to the pointer returned from the malloc here above
        return 0;
}

暂无
暂无

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

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