I'm having some problems with dynamic allocation, because I don't want to specify the size of the array. Basically what I need in my program is to store a name of a file in an array and make sure it is not wasting space.
I was trying to do something like creating the array name[255]
to make sure it had enough space and then dynamically allocate it so it had only the required space, like writing "matrix5", then store it in name[255]
and then change name[255]
to name[8]
.
char file_name[255];
printf("what is the name of the file? [.txt]\n");
scanf_s(" %s", file_name[255]); //store name eg: "matrix5"
i = 0;
char **fullpath;
while (file_name[i] != '\0' && i <=strlen(file_name))
{
fullpath= (char**)malloc(sizeof(char*)); //In here it would then make file_name[255] into file_name[8]
fullpath[i] = file_name[i];
i++;
}
What do I need to do here?