Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > c pointers notation basic question

Reply
Thread Tools

c pointers notation basic question

 
 
Eric Sosman
Guest
Posts: n/a
 
      03-17-2013
On 3/16/2013 10:29 PM, Richard Damon wrote:
> On 3/16/13 8:34 PM, Eric Sosman wrote:
>>
>> 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?
>>

>
> If you coding style says that the first should really be:
>
> double sumx; // The Sum of the X values
> double sumy; // The Sum of the Y values
> double sumxx; // The Sum of the square of the X values
> double sumyy; // The Sum of the square of the Y values
> double sumxy; // The Sum of the product of the X and Y values
>
> Then also allowing only one variable per line isn't as big of a limitation.


Many years ago -- many decades ago, in fact -- I had a colleague
who wrote his code in a style that minimized keypunching time. (See
"many decades," above.) One space, not several, separated his
assembler opcodes from the operands, and from the label if there
was one. Registers were referred to by number, not name, and of
course comments were not to be found anywhere:

L 0,X
L 1,Y
CR 0,1
BNL STORE
LR 0,1
STORE ST 0,MAX

The Management was not happy. "Use register names, not numbers.
Line up the columns. And write comments, dammit, comments!" So
he wrote himself a neatener program that took in his usual source
code and spat out

L R0,X LOAD R0 FROM X
L R1,Y LOAD R1 FROM Y
CR R0,R1 COMPARE R0 TO R1
BNL STORE BRANCH TO STORE IF NOT LOW
LR R0,R1 LOAD R0 FROM R1
STORE ST R0,MAX STORE R0 TO MAX

Dunno why, but something about the added value of the comments
you exhibit reminds me of my long-ago colleague's exploit.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      03-17-2013
On 3/16/2013 19:37, Keith Thompson wrote:
> Ian Collins <(E-Mail Removed)> writes:
>> 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.
>>
>>

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


Where whitespace causes the mind to instinctively partition the latter
into two pieces and where 'char*' isn't a type-specifier. But in the
former, we have:

char *p;
^^^^ ^^-- [init-declarator-list_opt], [init-declarator],
| [declarator], [pointer_opt direct-declarator]
+- [declaration-specifiers],
[type-specifier declaration-specifiers_opt]

But one more space could "split up" [pointer_opt direct-declarator].

char * p;
^^^^ ^ ^

I usually see the argument that this is undesirable because it's not
consistent with a convention of applying the indirection operator
without any whitespace. (Although we know that it's not that operator
in this particular declaration context.)

*p + 1; /* A tight fit */
* p + 1; /* What?! */

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


if (foo)
do_this();
else
if (bar)
do_that();
else
if (baz)
do_thother();
else
help();

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

Cheerily," -- Richard Harter
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      03-17-2013
On 3/16/2013 20:34, Eric Sosman wrote:
> On 3/16/2013 6:55 PM, Keith Thompson wrote:
>> 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;

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


double
sumx,
sumy,
sumxx,
/* Ooh, this next one's a good one */
sumyy,
sumxy,
* summingelse,
/* Could probably do without this */
* summore,
** sumtimetoday;

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

Cheerily," -- Richard Harter
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      03-17-2013
On Sunday, 17 March 2013 05:17:25 UTC+2, Shao Miller wrote:
> On 3/16/2013 21:55, Öö Tiib wrote:
> > 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.


On such case I would likely use CLang to translate the code from C99 to C89
immediately prior to compiling. Are those 20 years old C compilers used a
lot in practice?
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      03-17-2013
On 3/17/2013 00:23, Öö Tiib wrote:
> On Sunday, 17 March 2013 05:17:25 UTC+2, Shao Miller wrote:
>> On 3/16/2013 21:55, Öö Tiib wrote:
>>> 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.

>
> On such case I would likely use CLang to translate the code from C99 to C89
> immediately prior to compiling. Are those 20 years old C compilers used a
> lot in practice?
>


It's still in use for Windows compilers, and possibly will be for as
long as there is a Windows C compiler. Someone at Microsoft explained
this to me.

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

Cheerily," -- Richard Harter
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      03-17-2013
On 16-Mar-13 18:44, Ian Collins wrote:
> Stephen Sprunk wrote:
>> On 16-Mar-13 17:55, Keith Thompson wrote:
>>> 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;

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


Some coding styles require declaring all variables at the top of a
block. This is mandatory if your code must work with MSVC, which is
permanently stuck at C89.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      03-17-2013
On 16-Mar-13 18:50, Keith Thompson wrote:
> Stephen Sprunk <(E-Mail Removed)> writes:
>> "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;"


Really? Bizarre. If he thought that was better, he should have changed
the syntax and semantics so that actually worked in practice.

Yes, that would cause a gratuitous difference between C and C++, but
there are plenty of those already anyway.

> (and avoiding multiple declarations per line),


An annoying kludge that is probably a direct result of the above
problem, which he could have fixed if he wanted to.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-17-2013
Shao Miller <(E-Mail Removed)> writes:
> On 3/16/2013 19:37, Keith Thompson wrote:

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

>
> if (foo)
> do_this();
> else
> if (bar)
> do_that();
> else
> if (baz)
> do_thother();
> else
> help();


If I consistently followed the rule of having layout reflect syntax, I'd
write the above as:

if (foo)
do_this();
else
if (bar)
do_that();
else
if (baz)
do_thother();
else
help();

or, with braces:

if (foo) {
do_this();
}
else {
if (bar) {
do_that();
}
else {
if (baz) {
do_thother();
}
else {
help();
}
}
}

Instead, I pretend that "else if" is part of the grammar rather
than just one case of the other rules, and I write, as most C
programmers do:

if (foo) {
do_this();
}
else if (bar) {
do_that();
}
else if (baz)
do_thother();
}
else {
help();
}

(Some languages make "elseif", "elsif", or "elif" a keyword for just
this reason).

--
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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-18-2013
Stephen Sprunk <(E-Mail Removed)> writes:
> On 16-Mar-13 18:50, Keith Thompson wrote:
>> Stephen Sprunk <(E-Mail Removed)> writes:
>>> "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;"

>
> Really? Bizarre. If he thought that was better, he should have changed
> the syntax and semantics so that actually worked in practice.


It actually does work in practice.

char* ptr;

defines ptr as a pointer to char in both C and C++.

> Yes, that would cause a gratuitous difference between C and C++, but
> there are plenty of those already anyway.
>
>> (and avoiding multiple declarations per line),

>
> An annoying kludge that is probably a direct result of the above
> problem, which he could have fixed if he wanted to.


A reasonable stylistic preference, IMHO.

I don't agree with Stroustrup on this point, but I haven't found his
recommendations to cause any real problems, as long as you're aware of
the language rules.

--
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"
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      03-19-2013
Keith Thompson <(E-Mail Removed)> writes:
> 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):


Only by wusses.

Anyone who submits a patch containing:

+ int i;
+ int j;
+ int k;

to me is going to get a NACK and a patronising comment to assist them
in remembering not to do the same thing again. And you all wonder why
Greg KH doesn't want your code, eh?

Phil
--
"In a world of magnets and miracles"
-- Insane Clown Posse, Miracles, 2009. Much derided.
"Magnets, how do they work"
-- Pink Floyd, High Hopes, 1994. Lauded as lyrical geniuses.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert Infix notation to postfix notation Tameem C Programming 454 01-31-2014 06:01 PM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Hungarian Notation Vs. Pascal Notation? Grey Squirrel ASP .Net 6 03-21-2007 09:42 AM
pointers to pointers question Chad C Programming 5 03-27-2006 07:44 AM
Dot notation V Bracket notation Robert Mark Bram Javascript 3 07-05-2003 03:59 AM



Advertisments