I am a newbie in C.
I know this is correct:
char* Str;
Str = (char*)calloc(Str_Len, sizeof(char));
, but why this is not correct?
char* Str;
*Str = (char*)calloc(Str_Len, sizeof(char));
How to modify it? Thanks.
I am a newbie in C.
I know this is correct:
char* Str;
Str = (char*)calloc(Str_Len, sizeof(char));
, but why this is not correct?
char* Str;
*Str = (char*)calloc(Str_Len, sizeof(char));
How to modify it? Thanks.
First is legal, but do not cast the return value of malloc
or calloc
in C (as their return type is void *
).
In second case Str
is char
type, you can't allocate memory more thatn 1
byte to it. Also calloc
returns pointer
but *Str
is of type char
. You can't assign a char *
data type to char
type.
Actually none of both is correct, since there is no need to cast a void*
in C to anther
type, it's implicitly convertible. The cast is not an error per se but it could led to hidden errors.
The latter is wrong because *Str
dereferences the pointer (thus you access the char
) which is not a pointer type and it's not assignable from a pointer.