Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

c pointers notation basic question

 
 
ImpalerCore
Guest
Posts: n/a
 
      03-19-2013
On Mar 17, 10:39*pm, Keith Thompson <(E-Mail Removed)> wrote:
> 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.


The issue in my opinion is that the interpretation of '*' is different
between variable declarations and other interpretations when applying
'*' to a type.

Most people interpret '*' as a type modifier, like in contexts like
sizeof.

For example, sizeof (double) is different from sizeof (double*), where
'*' modifies the type, and is interpreted as 'double' and 'double*'
respectively.

In variable declarations, that interpretation isn't held anymore.

double *x, y;

The '*' in this situation is now a variable modifier, where 'x' is now
a pointer variable to type double, and 'y' is a normal variable of
type double. The grammar betrays the common vernacular that applying
'*' to a type modifies the type.

All in all it's not a big deal, but I still prefer 'double* x' over
'double *x' for that reason.

Best regards,
John D.
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      03-19-2013
On 3/19/2013 10:47, ImpalerCore wrote:
>
> The issue in my opinion is that the interpretation of '*' is different
> between variable declarations and other interpretations when applying
> '*' to a type.
>
> Most people interpret '*' as a type modifier, like in contexts like
> sizeof.
>
> For example, sizeof (double) is different from sizeof (double*), where
> '*' modifies the type, and is interpreted as 'double' and 'double*'
> respectively.
>


You can also do 'sizeof (double(*))'.

> In variable declarations, that interpretation isn't held anymore.
>
> double *x, y;
>
> The '*' in this situation is now a variable modifier, where 'x' is now
> a pointer variable to type double, and 'y' is a normal variable of
> type double. The grammar betrays the common vernacular that applying
> '*' to a type modifies the type.
>


You can also do 'double (* x), (y);'

> All in all it's not a big deal, but I still prefer 'double* x' over
> 'double *x' for that reason.
>


There's also 'double * x'.

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

Cheerily," -- Richard Harter
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      03-19-2013
Shao Miller於 2013年3月20日星期三UTC+8上午2時37分08秒 寫道:
> On 3/19/2013 10:47, ImpalerCore wrote:
>
> >

>
> > The issue in my opinion is that the interpretation of '*' is different

>
> > between variable declarations and other interpretations when applying

>
> > '*' to a type.

>
> >

>
> > Most people interpret '*' as a type modifier, like in contexts like

>
> > sizeof.

>
> >

>
> > For example, sizeof (double) is different from sizeof (double*), where

>
> > '*' modifies the type, and is interpreted as 'double' and 'double*'

>
> > respectively.

>
> >

>
>
>
> You can also do 'sizeof (double(*))'.
>
>
>
> > In variable declarations, that interpretation isn't held anymore.

>
> >

>
> > double *x, y;

>
> >

>
> > The '*' in this situation is now a variable modifier, where 'x' is now

>
> > a pointer variable to type double, and 'y' is a normal variable of

>
> > type double. The grammar betrays the common vernacular that applying

>
> > '*' to a type modifies the type.

>
> >

>
>
>
> You can also do 'double (* x), (y);'
>
>
>
> > All in all it's not a big deal, but I still prefer 'double* x' over

>
> > 'double *x' for that reason.

>
> >

>
>
>
> There's also 'double * x'.
>
>
>
> --
>
> - Shao Miller
>
> --
>
> "Thank you for the kind words; those are the kind of words I like to hear..
>
>
>
> Cheerily," -- Richard Harter


Nowadays a PC system with the 64 bit OS with a 2 to 4 core
cpu and a hard disc of tera bytes and giga bytes of DRAM is selling in the street under 2000 USD.

Well, the pointer part can be taught as a key to
a declared object. The pointer offers another way other than
the slow deep copy method for an object to be passed around
functions efficiently in the time critical applications.





 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      03-19-2013
On 19 Mar 2013 15:38:12 +0200, Phil Carmody
<(E-Mail Removed)> wrote:

>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


It depends a lot in which shop you're working.

