Home » Programming Tips & Tricks » C - Tips & Tricks

How should we declare/define a pointer variable?

By: IncludeHelp, on 08 FEB 2017

Here, I am writing about pointer declaration/definition rule, which style should be used among many styles of pointer declaration/definition?

In the pointer declarations/definitions, a common confusion about placing asterisk (*) sign, it can be placed close to the data type, after the data type and close to the pointer name.

int* ptr;
int * ptr;
int *ptr;

Recommended style of pointer declaration/definition

Style 3 (int *ptr;) is highly recommended that means - Asterisk (*) sign should be placed close to the pointer name.

This style is highly understandable as we can clear see that which variable is the pointer variable, so you should always use this style.

It is also helpful, when you are declaring pointer and normal variable together, let suppose you want to declare an integer variable and an integer pointer, then we can use this style to declare/define these variables.

int var, *ptr;

In this statement ptr is a pointer variable, while var is a normal integer variable.

Consider the given example:

#include <stdio.h>
int main()
	int var,*ptr;
	printf("var= %d\n",*ptr);
	return 0;


var= 10

In this program, there are two variables ptr as integer pointer and var as integer variable, both are declared using a single declaration statement int var, *ptr;

As you can see the declaration, it is cleared that var is an integer variable (because it does not contain * with the variable name) and ptr is an integer pointer (because it contains * with the ptr).

I would recommend using this style of pointer declaration.

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

© https://www.includehelp.com (2015-2018), Some rights reserved.

close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.