Thread: Is this code snippet utilizing a pointer?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Is this code snippet utilizing a pointer?

    Code:
        while (array[i] > max && i < size)
        {
            max = array[i];
            i++;
        }
    The code finds the largest value of an array. I'm asking because for this particular assessment, pointers should be used exclusively to access array elements. For the code above, I'm not sure how to access the elements any other way.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As previously pointed out, a[i] is equivalent to *(a+i). (i.e. the value contained at the address pointed to by a+i which is a pointer to the first element of the array a[0] incremented by the appropriate offset).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 843
    I'm asking because for this particular assessment, pointers should be used exclusively to access array elements. For the code above, I'm not sure how to access the elements any other way.
    I interpret this as an instruction to use a pointer instead of an index to iterate over the elements of the array. For example, given this:
    Code:
    int array[100];
    const size_t size = sizeof(array) / sizeof(array[0]);
    You might normally write:
    Code:
    size_t i;
    for (i = 0; i < size; ++i)
    {
        /* access array[i] */
    }
    But now you would write:
    Code:
    int *i;
    int *end;
    for (i = array, end = array + size; i != end; ++i)
    {
        /* access *i */
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  2. passing argument from incompatible pointer type
    By bhdavis1978 in forum C Programming
    Replies: 5
    Last Post: 03-17-2010, 12:42 PM
  3. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  4. why the code can compile -- about function pointer
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 03:25 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM