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 ...

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:

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?

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.

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?

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

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.