AV Rule 62
The dereference operator * and the address-of operator & will be
directly connected with the type-specifier.

Rationale: The int32* p; form emphasizes type over syntax while the
int32 *p; form emphasizes syntax over type. Although both forms are
equally valid C++, the heavy emphasis on types in C++ suggests that
int32* p; is the preferable form.

Examples:
int32* p; // Correct
int32 *p; // Incorrect
int32* p, q; // Probably error. However, this declaration cannot
occur
// under the one name per declaration style required by AV Rule 152.

AV Rule 152
Multiple variable declarations shall not be allowed on the same line.

Rationale: Increases readability and prevents confusion (see also AV
Rule 62).

Example:
int32* p, q; // Probably error.
int32 first button_on_top_of_the_left_box, i; // Bad: Easy to
overlook i
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      03-19-2013
On Saturday, March 16, 2013 9:24:12 AM UTC-5, 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;


No.

> Is pc is still pointing to type char data above?


Yes.

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

It's an accident of C (and C++) syntax that you can write it either way, or
with any amount of whitespace in between:

T *p;
T* p;
T * p;

All three of the above declarations will be interpreted as though they had
been written

T (*p);

IOW, the '*' is always bound to the declarator. If you write a declaration
like

T* a, b;

only a will be declared as a pointer to T; b will be declared as a regular T.

Declarations in C (and C++) are based on the types of *expressions*, not
objects. For example, if we have a pointer to int named p, and we want to
access the value that p points to, we use the '*' operator to dereference it:

x = *p;
printf("value = %d\n", *p);

The type of the *expression* '*p' is int, so the declaration is written as

int *p;

If we had an array of pointers to int, and we want the value being pointed
to by the i'th element, we'd write

x = *ap[i];
printf("value = %d\n", *ap[i]);

The type of the *expression* '*ap[i]' is int, so the declaration is written as

int *ap[N];

In both declarations above, '*p' and '*ap[N]' are known as *declarators*; they
introduce the name of the thing being declared, along with additional type
information not provided by the type specifier. The int-ness of 'ap' is given
by the type specifier 'int', while the array-ness and pointer-ness are given by
the declarator '*ap[N]'. We *could* write the above as

int* ap[N];

but that's discouraged for a number of reasons.

Note that many C++ programmers use the 'T* p;' style, as opposed to 'T *p;',
because there are times in C++ where you really do want to emphasize the type
of the object. The problem with this style is that it only works for simple
pointers; once you start getting into arrays of pointers, or pointers to arrays,
or pointers to functions returning pointers to arrays, or arrays of pointers to
functions returning pointers to functions returning pointers to arrays of
pointers to pointers to int, etc., that style stops being useful.

As an example, suppose we have a pointer to an array of pointers to functions
returning pointers to 'int'. To get to an integer value, we have to deference
the pointer to the array, retrieve the i'th element, dereference that pointer,
call the pointed-to function, and then dereference the result:

x = *(*(*fp)[i])();
printf("value = %d\n", *(*(*fp)[i])());

Again, the type of the *expression* '*(*(*fp)[i])()' is int, so the declaration
is written as

int *(*(*fp)[N])(void);

which reads as

fp -- fp
*fp -- is a pointer
(*fp)[N] -- to an N-element array
*(*fp)[N] -- of pointers
(*(*fp)[N])( ) -- to functions
(*(*fp)[N])(void) -- taking no parameters
*(*(*fp)[N])(void) -- returning pointers
int *(*(*fp)[N])(void) -- to int

Now, if you *really* *really* *really* wanted to use the 'T* p;' convention
with something like the above, you could create some typedefs:

typedef int *ipf(void);
typedef ipf *iparr[N];

iparr* fp;

but frankly, that doesn't make anything any clearer. You won't usually types
that obnoxious in the wild, but they do crop up occasionally.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      03-19-2013
On 03/19/2013 06:38 PM, John Bode wrote:
> On Saturday, March 16, 2013 9:24:12 AM UTC-5, bpascal123 wrote:

