简体   繁体   中英

Passing a member of a struct to a function by reference

How do I pass a struct MEMBER by reference? I do not want to pass the whole struct to the function.

I'm trying to make a function addString, that dynamicly allocates enough memory to store a string in a struct member.

It looks like that everything works fine until after the function addString finishes and I want to print the title member of the struct, there is still garbage in it.

#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;
}

Your compiler should be throwing a warning at you over this code.

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.

Notably, &(programs->title) is of type char ** , not char * .

An appropriate addString() function might look like:

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

Following is a corrected version of your program. The main issue with addString method is that it should pass a pointer to pointer, rather than just a pointer

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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