pointer parameter where to put the asterix in C?

Where to put the asterisk in C pointer parameters?

In C, the asterisk (*) is used to declare a pointer variable. When declaring a pointer parameter in a function, the asterisk can be placed in two different positions:

  1. Before the parameter name: This is the most common and recommended way to declare a pointer parameter in C. Placing the asterisk before the parameter name indicates that the parameter is a pointer to a certain type. For example:

c void foo(int* ptr);

In this example, ptr is a pointer to an integer.

  1. Before the parameter type: Placing the asterisk before the parameter type is less common and can be confusing for some readers. However, it is still valid syntax in C. For example:

c void foo(int *ptr);

This declaration is equivalent to the previous example.

Both ways of declaring pointer parameters are valid and have the same effect. The choice of where to place the asterisk is mostly a matter of personal preference and coding style. However, it is important to be consistent within a codebase to maintain readability and avoid confusion.

It's worth noting that when dereferencing a pointer within the function body, the asterisk is placed before the pointer variable itself, not the parameter declaration. For example:

void foo(int* ptr) {
    *ptr = 42; // Dereferencing the pointer
}

In this example, *ptr is used to assign the value 42 to the memory location pointed to by ptr.

[1]