What is the difference between float * varname and float * varname in classic c

What is the difference between float * varname and float * varname in classic C?

+7
c syntax
source share
6 answers

Formatting It. They mean the same thing.

If you put space (or if you have it, really), this is a matter of preference. Some prefer * next to varname, so you are not confused with something like:

float* a, b; 

(here only a is a pointer, b is not)

Others argue that they never declare multiple variables at once, so they prefer * next to float, as it is part of the type.

+22
source share

There is no difference. Spaces are completely ignored.

However, note that the following may be misleading:

 float* var1, var2; 

A casual examination of the above will lead you to the idea that both var1 and var2 are pointers to float. However, you are mistaken because it declares var1 as a pointer to a float, but var2 will be a regular float.

+8
source share

The latter is more obvious in that it shows that you are creating a variable that is a pointer to a float, instead of creating a variable that is a pointer to a float. This becomes important in designs such as:

 float *foo, bar; 

foo is a pointer to a float, but bar is just a float.

+4
source share

Basically, this is a coding style issue. However, most C programmers prefer the second option: float *var; , since it is closer to how the language syntax binds an asterisk * : * attached to the declarator, and not to the type specifier.

In other words, C programmers translate float *var since *var is of type float. but not var is of type float* .

Wikipedia article has a more detailed explanation of C types and variable declarations

Also, you can find the question C-pointer Syntax: polling style .

+1
source share

As others have said, it does not matter - a matter of style. Please note that in C ++ it is customary to write:

 type* var; 

Because the pointer is part of the type - at least this is true for custom types. But also for built-in types. Therefore, in C, it makes sense to do the same in order to be consistent. Personally, I find it more intuitive to have a type pointer.

0
source share

There is no difference; the ad is parsed as if it were written float (*varname); .

In C and C ++, declarations are centered around expressions, not objects; basically, the form of the declaration should match the form of the expression in the executable code (IOW, using simulated declarations). For example, if you have a pointer to an integer named p and you want to access this integer value pointed to by p , you cast the pointer like this:

 x = *p; 

Expression Type *p - int; so the declaration should look like

 int *p; 

Similarly, if you have an int array called arr and you want to access the integer value in a specific element i , you should write

 x = a[i]; 

Type of expression a[i] - int; so the declaration should look like

 int a[N]; 

In the above examples, *p and a[N] are called declarators; declarators enter the name of the declared object along with additional type information not specified in the type specifier. The intensity of both p and a provided by an int specifier, but the pointer p and the array a provided by their respective declarators.

This is an important point and something that needs to be emphasized; * like int *p; , so int* p; tied to the declaration, not the type specifier, regardless of the space. The fact that you can write it anyway is an accident of the C (and C ++) syntax, and as a result there is a school of thought that pointer variables should be declared as T* p; unlike T *p; since type p is a "pointer to T". My problem is that reasoning only works for simple pointer types; we cannot treat an array or types of functions in the same way. For example, we cannot write int[N] arr; although the arr type is "N-element array of int". It really crashes when we get the declaration of pointers to arrays or pointers to functions or their combinations. For example:

 int *(*(*(*farr)())[N])(); 

The farr type is "a pointer to a function that returns a pointer to an array of N-elements of function pointers that return pointers to int". Everything except "int" is part of the declarator; notation int* (*(*(*farr)())[N])(); or even int*(*(*(* farr)())[N])(); will be just stupid.

I believe that insisting on writing T* p; for declarations of pointers leads to more confusion, no less. Since Stroustrup and the legions of C ++ programmers would like to pretend otherwise, declarations in C and C ++ are centered around expressions, not objects.

Since I am part of a larger C ++ code writing team, I follow consistent coding rules that include declaring the pointer and references as T* p; and T& r; but it makes me grind my teeth every time I have to do it.

0
source share

All Articles