I am studying C Language I need some explanation about a code segments,
int *p;
p = (int *) malloc(sizeof(int));
What does the (int *) means and whats really happening when you execute the above Code?
I am studying C Language I need some explanation about a code segments,
int *p;
p = (int *) malloc(sizeof(int));
What does the (int *) means and whats really happening when you execute the above Code?
(int *)
is a so called cast operator. It is used to convert the value on the right side of the operator to the type enclosed in parenthesis.
Here, it is used to convert the void *
pointer returned by malloc()
to an int *
, i.e. a pointer to an integer.
This is a very bad usage, and you should not do it.
malloc
has return type void*
, which reads as pointer-to-void which means pointer-to-something.
p
has type int*
, so pointer-to-int. In order to assign the result of malloc
into p
, you need to convert int*
into void*
somehow.
If you write:
int* p = malloc(...)
the conversion happens automatically ("implicit conversion").
If you, for some reason, decide to write:
int* p = (int*) malloc(...)
the result is the same, but the conversion is explicit (i.e. a "cast").
Try this link man malloc
In C, there is no need of typecasting in malloc
so write directly
p = malloc(sizeof(int));
FYI , this (int *)
is nothing but the typecast. Since many functions return void*
so it needs to typecast in the respective type for further manipulations with pointers.
In case of malloc it's implicitely taken care of
(type)
is called type cast operator in C.
suppose for example,
int a;
float b = 11.4;
You want to put the integral value of b
in a
. Use type casting
a = (int)b
Here's the documentation on malloc.
You're allocating a block of memory the size of an int (this value may change depending on the system/compiler combo).
The (int *) casts the right hand side as an int *, which isn't necessary in C, you just need the malloc.
int *p; // pointer to an integer
p = (int *) malloc(sizeof(int)); // assignment of enough memory for an integer
The (int *)
part is called typecasting, you're telling the compiler:
"I know malloc() gives me a void *, but I'm going to assign it to something else".
There are useful reasons to typecast, but it can be dangerous too. In this case since malloc()
returns a void *
you don't need to typecast because it's automatically and safely converted to other pointer types.
Unwind already gave you the answer, but I want to expand on it a bit.
In C, some types are not compatible; you can't directly assign a value of type T
to a variable of type Q
; for example, you can't assign a pointer to int
to a variable that's type pointer to double
:
double *q;
int *p;
...
q = p; // will cause a diagnostic to be issued
If you really need to make that assignment, though, you can use a cast expression, which converts the value to the target type:
q = (double *) p;
This tells the compiler to convert the value of p
to pointer to double
and assign the result to q
.
In the original, K&R version of C, malloc
returned char *
, so if you wanted to assign the result of malloc
to a pointer of a different type, you had to apply the cast:
int *p = (int *) malloc(SIZE);
That changed in 1989, when the first C standard was introduced. That version introduced a void *
type to act as a "generic" pointer type. Values of type void *
can be assigned to other pointer types directly, without need of a cast. So, as of C89 and later, you can simply write
int *p = malloc(SIZE);
This was not only cleaner, it was also safer; if you forgot to include stdlib.h
or otherwise didn't have a declaration for malloc
in scope, the compiler would assume that malloc
returned an int
value and generate code accordingly. Since an int
value can't be directly assigned to a pointer type, you would get a diagnostic. If you added the cast, however, that warning would be suppressed, and you would potentially have issues at runtime.
The 1999 standard got rid of the implicit int
assumption, so that's no longer a real danger, but it's still better style to leave the cast off of a malloc
call.