Re: Pointer Notation

Discussion in 'C Programming' started by Malcolm, Jul 7, 2003.

  1. Malcolm

    Malcolm Guest

    <> wrote in message
    >
    > Is the program below using what is referred to as pointer notation?
    >

    Sort of. Pointers are the defining feature of C, and are used in two main
    places.

    1) A function needs to return more than one value. The return is passed in
    as a pointer.
    eg
    void getcursorpos(int *x, int *y)
    {
    *x = globalxpos;
    *y = globalypos;
    }

    2) You are passing an array of values.

    eg
    double mean(double *val, int N)
    {
    int i;
    double answer = 0;

    for(i=0;i<N;i++)
    answer += val;

    return answer/N;
    }

    You can also write the function

    double mean(double *val, int N)
    {
    double answer = 0;
    int i;

    for(i=0;<N;i++)
    answer += *val++;

    return answer/N;
    }

    This way we are stepping through the array by incrementing the pointer
    rather than increasing an index - on some old platforms this led to more
    efficient code.

    What is confusing is that if you know how many values you ahve in advance,
    you can use array notation.

    double mean(double val[123])

    personally I dislike this since C handles val internally as a pointer, and
    it is terribly confusing to newbies.
    Malcolm, Jul 7, 2003
    #1
    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. Angela
    Replies:
    3
    Views:
    349
    Artie Gold
    Oct 27, 2003
  2. Jonathan Mcdougall
    Replies:
    4
    Views:
    525
    jeffc
    Aug 19, 2004
  3. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,271
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  4. Tameem
    Replies:
    454
    Views:
    11,571
  5. Robert Mark Bram

    Dot notation V Bracket notation

    Robert Mark Bram, Jul 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    456
    Robert Mark Bram
    Jul 5, 2003
Loading...

Share This Page