Wednesday, 1 March 2017

C Syntax Explanation



Hey guys I'm 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, Thaks In Advance.



Answer



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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...