Like Tree2Likes

How to return pointer of a sub array?

This is a discussion on How to return pointer of a sub array? within the C Programming forums, part of the General Programming Boards category; Hey all~ I am writing a function which takes an array as parameter and returns the pointer to the part ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18

    How to return pointer of a sub array?

    Hey all~

    I am writing a function which takes an array as parameter and returns the pointer to the part of the array which had not yet been written with negative values. In the beginning I had initialised the entire array with a positive value. So now my function looks like this but apparently it doesn't work:

    Code:
    int * myfunction(int *array, int size){ int i = 0;
    	while(array[i] < 0) i++;
    	int *pointer = array[i];
    	return pointer;
    }
    any help to make this work?

    Thanks in advance~

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You want the pointer to be set at the address of the proper element.That is
    Code:
    &array[i]
    ymc1g11 likes this.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    Hi SAMARAS,

    is it possible to pass an explicit length of the array? for example the memory location from array[i] to array[i+size]?

    Thanks in advance~!

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes, but my guess is that you want to know the length of the array after the negative values.
    So i suggest you to modify the function to compute the length of the array that your pointer points to
    Code:
    int * myfunction(int *array, int size ,int *sizeNew){ int i = 0;
    	while(array[i] < 0) i++;
    	int *pointer = array[i];
            
             /* Compute the new size */
             *newSize = size - i;
    
    	return pointer;
    }
    If you do need the old size, you can do it with no extra parameter.Notice that we pass the third parameter by reference, so that any change in its value to be preserved after the function terminates.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    SAMARAS,

    well actually i wasn't trying to find out the length of the array after the negative value.

    i is actually the position where i want the pointer to point to.

    By returning pointer, when assign some pointer with the return value of this function (so let the 'some pointer' be 'ptr'), I wanted to add element with:
    Code:
    ptr[0] = (an int);
    ptr[1] = (an int);
    .
    .
    .
    and ptr[0] in this case would be array[i].

    The problem is how can i bound the length of pointer (which may not reach the end of 'array'), so that later at some point i can use 'ptr' to compare with 'array' and change the value of that chunk of memory?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Oh sorry that i misunderstood :/

    It is the programmers responsibility to take care of the pointer not to go out of bounds of the array.I mean it is a pointer, so it is valid to point anywhere in the memory that belongs to your program.

    A simple solution would be to use the code that i wrote to you when i did not understand the question and use this newSize ,so that you check if the ptr[i] you write has an i that is smaller than newSize.This will keep your code safe for going out of borders of the array.

    Did this help you or i did not understand again? :/

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ymc1g11 View Post
    The problem is how can i bound the length of pointer (which may not reach the end of 'array'), so that later at some point i can use 'ptr' to compare with 'array' and change the value of that chunk of memory?
    You know the end (last element) of your array, don't you?

    Thus
    Code:
    array <= ptr <= &array[index_last_element]
    // or
    &array[0] <= &ptr[i] <= &array[index_last_element]
    should always be true, if you want to stay inside your array bounds.

    Bye, Andreas
    ymc1g11 likes this.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    Hi Andreas,

    I think the solution you provided requires me to use malloc for ptr first, which I should'nt be doing (I should have said this sorry).

    Anyway, I got it working by using *pointer[], assign pointer[i] with &array[i] and return pointer, and that seems fine.

    Thank you very much for helping!

  9. #9
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    Quote Originally Posted by AndiPersti View Post
    You know the end (last element) of your array, don't you?

    Thus
    Code:
    array <= ptr <= &array[index_last_element]
    // or
    &array[0] <= &ptr[i] <= &array[index_last_element]
    should always be true, if you want to stay inside your array bounds.

    Bye, Andreas
    Hi,

    found my solution not working again :/ so im trying to do it in ur way.

    so i wrote my function as follow:
    Code:
    int *pointer;
    int j;
    for(j=0;j<size;j++) &pointer[j] = &array[j+size];
    return pointer;
    however it gave me error of "Lvalue required as left operand of assignment"...

    any help? :S

    Much thanks

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by ymc1g11 View Post
    so i wrote my function as follow:
    Code:
    int *pointer;
    int j;
    for(j=0;j<size;j++) &pointer[j] = &array[j+size];
    return pointer;
    however it gave me error of "Lvalue required as left operand of assignment"...
    To fix the compilation error I think you want

    pointer = &array[j+size];

    pointer has type (int *) and taking the address of an int array yields the type (int *). However this index is beyond the bounds of your array if j>0.

    Why not do it like this

    Code:
    // return pointer to first nonnegative value in array, or NULL if not found
    int *myfunction(int *array, int size) 
    {
        for (int i=0; i < size; i++)
            if (array[i] >= 0)
                return &array[i]; // pointer to first nonnegative value
        return NULL; // no nonnegative values found
    }

  11. #11
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    Quote Originally Posted by c99tutorial View Post
    To fix the compilation error I think you want

    pointer = &array[j+size];

    pointer has type (int *) and taking the address of an int array yields the type (int *). However this index is beyond the bounds of your array if j>0.

    Why not do it like this

    Code:
    // return pointer to first nonnegative value in array, or NULL if not found
    int *myfunction(int *array, int size) 
    {
        for (int i=0; i < size; i++)
            if (array[i] >= 0)
                return &array[i]; // pointer to first nonnegative value
        return NULL; // no nonnegative values found
    }
    Hi c99tutorial,

    Yea that was what I had been doing.

    but what i wanted is: say i have an array of length 20 passed into my function. but then I wanted my function to return the location of, say, from index 4 to 12.

    Is it even possible to do it without passing one more argument?

    Thanks

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ymc1g11 View Post
    so i wrote my function as follow:
    Code:
    int *pointer;
    int j;
    for(j=0;j<size;j++) &pointer[j] = &array[j+size];
    return pointer;
    however it gave me error of "Lvalue required as left operand of assignment"...
    You can't use the &-operator on the left side of an assignment.

    I'm not sure what you want to achieve with this code.

    As I undestand it the first part of your problem was to find the pointer to the first element of the array which is not negative. This part is solved, isn't it?

    Now I think you want to further process that subarray (from the pointer you've got up to the end of the array). So assuming that "start_subarray" is the pointer to the first non-negative element (the pointer you've got from your function), "test_array" is the original array and "size" the number of elements of this array, then this code-snippet
    Code:
    printf("Non-negative elements: ");
    while (start_subarray != test_array + size)
    {
        printf("%d ", *start_subarray);
        start_subarray++;
    }
    will print all non-negative elements up to the end.

    If that is not what you want, then please describe again what you want to achieve and post also the complete code you've tried.

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ymc1g11 View Post
    but what i wanted is: say i have an array of length 20 passed into my function. but then I wanted my function to return the location of, say, from index 4 to 12.

    Is it even possible to do it without passing one more argument?
    So you want to find the start and the end of the subarray. One solution would be to return a struct containing both pointers.

    How is the end point defined? Shouldn't this be another function?

    Bye, Andreas

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by ymc1g11 View Post
    say i have an array of length 20 passed into my function. but then I wanted my function to return the location of, say, from index 4 to 12.

    Is it even possible to do it without passing one more argument?
    An array is only fully specified if it has a starting point and a length. So if you change the length you need to provide it back to the caller. Revise your function to also return the new size>

    Code:
    void myfunction(int *array, int size, int *outArray, int *outSize) 
    {
        for (int i=0; i < size; i++)
            if (array[i] >= 0) {
                outArray = &array[i];
                *outSize = size-i;
                return;
            }
        outArray = NULL;
        *outSize = 0;
        return;
    }
    Test case

    Code:
    #define NELEMS(x) (sizeof (x) / sizeof (x[0]))
    int main()
    {
        int oldData[] = {-1, -1, -1, -1, -1, 3, 2, 3, 4, 5, 6, 7, 8, 2};
        
        int newData[NELEMS(oldData)];
        int newSize;
        myfunction(oldData, NELEMS(oldData), newData, &newSize);
        
        printf("Old data starts at %p and is %d elements long\n",
            oldData, NELEMS(oldData));
        printf("New data starts at %p and is %d elements long\n",
            newData, newSize);
        return 0;
    }

  15. #15
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    Andreas,

    No, what I want is the pointer. Here's my code:

    Code:
    int * mymalloc(int *array, int size){
        int i = 0;
        /*array is initialised with 0's
              a non zero means the array at that index is occupied
              i looks for index where it is not occupied
            */
        while(array[i] != 0) i++;
    
    
        /*to check if it will go out of bound of array
              might not be the right way of checking
              but need not to worry for now :P */
        if(array[i+size] != 0) return (int *)0;
    
    
    else{
                    /* the following code intends to make pointer of size 'size'
                       to point to array from index i to i+size
                       and then return pointer (of size 'size') */ 
            int *pointer;
            int j;
            for(j=0;j<size;j++)
                &pointer[j] = &array[j+size];
    return pointer;
        }
    }
    but apparently it doesn't work.

    the pointer return by this function is used later on to write values into 'array'.

    the reason that i need to 'bound' pointer is i need to erase the content of 'array' where pointer of this function is pointing to, say if array is of length 20, and 'pointer' points to element from index 4 to 12, i will then erase the content of 'array' from index 4 to 12.

    Any help?

    Thanks
    Last edited by ymc1g11; 11-11-2012 at 11:26 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 11-23-2011, 12:29 PM
  2. how to return address of a pointer?
    By spotty in forum C Programming
    Replies: 1
    Last Post: 02-11-2010, 08:11 PM
  3. Replies: 2
    Last Post: 06-02-2009, 04:07 AM
  4. Return pointer to 2d array
    By albert3721 in forum C Programming
    Replies: 3
    Last Post: 10-22-2007, 05:30 AM
  5. Replies: 1
    Last Post: 07-04-2007, 01:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21