newbie question: difference between (*ptr) and *ptr

Discussion in 'C Programming' started by hans christian, Jun 11, 2006.

  1. Hello everyone,

    Could someone explain to me what the difference is between
    (*ptr)

    and

    *ptr

    I am a bit confused, since I am just starting to program in C.

    Thank you for any clear answers !

    HHcrist.
     
    hans christian, Jun 11, 2006
    #1
    1. Advertising

  2. hans christian

    Ben Pfaff Guest

    hans christian <> writes:

    > Could someone explain to me what the difference is between
    > (*ptr)
    >
    > and
    >
    > *ptr


    There is no difference in isolation. Perhaps you should explain
    your situation.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Jun 11, 2006
    #2
    1. Advertising

  3. On Sat, 10 Jun 2006 16:29:47 -0700, Ben Pfaff wrote:

    > hans christian <> writes:
    >
    >> Could someone explain to me what the difference is between
    >> (*ptr)
    >>
    >> and
    >>
    >> *ptr

    >
    > There is no difference in isolation. Perhaps you should explain
    > your situation.


    My situation is that I am reading Numerical recipes in C, and in the misc.
    functions they mix the previous given notation. That confuses me, because
    I do not know what the difference might be, or can discover any coherent
    usage of the two different notations.

    For example some arbitrary snippets from the same function:

    *adev = (*var) = (*skew) = (*curt) = 0.0;
    *var += (p = s * s);
    *skew /= (n * (*var) * (*sdev));

    In the first (*skew) is used, but in the last line they use the other form
    *skew ?

    THank you for the quick reply.

    HHcrist
     
    hans christian, Jun 11, 2006
    #3
  4. hans christian

    Ben Pfaff Guest

    hans christian <> writes:

    >> hans christian <> writes:
    >>
    >>> Could someone explain to me what the difference is between
    >>> (*ptr)
    >>>
    >>> and
    >>>
    >>> *ptr


    [...]

    > *adev = (*var) = (*skew) = (*curt) = 0.0;


    No difference between the two forms here.

    > *var += (p = s * s);


    Here the parentheses serve the valuable purpose of making it
    clear what's going on. It would be even better written as
    p = s * s;
    *var += p;

    > *skew /= (n * (*var) * (*sdev));


    No purpose, except a subjective difference in readability.
    --
    "You call this a *C* question? What the hell are you smoking?" --Kaz
     
    Ben Pfaff, Jun 11, 2006
    #4
  5. hans christian

    Guest

    hans christian <> wrote:
    > On Sat, 10 Jun 2006 16:29:47 -0700, Ben Pfaff wrote:
    >
    > > hans christian <> writes:
    > >
    > >> Could someone explain to me what the difference is between
    > >> (*ptr)
    > >>
    > >> and
    > >>
    > >> *ptr

    > >
    > > There is no difference in isolation. Perhaps you should explain
    > > your situation.

    >
    > My situation is that I am reading Numerical recipes in C, and in the
    > misc. functions they mix the previous given notation. That confuses me,
    > because I do not know what the difference might be, or can discover any
    > coherent usage of the two different notations.
    >
    > For example some arbitrary snippets from the same function:
    >
    > *adev = (*var) = (*skew) = (*curt) = 0.0;


    Although C doesn't care, I like the paranthesis for readability.
    It would take me a few seconds to realize they are not multiplications.

    > *var += (p = s * s);
    > *skew /= (n * (*var) * (*sdev));
    >
    > In the first (*skew) is used, but in the last line they use the other
    > form *skew ?


    In the least line, it takes little mental effort to realize it is not
    a multiplication.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 11, 2006
    #5
  6. Re: newbie question: difference between (*ptr) and *ptr thnkx

    On Sun, 11 Jun 2006 00:42:51 +0000, xhoster wrote:

    > hans christian <> wrote:
    >> On Sat, 10 Jun 2006 16:29:47 -0700, Ben Pfaff wrote:
    >>
    >> > hans christian <> writes:
    >> >
    >> >> Could someone explain to me what the difference is between
    >> >> (*ptr)
    >> >>
    >> >> and
    >> >>
    >> >> *ptr
    >> >
    >> > There is no difference in isolation. Perhaps you should explain
    >> > your situation.

    >>
    >> My situation is that I am reading Numerical recipes in C, and in the
    >> misc. functions they mix the previous given notation. That confuses me,
    >> because I do not know what the difference might be, or can discover any
    >> coherent usage of the two different notations.
    >>
    >> For example some arbitrary snippets from the same function:
    >>
    >> *adev = (*var) = (*skew) = (*curt) = 0.0;

    >
    > Although C doesn't care, I like the paranthesis for readability.
    > It would take me a few seconds to realize they are not multiplications.
    >
    >> *var += (p = s * s);
    >> *skew /= (n * (*var) * (*sdev));
    >>
    >> In the first (*skew) is used, but in the last line they use the other
    >> form *skew ?

    >
    > In the least line, it takes little mental effort to realize it is not
    > a multiplication.
    >
    > Xho


    Ok super... ! Thank you for the answers. I'm a bit more secure in my
    understanding now.. thankx..

    HHcrist
     
    hans christian, Jun 11, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,248
  2. Sid
    Replies:
    5
    Views:
    1,080
  3. Jason

    difference between *ptr++ and ++*ptr ?

    Jason, May 15, 2005, in forum: C Programming
    Replies:
    19
    Views:
    6,552
    Chris Torek
    May 19, 2005
  4. AY
    Replies:
    4
    Views:
    2,200
  5. Nick Keighley
    Replies:
    0
    Views:
    513
    Nick Keighley
    Jul 28, 2009
Loading...

Share This Page