3 Replies - 1531 Views - Last Post: 28 October 2006 - 09:36 AM Rate Topic: -----

#1 CBart21  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 28-October 06

Pointer Code Syntax

Posted 28 October 2006 - 08:59 AM

I just have a question about the syntax pointers.

Not that it at all matters when it comes to compiling the code, but, would it be better for the syntax be:

int *somePointer;


instead...

int* somePointer;


The way I learned pointers is that the code is read right to left. For example...

int *somePointer;

is read: somePointer is a pointer to an int.

it seems to me that the code:

int* somePointer;

would be more confusing for a beginner, especially when you write the code...

int x = 7;
int* somePointer;
somePointer = &x;
*somePointer = 100;



If I was a beginner dealing with the complexities of pointers, I would like to see that the variable name is the one with the asterix (*), instead of the data type. My first question, if I was a beginner would be, where did the asterix come from in the code:

*somePointer = 100;


I hope this argument makes sense. I would like some feedback about what you guys think.

Thanks,

CBart21

Is This A Good Question/Topic? 0
  • +

Replies To: Pointer Code Syntax

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Pointer Code Syntax

Posted 28 October 2006 - 09:15 AM

I'm not sure what your question is, but from a compilers point of view, it doesn't really matter...placement is a matter of preference, although there are valid reasons for preferring one over the other.

I use the following syntax myself:
int *myIntPtr;


Generally I find it cleaner for readability, but also because it eliminates the perception the following can erroneously cause:
int* ptrA,ptrB;


Many people might read that as declaring two pointers, associating the asterisk with multiple declarations. By keeping the asterisk next to the variable instance, I find it more clearly states that the particular instance is the pointer...but that's just me.

Overall, I'm not sure if it would be more confusing for a beginner to use the variable type placement...I've always done it one way.
Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Pointer Code Syntax

Posted 28 October 2006 - 09:29 AM

much of the problem comes from C (and C++) using the same symbol (e.g. *) to mean different things depending context. In this case
	type *p_type;									 /* define a 'pointer to type' */


Prefixing an identifier with an * defines a variable of type type *, which is a 'pointer to type', i.e. p_type can hold the address of an variable of type. For example:
	int *p_int;										/* define a 'pointer to int' */


This defines p_int to be of type int * which is a 'pointer to int'.

you can put the * by the int
 
   int* p_int;										/* define a 'pointer to int' */


but is not advised as this can cause confusion in a statement such as
	int* p_int, x;


p_int is a 'pointer to int' but x is an int.

You then use the same symbol * as the indirection or dereferencing operator *, i.e. to access the variable to which a pointer points (holds the address of), the pointer is prefixed by the indirection or dereferencing operator *, e.g.:
	int *p_int;					 /* define a pointer to an int */				  
	int number;				   /* define an int variable */
	p_int = &number;		  /* point p_int to variable number */
	*p_int = 10;	 /* assign the value 10 to the int pointed to by p_int */


This post has been edited by horace: 28 October 2006 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 CBart21  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 28-October 06

Re: Pointer Code Syntax

Posted 28 October 2006 - 09:36 AM

I appologize for not being clear enough in my Topic. Thank you for the clearity if your replies. It is easier to see what is going on with the code:

int *somePointer


and in some cases easier to read. It just comes down to personal preference.

Thanks again,

CBart21
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1