Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer declarations and dereferencing 4

Status
Not open for further replies.

ivanachukapawn2

Technical User
Aug 2, 2005
2
0
0
US
A tutorial says:

float* px;
defines px as a pointer to a float.

AND LATER

float *px;
defines px as a pointer to a float

I am a C newbie and find this very confusing. Can you confirm, are these 2 statements (defining px as a pointer) equivalent?

Is there one way of declaring a pointer predominately used and/or preferred?

Further on in the tutorial, I find this:

The content of the memory location referenced by a pointer is obtained using the ``*'' operator (this is called dereferencing the pointer). Thus, *px refers to the value of x.

This is confusing also. In a previous example, the tutorial states that *px declares px to be a pointer, but in this example *px is said to dereferenc the pointer. When I encounter *px in code how can I discern its meaning?
 
The C declaration (informal) syntax:
Code:
type_name comma_separated_list_of_declarators;
Semicolon terminates declaration.
The simplest declaration:
Code:
type_name declarator;

Now we have in
Code:
float * px;
float - type name,
*px - declarator.

float, * and px are tokens (lexemes, i.e. lexical units of the C language). Remember: whitespaces are optional token separators. See, for example, six identical declarations:
Code:
/*1*/ float*px;
/*2*/ float * px;
/*3*/ float* px;
/*4*/ float *px;
/*5*/ float * px ;
/*6*/ float
*
px
;


There is one and only one name in the declarator (px in your snippet). Declaration meaning: if you see (below the declaration) declarator construct in a correct expression, it yields a value of the basic type. So after float*px this construct: *px must yield float value. In other words, px is a pointer to float because of unary * operator (dereferencing) has a pointer operand in the language (expression *name is correct if and only if name is a pointer).

No common preferred forms of declaration in this case. I like case #3, but be careful:
Code:
float* px, py;
declares px as a pointer to float but py is not a pointer variable! We have two declarators in this declaration: *px and py (not *py)...

That's funny the C language declaration syntax. By the way, see yet another equivalent of the px as a pointer to float declaration:
Code:
float(*px);
 
And expanding on ArkM's answer, anytime you encounter *px outside of a variable declaration, it will be a pointer dereference.
 
>...anytime you encounter *px outside of a variable declaration, it will be a pointer dereference.

Be careful, we can see *px in the snippet::
Code:
typedef struct { float* p; int junk; } S;
S px;
...
*px.p = 0.0; /* *px here... and px is not a pointer! */
Now px is NOT a pointer, it's a structure variable (bad;) name.
Let's set out parentheses (remember operator precedence):
Code:
*(px.p) = 0.0;
Now px.p must be a pointer, but px must be a structure variable name: operator . wants a structure as a left operand.
 
float *px; // this only for the px to be floating pointer

float* x,y; // here both x and y become pointers.

SHRIKANT BADIGER
SJCE MYSORE
 
Code:
float* x, y; /* px - pointer to float, y - float! */
(that's a common C trap for beginners;).
There are two declarators: *x and y - same as two declarations:
Code:
float*x; /* pointer to float */
float y; /* float */

Compare with
Code:
typedef float* Pointer2float;
Pointer2float px, py; /* (That's OK;) */

Incidentally, avoid using float type in math calculations: it has insufficient precision (less than 7 decimal digits). Use double type in all cases (it's the other story where is float data natural habitat;).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top