Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   c pointers notation basic question (http://www.velocityreviews.com/forums/t958721-c-pointers-notation-basic-question.html)

bpascal123 03-16-2013 02:24 PM

c pointers notation basic question
 
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 03-16-2013 02:35 PM

Re: c pointers notation basic question
 
In article <651da82e-4e32-4368-a4cd-077b1b1d0b54@googlegroups.com>,
bpascal123 <bpascal123@gmail.com> 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 03-16-2013 02:38 PM

Re: c pointers notation basic question
 
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

[ Please reply to the group,
return address is invalid ]

bpascal123 03-16-2013 02:45 PM

Re: c pointers notation basic question
 
thanks

Eric Sosman 03-16-2013 02:54 PM

Re: c pointers notation basic question
 
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
esosman@comcast-dot-net.invalid

Shao Miller 03-16-2013 03:02 PM

Re: c pointers notation basic question
 
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 03-16-2013 05:55 PM

Re: c pointers notation basic question
 
In article <mstorkamp-95B81A.09352616032013@news.eternal-september.org>,
Mark Storkamp <mstorkamp@yahoo.com> 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, falk@despams.r.us.com
http://thespamdiaries.blogspot.com/

Shao Miller 03-16-2013 08:18 PM

Re: c pointers notation basic question
 
On 3/16/2013 13:55, Edward A. Falk wrote:
> In article <mstorkamp-95B81A.09352616032013@news.eternal-september.org>,
> Mark Storkamp <mstorkamp@yahoo.com> 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 03-16-2013 08:56 PM

Re: c pointers notation basic question
 
Edward A. Falk wrote:
> In article <mstorkamp-95B81A.09352616032013@news.eternal-september.org>,
> Mark Storkamp <mstorkamp@yahoo.com> 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 03-16-2013 10:55 PM

Re: c pointers notation basic question
 
Roberto Waltman <usenet@rwaltman.com> 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) kst-u@mib.org <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"


All times are GMT. The time now is 11:18 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.