c pointers notation basic question

B

bpascal123

Hi

Can someone help me out remember basic pointer notation?

p points to a type int data
int *pi;

char *pc;
pc points to a type char data

Is there any difference with this notation?
char* pc;

Is pc is still pointing to type char data above?

I understand notation '*' operator means pointer. So if it's following the variable type declaration like int* or char*, is it a different meaning as if there is a space like int *var ?

bpascal
 
M

Mark Storkamp

bpascal123 said:
I understand notation '*' operator means pointer. So if it's following the
variable type declaration like int* or char*, is it a different meaning as if
there is a space like int *var ?

char *p;
and
char* p;
are equivalent. But see
http://c-faq.com/decl/charstarws.html
to see why some prefer one style over the other.
 
R

Roberto Waltman

bpascal123 said:
char *pc;
pc points to a type char data

Is there any difference with this notation?
char* pc;

No.

"White space" (spaces, tabs, new-lines, etc.) is not relevant, so all
of the following are equivalent:

char* pc;
char *pc;
char * pc ;
char *
pc ;
char
*
pc
;

char


*


pc


;

It is recommended to use the form "char *pc;" to make clear what is a
pointer and what is not when declaring more than one variable in a
single statement.

char *yes_a_pointer, not_a_pointer;
 
E

Eric Sosman

Hi

Can someone help me out remember basic pointer notation?

p points to a type int data
int *pi;

char *pc;
pc points to a type char data

Is there any difference with this notation?
char* pc;

Is pc is still pointing to type char data above?

I understand notation '*' operator means pointer. So if it's following the variable type declaration like int* or char*, is it a different meaning as if there is a space like int *var ?

No.

