Velocity Reviews > c pointers notation basic question

# c pointers notation basic question

bpascal123
Guest
Posts: n/a

 03-16-2013
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

Mark Storkamp
Guest
Posts: n/a

 03-16-2013
In article <(E-Mail Removed)>,
bpascal123 <(E-Mail Removed)> wrote:

> 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.

Roberto Waltman
Guest
Posts: n/a

 03-16-2013
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:

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;
--
Roberto Waltman

bpascal123
Guest
Posts: n/a

 03-16-2013
thanks

Eric Sosman
Guest
Posts: n/a

 03-16-2013
On 3/16/2013 10:24 AM, bpascal123 wrote:
> 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>).

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d

Shao Miller
Guest
Posts: n/a

 03-16-2013
On 3/16/2013 10:24, bpascal123 wrote:
> 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.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter

Edward A. Falk
Guest
Posts: n/a

 03-16-2013
In article <(E-Mail Removed)-september.org>,
Mark Storkamp <(E-Mail Removed)> wrote:
>
>But see
>http://c-faq.com/decl/charstarws.html

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.

--
-Ed Falk, (E-Mail Removed)
http://thespamdiaries.blogspot.com/

Shao Miller
Guest
Posts: n/a

 03-16-2013
On 3/16/2013 13:55, Edward A. Falk wrote:
> In article <(E-Mail Removed)-september.org>,
> Mark Storkamp <(E-Mail Removed)> wrote:
>>
>> But see
>> http://c-faq.com/decl/charstarws.html

>
>
> 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;

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter

Ian Collins
Guest
Posts: n/a

 03-16-2013
Edward A. Falk wrote:
> In article <(E-Mail Removed)-september.org>,
> Mark Storkamp <(E-Mail Removed)> wrote:
>>
>> But see
>> http://c-faq.com/decl/charstarws.html

>
>
> 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.

--
Ian Collins

Keith Thompson
Guest
Posts: n/a

 03-16-2013
Roberto Waltman <(E-Mail Removed)> writes:

> 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;

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"