Thread: use pointers instead of arrays

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    use pointers instead of arrays

    this code seems to work, but i have to get it to use pointers as opposed to arrays .

    Can you guys see what changes i'd have to make for this to work with pointers (ideally i'd like their to be no [] anywhere)

    Code:
    int maxarray(int s, int d[]){
    int lo = 0;
    int i = 0;
    for(; i< s;i++)
    {
    if(d[i]>d[lo])
    {lo = i;}
    }
    return (d[lo]);
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Do you understand how an array is mapped out in memory? Do you know what the []'s are actually doing? When you pass an array to a function why don't you have to specify the size in the function prototype?

    How can you mimick this behavior with pointers?

    Hint: You're actually doing the opposite here with a pointer when you write a function this way. In many cases pointer notation and array notation are interchangable as are pointers to first elements of arrays and arrays themselves. This is not always true, but is for a good number of cases.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    i have a rough idea of how an array is allocated in memory, and a clear understanding as to what the []'s actually do (they make an array). I dont have to pass in something to specify the size because it is only working with one array


    i can mimik this behaviour with pointers by adressing it to memory i guess, but how i would do that is beyond me

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    An array is allocated in contiguous memory. That means if you have an array of 5 elements, element 1 will be right before element 2.

    When you have an array and you use the []'s to select an element, you're really derefencing the address of the (address of array's first element + (the index * size of one element)).

    You can do that manually with pointers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    i read over some of my c programming book and this seems to be correct, but it doesnt work what is wrong with it?

    Code:
    int maxarray(int s, int *d){
    int lo = 0;
    int i = 0;
    for(; i< s;i++)
    {
    if(*(d+i)>*(d+lo))
    {lo = i;}
    }
    return (*(d+lo));
    }

    also i posted this before you made your message, but it appears as if what you said supports the change
    Last edited by amazonshopper; 10-29-2007 at 10:16 PM. Reason: had to update

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Indeed it does support what you did, but you missed something important. Note the formula I gave you.

    On your machine, an int is probably 32-bits, which means it spans 4 bytes. Remember that memory is also most likely mapped out in bytes. This means that if you have an array of 5 elements at address 1000, this is what it might look like:

    Address 1000 : array[0]
    Address 1004 : array[1]
    Address 1008 : array[2]
    Address 1012 : array[3]
    Address 1016 : array[4]
    The forumula to convert array[i] is (address of array's first element + (the index * size of one element)).

    This can be realized by this: (array + (i*4)).

    So why the multiplication by 4? Because each int is 4 bytes. Now this might not be true for all archutectures. So you shouldn't assume it. This is why we have the sizeof operator. It'll let you get the size of a variable in bytes.

    This means instead of 4, you can put sizeof(int) and it will return 4. To be even more portable and allow your code to work for any data type, you can write sizeof(*array). This will take the size of whatever type the array contains.

    Hope this makes sense.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Works for me
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int maxarray(int s, int d[])
    {
        int lo = 0;
        int i = 0;
        for (; i < s; i++) {
            if (d[i] > d[lo]) {
                lo = i;
            }
        }
        return (d[lo]);
    }
    
    int maxptr(int s, int *d)
    {
        int lo = 0;
        int i = 0;
        for (; i < s; i++) {
            if (*(d + i) > *(d + lo)) {
                lo = i;
            }
        }
        return (*(d + lo));
    }
    
    int main()
    {
        int test[] = { 1, 45, 23, 244, 11, 34, 2 };
        printf("%d\n", maxarray(7, test));
        printf("%d\n", maxptr(7, test));
        return 0;
    }
    
    $ gcc -W -Wall -ansi -pedantic foo.c
    $ ./a.exe
    244
    244
    > The forumula to convert array[i] is (address of array's first element + (the index * size of one element)).
    Except C already takes this into account based on the type of the pointer.
    arr[ i ] is just *( arr + i )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, you're right. I'm an idiot today....

    /me slaps self.

    Been confusing languages lol....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM