Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 通过引用将结构的成员传递给函数_C - Fatal编程技术网

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

C 通过引用将结构的成员传递给函数,c,C,如何通过引用传递结构成员?我不想将整个结构传递给函数 我试图创建一个addString函数,它动态地分配足够的内存来将字符串存储在结构成员中 看起来一切正常,直到函数addString完成并且我想打印结构的title成员之后,它仍然有垃圾 #include <stdio.h> #include <stdlib.h> #include <string.h> struct program { int check; char* ti

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

我试图创建一个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;
}
编译器应该为此代码向您发出警告。 值得注意的是,&programs->title的类型是char**,而不是char*

适当的addString函数可能如下所示:
以下是您的程序的更正版本。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;
}

啊,对了,我已经试着解决这个问题两个多小时了。谢谢!还有一个简单的问题,我正在使用Visual Studio 2012,我的警告级别为:EnableAllWarnings,但我没有收到任何警告。@CrazyEgoist:通常,较新的编译器提供更好的警告和错误消息。我通常在linux上使用它来获取高质量的错误消息,就像我在回答中包含的错误消息一样。@CrazyEgoist:Visual Studio 2013也会对此发出警告:我们在学校使用Visual Studio,所以我必须继续使用它,但当我再次陷入困境时,我会尝试更新的编译器,再次感谢!好的,我只是再次检查,当我测试我的代码时,我主要使用调试器,一行一行地检查我的代码,看看内存中发生了什么,看起来它有时根本没有显示任何警告。但我刚刚看到,当我重建我的解决方案时,它确实向我显示了所有警告,非常好。这将在将来为我节省很多时间。主函数是传递一个指向addString函数指针的指针。所以addString需要有一个指向指针参数的指针。为什么addString定义为返回一个在main中被忽略的char*,然后实际返回一个整数,如果main注意到返回的值为NULL,则在main中可能会出错?如果启用编译器上的所有警告,你会被告知这个程序有什么问题。顺便说一句:警告需要修复,不能忽略当在C中使用malloc和family函数时,1不要强制转换malloc返回的值2始终检查!=NULL返回值,以确保操作成功是的,这是我犯的错误,我的警告设置很好,但我只是找不到正确的位置。将函数改为void会更好吗,因为我实际上没有返回任何东西?在学校使用malloc或其他类似函数时,我们必须进行强制转换。因为到时候我们会更好地了解我们在做什么。也感谢你帮助我!这是我在这里发布的第一个问题,我没想到会得到这么快的回复。
void addString(char** destination, char* string) {
    *destination = malloc(strlen(string) + 1);
    strcpy(*destination, string);
}
#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;
}