....
>> char *pc;
>> pc points to a type char data
>>
>> Is there any difference with this notation?
>> char* pc;

....
> It's an accident of C (and C++) syntax that you can write it either way, or
> with any amount of whitespace in between:


It's not really an accident. C declarations were deliberately designed
to mirror the way the declared identifier could be used. "* p" and
" *p" are equally valid expressions, with precisely the same meaning,
resulting in a value of type T, so "T* p" and "T *p" should be equally
valid ways of declaring p.
NB: not all of the declarational freedom that could be inferred by
abusing this argument actually exists in C declarations.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-20-2013
88888 Dihedral wrote:
>
> Nowadays a PC system with the 64 bit OS with a 2 to 4 core
> cpu and a hard disc of tera bytes and giga bytes of DRAM is selling in the street under 2000 USD.


All that hardware and you still can't quote correctly.

--
Ian Collins
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      03-20-2013
(E-Mail Removed)於 2013年3月20日星期三UTC+8下午2時54分54秒 寫道:
> On Tue, 19 Mar 2013 13:27:19 -0700 (PDT), 88888 Dihedral
>
> <(E-Mail Removed)> wrote:
>
>
>
> >Shao Miller? 2013?3?20????UTC+8??2?37?08????

>
> >> On 3/19/2013 10:47, ImpalerCore wrote:

>
> >>

>
> >> >

>
> >>

>
> >> > The issue in my opinion is that the interpretation of '*' is different

>
> >>

>
> >> > between variable declarations and other interpretations when applying

>
> >>

>
> >> > '*' to a type.

>
> >>

>
> >> >

>
> >>

>
> >> > Most people interpret '*' as a type modifier, like in contexts like

>
> >>

>
> >> > sizeof.

>
> >>

>
> >> >

>
> >>

>
> >> > For example, sizeof (double) is different from sizeof (double*), where

>
> >>

>
> >> > '*' modifies the type, and is interpreted as 'double' and 'double*'

>
> >>

>
> >> > respectively.

>
> >>

>
> >> >

>
> >>

>
> >>

>
> >>

>
> >> You can also do 'sizeof (double(*))'.

>
> >>

>
> >>

>
> >>

>
> >> > In variable declarations, that interpretation isn't held anymore.

>
> >>

>
> >> >

>
> >>

>
> >> > double *x, y;

>
> >>

>
> >> >

>
> >>

>
> >> > The '*' in this situation is now a variable modifier, where 'x' is now

>
> >>

>
> >> > a pointer variable to type double, and 'y' is a normal variable of

>
> >>

>
> >> > type double. The grammar betrays the common vernacular that applying

>
> >>

>
> >> > '*' to a type modifies the type.

>
> >>

>
> >> >

>
> >>

>
> >>

>
> >>

>
> >> You can also do 'double (* x), (y);'

>
> >>

>
> >>

>
> >>

>
> >> > All in all it's not a big deal, but I still prefer 'double* x' over

>
> >>

>
> >> > 'double *x' for that reason.

>
> >>

>
> >> >

>
> >>

>
> >>

>
> >>

>
> >> There's also 'double * x'.

>
> >>

>
> >>

>
> >>

>
> >> --

>
> >>

>
> >> - Shao Miller

>
> >>

>
> >> --

>
> >>

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

>
> >>

>
> >>

>
> >>

>
> >> Cheerily," -- Richard Harter

>
> >

>
> >Nowadays a PC system with the 64 bit OS with a 2 to 4 core

>
> >cpu and a hard disc of tera bytes and giga bytes of DRAM is selling in the street under 2000 USD.

>
>
>
>
>
> You really need to find a better place to buy PCs. You can get a
>
> brand name system with those specs for $500 (including a Windows
>
> license), a white-box shop ought to be able to do one for $300 with
>
> Linux.


As an engineer I prefer just quoting the upper bound of
my budgets.

It is cheap to write programs plus a dedicated machine
to those professionals with high hourly wages to save their time.



 
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