[Solved] getting a “free(): invalid pointer” in c


I’m just going to focus on what is causing your error (I didn’t read most of your code).

Pointers are one of the concepts in C that take a long time to learn, and you have a long way to go.
A pointer is just an address in memory, nothing else. You can think of *pointer as being a function call that says, “take the number stored in pointer, go out to memory, and return the value that you find at the address corresponding to that number”.


When you say:

char* pointer = fileStruct->name;  

You aren’t connecting those two variables in any way. It would be like saying:

int foo = 3;
int bar = foo;

For now they have the same value, but if you change foo, bar doesn’t change.

In your code, you aren’t actually using pointer anywhere, so you can just gt rid of it and call free(fileStruct->name) when you are done using it.


That being said, you need more practice/reading/learning about how pointers work. If you are just starting to program in general, you might want to avoid pointers all together until you are are comfortable with the basics.

2

solved getting a “free(): invalid pointer” in c