3

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?

peterh
  • 11,875
  • 18
  • 85
  • 108

6 Answers6

9

(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.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
3

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").

Kos
  • 70,399
  • 25
  • 169
  • 233
1

Try this link man malloc

In C, there is no need of typecasting in mallocso 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 bin a. Use type casting

a = (int)b

Omkant
  • 9,018
  • 8
  • 39
  • 59
  • 1
    True, but I think it's always a good thing to do the implicit conversion, you know what you are doing this way. – Joze Nov 28 '12 at 15:55
  • 1
    *"Using typecast you can convert any type to any other type."*, keyword: *type*. It doesn't convert the *values* except when the standard says so (e.g.: for numeric types). – netcoder Nov 28 '12 at 15:57
  • @YasassriChaturangeRatnayake: For more .. see this http://stackoverflow.com/questions/4993327/is-typecast-required-in-malloc – Omkant Nov 28 '12 at 15:59
  • @Joze No, it is almost always a bad thing. Please see the answers to http://stackoverflow.com/questions/4993327/is-typecast-required-in-malloc and http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/ . It can be an acceptable compromise to cast in some circumstances, but not because “you know what you are doing”. One demonstrates that one knows what one is doing by not casting the result of `malloc()`. – Pascal Cuoq Nov 28 '12 at 16:16
  • If you have to use your code in C++ aswell it's really useful for compatibility. But I agree with argument. Thanks for the information. – Joze Nov 28 '12 at 16:26
1

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Boumbles
  • 2,473
  • 3
  • 24
  • 42
1
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.

Mike
  • 47,263
  • 29
  • 113
  • 177
1

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198