TZPike05
TZPike05

Reputation: 1188

Pointers in C with parenthesis

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.

Upvotes: 3

Views: 2523

Answers (5)

John Bode
John Bode

Reputation: 123448

The (double*) is a cast; whenever you see an expression of the form (type-name) expression, it means "interpret the result of expression as a value of type type-name". In this case, it's saying "interpret the result of malloc as a pointer to double".

Normally, pointer values of one type (such as char *) cannot be directly assigned to pointer variables of a different type (such as double *), so you have to use the cast expression to explicitly convert the source value to the target type. Before the 1989 standard, malloc, calloc, and realloc all returned char * values, so you had to use a cast to assign the result to a different pointer type.

The void * type was introduced in the 1989 standard as a generic pointer type that can be assigned to different pointer types without the need for a cast, and the *alloc functions were changed to return values of that type. Explicitly casting the result of malloc is now considered bad practice.

The structure of the type in a cast expression closely matches the structure of the type in a declaration, just without the name of the thing being declared. This is probably best explained with some examples.

int *p declares p as a pointer to an int; to cast the result of an expression to a pointer to int, you write (int *). It's the same as the declaration, minus the identifier p.

Here are a few more examples:

Declaration             Cast                 Type
-----------             ----                 ----
int (*ap)[10]           (int (*)[10])        Pointer to 10-element array of int
int (*f)(void)          (int (*)(void))      Pointer to function returning int
char **p                (char **)            Pointer to pointer to char

So again, the structure of a cast expression is the same as the structure of a declaration, minus the name of the thing being declared.

Upvotes: 2

user529758
user529758

Reputation:

This means that it's a cast from malloc()'s return type (void *) to float *. What is a cast? http://en.wikipedia.org/wiki/Type_conversion#Explicit_type_conversion

By the way, it's a very bad practice to cast the return value of malloc(). See why: Do I cast the result of malloc?

Upvotes: 2

Blindy
Blindy

Reputation: 67380

It's in parenthesis because it's a cast, which is basically telling the compiler, hey, you there, treat this piece of memory as having this type because I say so (incidentally, it's the leading cause of errors in C, besides memory allocation/deallocation pairs mismatch).

In C it's also considered bad form to cast the result of malloc, since casts from void * are automatic. It's just adding code noise for no benefit.

As to the three types of variable declarations, keep in mind that int* a, b; is very different from int *a, *b. I prefer the latter because it shows explicitly the pointer binding to the variable name, not the type.

Upvotes: 1

Cacho Santa
Cacho Santa

Reputation: 6914

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.

Upvotes: 6

David Heffernan
David Heffernan

Reputation: 612914

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; 

Upvotes: 4

Related Questions