0

I have the following struct:

struct Payload
{
  int id;
  int nameLength;
  char *fileName;
  int filesize;
  char *content;
};

I know the maximum struct size including arrays beforehand, but not the individual array sizes for fileName and content. I want to pass a pointer to this struct to my functions and preferably keep allocation outside. Is there a way to allocate space for the struct in a way that includes the pointer data?

2
  • 1
    See: How to create a Minimal, Reproducible Example. The way your question is now, people can only guess how to resolve your problem. Commented May 1, 2020 at 13:55
  • I have added some of my code and tried to formulate it better. @fiddling-bits Commented May 1, 2020 at 14:18

2 Answers 2

1
struct Payload *xxx;
xxx = calloc(5, sizeof *xxx); // space for 5 structs, all zero initialized

if (fileNamelen) xxx[2].fileName = malloc(fileNamelen + 1);
if (contentlen) xxx[4].content = malloc(contentlen + 1);

Remember to free() resources when you no longer need them.

free(xxx[2].fileName);
free(xxx[4].content);
free(xxx);
Sign up to request clarification or add additional context in comments.

Comments

0

you can allocate the the pointer inside the struct as the other primary variable (int, float,etc).

For example you have print_struct function:

void print_struct(struct Payload * pl) {
    printf("filename: %s\n", pl->filename);
    printf("content: %s\n", pl->content);
}

Before this function, you have to allocate for content and filename.

struct Payload pl1; 
struct Payload *pl2 = malloc(sizeof(struct Payload)); // use pointer.
if(!pl2) {
   // handle the error
}
pl1.content = malloc (sizeof(char)*30); // max length of content is 29 in this case.
if(!pl1.content) {
   // handle the error
}
pl1.filename = malloc (sizeof(char)*30); // max length of filename is 29 in this case.
if(!pl1.filename) {
   // handle the error
}

pl2->content = malloc (sizeof(char)*30); // max length of content is 29 in this case.
if(!pl2->content) {
   // handle the error
}
pl2->filename = malloc (sizeof(char)*30); // max length of filename is 29 in this case.

if(!pl2->filename) {
   // handle the error
}

Then when you call the print_struct function:

print_struct(&pl1) // pass by reference 
print_struct(pl2)  // pass by pointer

Do not forget to free memory to avoid memory-leaks.

free(pl1.content);
free(pl1.filename);

free(pl2->content);
free(pl2->filename);
free(pl2);

Short program for testing:

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

struct Payload
{
  int id;
  int nameLength;
  char *filename;
  int filesize;
  char *content;
};

void print_struct(struct Payload * pl) {
    printf("filename: %s\n", pl->filename);
    printf("content: %s\n", pl->content);
}

int main()
{
    struct Payload pl1; 
    struct Payload *pl2 = malloc(sizeof(struct Payload)); // use pointer.
    if(!pl2) {
       return 0;
    }
    pl1.content = malloc (sizeof(char)*30); // max length of content is 29 in this case.
    if(!pl1.content) {
        return 0;
    }
    strcpy(pl1.content,"pl1.content");
    pl1.filename = malloc (sizeof(char)*30); // max length of filename is 29 in this case.
    if(!pl1.filename) {
        return 0;
    }
    strcpy(pl1.filename,"pl1.filename");

    pl2->content = malloc (sizeof(char)*30); // max length of content is 29 in this case.
    if(!pl2->content) {
        return 0;
    }
    strcpy(pl2->content,"pl2->content");
    pl2->filename = malloc (sizeof(char)*30); // max length of filename is 29 in this case.

    if(!pl2->filename) {
        return 0;
    }
    strcpy(pl2->filename,"pl2->filename");

    print_struct(&pl1); // pass by reference 
    print_struct(pl2);  // pass by pointer
    free(pl1.content);
    free(pl1.filename);

    free(pl2->content);
    free(pl2->filename);
    free(pl2);

    return 0;
}

the output:

filename: pl1.filename                                                                                                  
content: pl1.content                                                                                                    
filename: pl2->filename                                                                                                 
content: pl2->content

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.