Desert Wind
Desert Wind

Reputation: 3

C, how to fill in an allocated struct with strings of fixed sizes

I am very new to allocating. Below is a simplefied version of my code. I need to allocate a struct in which variable data needs to be stored that has a set length when it passes through several functions. I actualy don't care much if the struct itself has these set max characters, but the variables I get will have these setup for sure.

What I get now is errors like: error: array type 'char[260]' is not assignable

and I am worried that I am not allocating enough room for my struct that get me in trouble later. ... So I am not sure how to solve this problem and do it correctly.

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


typedef struct megaStruct {
    char dirPath[260+1];
}megaStruct;

int main(void) {

    // allocating
    struct megaStruct *toTheStruct;
    toTheStruct = (struct megaStruct*)malloc(sizeof(struct megaStruct));

    // Dir Path
    char CustomPath[260] = "C:\\Windows\\";

    // wrtiting it to the struct
    toTheStruct->dirPath = CustomPath;

    return 0;
}

Thank you very much for your time and energy!

Upvotes: 0

Views: 41

Answers (0)

Related Questions