Reputation: 1
Hello guys I have a question,
I have an array with 10 elements and a function which returns me a pointer to a randomly chosen element in the array. Now I want to print the array from this element the pointer points to, how can I do that.
Example: Array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Pointer points to Element 6. I want to print: 6, 7, 8, 9, 10
Code looks like this in C:
int* randomElement(int* array, int size) {
int* pointer = NULL;
int randomNumber = 0;
randomNumber = rand() % size;
*pointer = *(array + randomNumber);
return pointer;
}
int main(void) {
int size = 10;
int array[size];
int* pointer = NULL;
srand(time(NULL));
pointer = randomElement(array, size);
/* Want to print the array from this element here */
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 2502
Reputation: 31
I will not comment on your code, that is already done. I'll anwer the original question instead. Blue Moon says "You can't", but you can if you wish. You can always do a sequential lookup in any table, if your list of values is terminated with what is called a sentinel value. That is a special value which is known in advance not to belong to the collection of normal data, and thus can be used to flag the end of the sequence, for instance a negative value if all normal data are known to be positive.
For a sequence of n
non-negative integers stored in an array of n+1
elements, we can store array[n]= -1
as a sentinel after populating the first n
elements with normal data. Then start at a random position in the data sequence and read until the end, with something like
// -- we assume pointer p is an initialized int* that
// satisfies the condition array <= p < array + n
// AND that array[n] is negative.
for( ; *p >= 0; ++p ){
// do something useful with *p
}
Terminating a sequence with a sentinel is a well known general technique and you'll find it in any book on algorithms. You can use it with arrays and with linked lists. In your case you must make sure that the element following the last element you wish to access in the array is a sentinel and that the random choice has not selected the sentinel (if n
is the length of the sequence, then rand() % n
will give you a correct offset; pointing to a sentinel would not harm, but the for
loop would terminate immediately). The size of the allocated array has to be at least n+1
, that is the maximum number of data elements you need, plus one for the sentinel.
By the way, in C the last element of an array is not array[size]
but array[size-1]
. There is no bounds checking on array access when bumping a pointer in C, that's up to you, as you will learn sooner or later from crashing programs. To get rid of confusion between pointers and referenced values you can read the instructive answers to "What exactly is a C pointer if not a memory address?", here at Stackoverflow.
Upvotes: 1
Reputation: 141534
The usual way to use a pointer to iterate over an array where you know the size is:
for (p = array; p != array + size; ++p)
In your case you are initializing p
differently but the rest of the loop can stay the same:
for ( ; pointer != array + size; ++pointer )
{
printf("%d\n", *pointer);
}
Of course it is also possible to use an integer index instead of a pointer (where you find the initial index by doing pointer - array
).
Note that your randomElement
function is currently implemented wrongly. It writes through a null pointer. Instead, what you want to do is to form a pointer to a randomly-selected element and return that pointer, without dereferencing. (i.e. take the *
's out of the line before the return
).
Upvotes: 0
Reputation: 121347
You can't.
By only knowing a random element in an array (i.e. having a pointer to one element), you can't print the entire array using that pointer.
You need at least two other data:
1) Length of the array (which you seem to indicate always 10.)
2) The position of the element within the array for which you have a pointer. So that you can calculate, from that position, how many elements before and after are available. (Basically determining array boundaries).
After you edit, the answer remains the same. But your code has problems:
1) You are not filling the array with values. So their values are indeterminate and accssing them is undefined behaviour.
2) You never set pointer
to point to an element of the array in randomElement()
and the function returns NULL
. you are dereferencing NULL (pointer
). Again, this will lead to undefined behaviour.
3) You should use size_t
for representing array size instead of int
.
Upvotes: 2