TIL: Recommended Way to Define Pointer Variables in C

Jan 16, 2023

Today, I learn that we should use int* x to define a pointer variable than int *x. Why?

Following is the way, we can define a variable in C.

int x = 3;
printf("%d\n", x); // prints "3".

If we prefix this x variable with & as &x, it will return the address of x. The type of this return address is int* meaning address of integer and * as pointer to that address.

int* address = &x;

To print the address, we can use %p as placeholder.

printf("%p\n", address);

Notice, here I wrote address and not *address. Because, * is NOT part of address variable name than part of type.

Tags: til c