There are only a few places where white space matters in C source:
When it's the only thing that separates two tokens that would otherwise
run together (`int main', not `intmain'), when it's part of a string
literal or character literal ("hello world", ' '), and sometimes in
preprocessor directives (`#define M(x) y' vs. `#define M (x) y', for
example). Everywhere else you may add or remove as much white space
as you please without changing the meaning of the code, provided the
lines don't get too long. So:

char *pc;
char* pc;
char*pc;
char/**/*/**/pc/**/;//**//
char * pc ;

.... are all equivalent. (Yes, comments are "white space" to the
compiler, even if they're un-white enough to strain human eyes!)

Most C programmers I've met seem to prefer

char *pc;

as more readable than the others. It's thought that this form
may reduce the likelihood of making errors like

char* firstName, lastName; // two strings

.... which does not do what the comment suggests (see Question 1.5
on the comp.lang.c Frequently Asked Questions -- FAQ -- page at
<http://www.c-faq.com/index.html>).
 
S

Shao Miller

Hi

Can someone help me out remember basic pointer notation?

p points to a type int data
int *pi;

char *pc;
pc points to a type char data

Is there any difference with this notation?
char* pc;

Is pc is still pointing to type char data above?

I understand notation '*' operator means pointer. So if it's following the variable type declaration like int* or char*, is it a different meaning as if there is a space like int *var ?

The following are the same:

char*pc;
char * pc;
char* pc;
char *pc;
char(*pc);
char(*(pc));
char (* pc);

et cetera.
 
S

Shao Miller

I read that as "Char Star Wars". Anyway, I totally agree 100% with
the point made on that web page. That's how I do it. That's how all
right-thinking people do it. People who do it the other way probably
kick puppies too.

That's just what a right-handed person might say. Obviously,

type * p, * q;

is far superior to:

type* p, * q;

and

type *p, *q;

;)
 
I

Ian Collins

Edward said:
I read that as "Char Star Wars". Anyway, I totally agree 100% with
the point made on that web page. That's how I do it. That's how all
right-thinking people do it. People who do it the other way probably
kick puppies too.

The argument presented is moot if your style rules (are any good and..)
prevent multiple variable declarations one one line.

:)
 
K

Keith Thompson

Roberto Waltman said:
bpascal123 said:
char *pc;
pc points to a type char data

Is there any difference with this notation?
char* pc;

No.

"White space" (spaces, tabs, new-lines, etc.) is not relevant, so all
of the following are equivalent: [snip]
It is recommended to use the form "char *pc;" to make clear what is a
pointer and what is not when declaring more than one variable in a
single statement.

char *yes_a_pointer, not_a_pointer;

It's also recommended not to declare more than one variable in a single
declaration (not statement):

char *yes_a_pointer;
char not_a_pointer;
 
K

Keith Thompson

Eric Sosman said:
Most C programmers I've met seem to prefer

char *pc;

as more readable than the others. It's thought that this form
may reduce the likelihood of making errors like

char* firstName, lastName; // two strings

... which does not do what the comment suggests (see Question 1.5
on the comp.lang.c Frequently Asked Questions -- FAQ -- page at
<http://www.c-faq.com/index.html>).

I can't resist pointing out that this:

char *firstName, *lastName; // two strings

also doesn't do what the comment suggests. firstName and lastName
may be *pointers* to strings if you assign appropriate values
to them.
 
S

Stephen Sprunk

It's also recommended not to declare more than one variable in a
single declaration (not statement):

char *yes_a_pointer;
char not_a_pointer;

"char *pc" is clearly superior to "char* pc"; I'm not aware of any case
where that would impair readability, nor anyone who would seriously
recommend the latter.

I also agree that the form "char* pc, c;" is to be avoided, simply
because it's visually similar to a common and obvious mistake.

However, there are cases where declaring one variable per line hurts
more than it helps. It is more common, in my experience, to see one
type per line:

char *a, *b, *c, *d;
char e, f, g, h;
int i, j, k, l;

If the identifiers are meaningful and relatively short, at least. If
they're long, or if they're cryptic enough to merit comments, then one
per line starts to make sense.

S
 
K

Keith Thompson

Ian Collins said:
The argument presented is moot if your style rules (are any good and..)
prevent multiple variable declarations one one line.

:)

Agreed. On the other hand, I still prefer
char *p;
over
char* p;
because it reflects the grammar.

On the other other hand, if I always followed that rule, my if /
else / else if statements would crawl across the screen.
 
I

Ian Collins

Stephen said:
"char *pc" is clearly superior to "char* pc"; I'm not aware of any case
where that would impair readability, nor anyone who would seriously
recommend the latter.

I also agree that the form "char* pc, c;" is to be avoided, simply
because it's visually similar to a common and obvious mistake.

However, there are cases where declaring one variable per line hurts
more than it helps. It is more common, in my experience, to see one
type per line:

char *a, *b, *c, *d;
char e, f, g, h;
int i, j, k, l;

If the identifiers are meaningful and relatively short, at least. If
they're long, or if they're cryptic enough to merit comments, then one
per line starts to make sense.

Of if you prefer to declare and initialise them at the point of first use.
 
G

glen herrmannsfeldt

bpascal123 said:
Can someone help me out remember basic pointer notation?
(snip)
char *pc;
pc points to a type char data
Is there any difference with this notation?
char* pc;
Is pc is still pointing to type char data above?

Those are the same, but one is less readable.

It is more interesting in Java, where there is:

char []pc;

and

char pc[];

Which again mean the same thing, and similar to the
C meaning of char *pc, but:

char[] x,y;
char x[], y;
char x,y[];

Mean three different things.

-- glen
 
K

Keith Thompson

Stephen Sprunk said:
"char *pc" is clearly superior to "char* pc"; I'm not aware of any case
where that would impair readability, nor anyone who would seriously
recommend the latter.

Bjarne Stroustrup, as I recall, advocates "char* pc;" (and avoiding
multiple declarations per line), and many, perhaps most, C++ programmers
follow that convention. It's easily read as "pc is of type char*", even
though it *really* means "*pc is of type char, from which you can infer
that pc is of type char*".

Yes, C and C++ are distinct languages, but they share the same syntax
and semantics in this particular area.

[...]
 
E

Eric Sosman

Roberto Waltman said:
bpascal123 said:
char *pc;
pc points to a type char data

Is there any difference with this notation?
char* pc;

No.

"White space" (spaces, tabs, new-lines, etc.) is not relevant, so all
of the following are equivalent: [snip]
It is recommended to use the form "char *pc;" to make clear what is a
pointer and what is not when declaring more than one variable in a
single statement.

char *yes_a_pointer, not_a_pointer;

It's also recommended not to declare more than one variable in a single
declaration (not statement):

char *yes_a_pointer;
char not_a_pointer;

Yah. Shrug. Whaddevah.

double sumx;
double sumy;
double sumxx;
double sumyy;
double sumxy;

*So* much better than

double sumx, sumy, sumxx, sumyy, sumxy;

It's a crying shame the grammar permits the latter, don't you think?
 
J

James Kuyper

On 03/16/2013 04:56 PM, Ian Collins wrote:
....
The argument presented is moot if your style rules (are any good and..)
prevent multiple variable declarations one one line.

prevent => prohibit?
 
Ö

Öö Tiib

Roberto Waltman said:
bpascal123 wrote:
char *pc;
pc points to a type char data

Is there any difference with this notation?
char* pc;

No.

"White space" (spaces, tabs, new-lines, etc.) is not relevant, so all
of the following are equivalent: [snip]
It is recommended to use the form "char *pc;" to make clear what is a
pointer and what is not when declaring more than one variable in a
single statement.

char *yes_a_pointer, not_a_pointer;

It's also recommended not to declare more than one variable in a single
declaration (not statement):

char *yes_a_pointer;
char not_a_pointer;

Yah. Shrug. Whaddevah.

double sumx;
double sumy;
double sumxx;
double sumyy;
double sumxy;

*So* much better than

double sumx, sumy, sumxx, sumyy, sumxy;

It's a crying shame the grammar permits the latter, don't you think?

It is also often recommended to declare variables close to first usage
and if possible then to initialize at point of declaration.

It may well be that both above examples are doing neither. Can you bring
bit longer example where the declarations were closest to first usage and
where it does not make sense to initialize them at point of declaration?
 
S

Shao Miller

It is also often recommended to declare variables close to first usage
and if possible then to initialize at point of declaration.

It might be often, but not often when considering C89/C90 compatibility,
of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top