# How to return pointer of a sub array?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-10-2012
ymc1g11
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?

• 11-10-2012
std10093
You want the pointer to be set at the address of the proper element.That is
Code:

`&array[i]`
• 11-10-2012
ymc1g11
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]?

• 11-10-2012
std10093
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.
• 11-10-2012
ymc1g11
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?
• 11-10-2012
std10093
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.

• 11-10-2012
AndiPersti
Quote:

Originally Posted by ymc1g11
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
• 11-11-2012
ymc1g11
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! :D
• 11-11-2012
ymc1g11
Quote:

Originally Posted by AndiPersti
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 :D
• 11-11-2012
c99tutorial
Quote:

Originally Posted by ymc1g11
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-2012
ymc1g11
Quote:

Originally Posted by c99tutorial
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 :D
• 11-11-2012
AndiPersti
Quote:

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

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

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