# How to return pointer of a sub array?

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 11-11-2012
c99tutorial
Quote:

Originally Posted by ymc1g11
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?
• 11-11-2012
ymc1g11
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.
• 11-11-2012
c99tutorial
Quote:

Originally Posted by ymc1g11
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.
• 11-11-2012
dojha00
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 is:(12-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...
• 11-11-2012
AndiPersti
Quote:

Originally Posted by ymc1g11
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
• 11-11-2012
Click_here
"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]; }```
• 11-11-2012
Click_here
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];     } }```
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12