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; Originally Posted by ymc1g11 Andreas, Here's my code: Code: int * mymalloc( int *array, int size){ int i = 0 ...

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    1,111
    Quote Originally Posted by ymc1g11 View Post
    Andreas,

    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;
    but apparently it doesn't work.
    Suppose array is declared as int array[200]. And then you call your function like this:

    mymalloc(array, 200);

    Then the line you just have above will try to do this:

    if(array[i+200] != 0) return (int *)0;

    Which is clearly wrong and will produce undefined behaviour. What are you trying to do with that line?

  2. #17
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    c99tutorial,

    that is to check size+indexwherecurrentelementiszero does not go out of bound of array length.

    and 'size' is not the size of 'array', it is the size of pointer to the array where i wanted to return, if it is possible.

  3. #18
    Registered User
    Join Date
    Nov 2012
    Posts
    1,111
    Quote Originally Posted by ymc1g11 View Post
    that is to check size+indexwherecurrentelementiszero does not go out of bound of array length.

    and 'size' is not the size of 'array', it is the size of pointer to the array where i wanted to return, if it is possible.
    To check whether it goes out of the bounds, then your function needs to know the bounds of the array. Otherwise it is undefined behaviour. So I think you should work on a function that has these 4 pieces of information:

    1. a pointer to the "input array" (array)
    2. the size of the input array (20)
    3. a pointer to the "output array" (outArray)
    4. the size of the output array (e.g. 12)

    If you give your function these 4 items, then it should use the first 2 to produce the second two. Look at the earlier example I posted and adjust it as you need for your application.

  4. #19
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    As i understood your question " array of 20 and u want to return 4 to 12 index values."
    So 1st thing is to return address of 4th element and the no of elements u need to read from there in this case it is12-4)=8. So for 8 we can use the 'pass by address' function calling--
    Code:
    #define ARRAYSIZE 20
    int main()
    {
    int *ptr,array[ARRAYSIZE],readno=0;
    ptr=function(array,ARRAYSIZE,&readno);
    return 0;
    }
    int* function(int* array , int oldsize,int* readno)//u can make oldsize(20) as global variable also and read no is 8 which is initially during calling u can use 0.
    {
         int j=4;
         *readno=8;//which can be accessed from main(the function fron u calling function() without returning;
         return array+j;
    }
    I think this will help u if still there is problem then plz elaborate ur question with an example it took me alot of time to understand your question...
    What is life??

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ymc1g11 View Post
    and 'size' is not the size of 'array', it is the size of pointer to the array where i wanted to return, if it is possible.
    A size of a pointer is always a constant value depending on your system. As you have been already told in another thread, you can't find out the size of an array if you just have a pointer to one element of the array.

    What I still don't understand is, how you determine the end of your subarray. Is it a constant value?

    Bye, Andreas

  6. #21
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,392
    "pointer" is not an array -> it is a pointer.

    Also, you can not do the following - It's like saying 4 = something
    Code:
    int *pointer;
    int j;
    
    for(j=0;j<size;j++)
      &pointer[j] = &array[j+size];
    What I suggest you do is make a function that returns a pointer to an array - It is a bit tricky to get your head around the declaration, so I'll leave you a link to a tutorial that has helped a lot of people - Also, I have knocked together a little function which demonstrates what I've suggested.

    http://untroubled.org/articles/cdecls.txt

    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    
    
    int (*get_subarr ( int in[10] ))[4];
    
    
    int main(void)
    {
        int grape[10] = {1,2,3,4,5,6,7,8,9,0};
        int (*carrot_ptr)[4];
        int i;
    
    
        carrot_ptr = get_subarr(grape);
    
    
        for(i=0;i<4;i++)
        {
            printf ("%d ", (*carrot_ptr)[i]);
        }
    
    
        return EXIT_SUCCESS;
    
    
    }
    
    
    int (*get_subarr(int banana[10]))[4]
    {
        return (int (*)[4]) &banana[4];
    }
    Last edited by Click_here; 11-11-2012 at 05:43 PM.
    Fact - Beethoven wrote his first symphony in C

  7. #22
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,392
    I kept going with my example, and this is what I came up with - I'm sure that you can get something out of this

    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    
    
    #define BUFF 10
    
    
    /****************************************************************
     * Function get_subarr (get sub array)
     *
     * Returns a pointer to an array (located in a larger array)
     *
     * param m_buff (main buffer) - The buffer that the sub array will be
     *  allocated from.
     *
     * param m_buf_size (main buffer size) - The size of the m_buff array
     *
     * param idx (index)
     * This is where the function will try to
     * allocate the sub array
     *
     * param s_arr_size (sub array size)
     * This is the size of the sub array
     ****************************************************************/
    int (*get_subarr (int m_buf[], size_t m_buf_size, int idx, size_t s_arr_size) )[];
    
    
    int main(void)
    {
        int main_buffer[BUFF] = {0,1,2,3,4,5,6,7,8,9};
    
    
        int (*sub_buffer_ptr)[5] = NULL;
        int i;
    
    
        int sub_array_size = 4;  //Play around with these two values...
        int sub_array_index = 4; //
    
    
    
    
        sub_buffer_ptr = get_subarr(main_buffer, BUFF, sub_array_index, sub_array_size);
        if (sub_buffer_ptr == NULL)
        {
            fputs("Error in get_subarr", stderr);
            return EXIT_FAILURE;
        }
    
    
        for(i=0;i<sub_array_size;i++)
        {
            printf ("%d ", (*sub_buffer_ptr)[i]);
        }
    
    
        return EXIT_SUCCESS;
    
    
    }
    
    
    int (*get_subarr (int m_buf[], size_t m_buf_size, int idx, size_t s_arr_size) )[]
    {
        if ((s_arr_size+idx) > m_buf_size)
        {
            return NULL;
        }
        else
        {
            return (int (*)[]) &m_buf[idx];
        }
    }
    Last edited by Click_here; 11-11-2012 at 08:28 PM. Reason: Got rid of food naming convention
    Fact - Beethoven wrote his first symphony in C

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

Similar Threads

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

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