3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
TZPike05
  • 1,188
  • 5
  • 15
  • 24
  • 4
    Those are all the same syntax. The whitespace is not significant. – user207421 Jun 18 '12 at 14:32
  • 1
    I don't know much about downvoting, but is someone able to explain why post was downvoted? I see the description of a downvote is "This question does not show any research effort, it is unclear, or not useful." However, I was looking for an answer to my question and couldn't find one. I stated I did research and thought I was very clear on what I was looking for. The answer was useful to me in understanding the code I'm working with. Did I miss something? – TZPike05 Jun 18 '12 at 14:42

5 Answers5

6

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.

Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
  • +1 for wikipedia link! :D anytime @TZPike05, can you make my response- or any other - as the *response* to your question.... if you have another question, more specific about C just throw another question! – Cacho Santa Jun 18 '12 at 14:36
  • I believe your question is 100% valid, people who downvoted should explain why they did it in the comments... – Cacho Santa Jun 18 '12 at 14:54
  • Thanks Cacho, first downvote I got so was confused. Try to do everything by the book on here because I've seen bad questions on here and understand what a waste of time they can be for people. – TZPike05 Jun 18 '12 at 14:56
4

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; 
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

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?

Community
  • 1
  • 1
  • 1
    Just as a side note: relying on implicit casting of `void *` in C leads to problems later if one decides to migrate the code to C++. – Hristo Iliev Jun 18 '12 at 17:01
  • 1
    Of course, but, as in the question I linked one mentions, we talking about C only. If one uses `malloc()` instead of `new` in C++, he should consider revising his code. –  Jun 18 '12 at 17:53
2

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.

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

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.

Blindy
  • 65,249
  • 10
  • 91
  • 131