Well the thing is - you asked the question to misguide us not to help us to help you.
char ** world = malloc(..);
This is ok.
When you did this before char** world;
and then you do this
**world = ...
Wrong. Because you have used a char
to store the value of a pointer.
Well now see what you did, instead creating a chunk of memory which contains multiple char*
you allocated for char
then again used each of them to store the address of memory where char
will be stored. Yes wrong it is
world = malloc(sizeof(char*) *grammes);
Better
world = malloc(sizeof *world * grammes);
And malloc
's return value should be checked and malloc
returns a void*
which can be implicitly converted to char*
no need to cast the result.
world = malloc(sizeof *world * grammes);
if( world == NULL ){
perror("malloc failed");
exit(EXIT_FAILURE);
}
Check the return value of fscanf
also. You can check the man pages or standard to know their success value that hey return.
chux
points out -
There are few more things but it is not sure if you will be engage in such details
malloc(sz)
may return NULL
if sz = 0
So saying malloc
returns NULL
means error is not perfectly correct or even if the sz
is an overflown value then also it might return NULL
.
Here it is better to write the check like this
if( world != NULL && grammes != 0){
//Error.
}