Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers in C with parenthesis

Tags:

c

pointers

I found this line of code in some code I'm analyzing:

Mintau = (double*) malloc(FadeAll.num_paths*sizeof(double));

I also found a question on here (that was a duplicate of other questions it appears) that explains different syntax of pointers including:

int *ptr;  
int * ptr;  
int* ptr; 

I should explain that I fully understand that all three of the above are saying the same thing. The last one is the one that most closely resembles my line of code. What I was wondering is why double has to be in parenthesis in this case? I apologize if this is a duplicate question but I couldn't find any regarding this.

like image 426
TZPike05 Avatar asked May 11 '26 07:05

TZPike05


2 Answers

There are no difference between this three declarations:

int *ptr;  
int * ptr;  
int* ptr; 

(double*) has to be in parenthesis because it is a Cast.

Read a little about casting in this link

Hope it helps.

like image 127
Cacho Santa Avatar answered May 13 '26 01:05

Cacho Santa


That signifies a cast. The function malloc returns a void*. The (double*) casts that value to be of type double*.

In C, this cast is needless since any pointer type is assignment compatible with void*. But if this was C++ then the cast would be needed.

There is no difference between:

int *ptr;  
int * ptr;  
int* ptr; 
like image 43
David Heffernan Avatar answered May 13 '26 01:05

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!