I have a function that generates a random array, and I have another function which prints the generated array.
The issue is that in the print function I only have the address of the first & last index of the array.
How can I can print the whole array using only the addreses of the first element and the last element?
Here is the function which is required to print the array :
void printArray(int *s_ptr, int *e_ptr);
And here is my main function :
int randInRange(int min, int max) { return min rand() % (max - min 1); }
int main(void) {
int array[MaxSize];
srand(time(NULL));
for(int i=0 ; i<MaxSize ; i ) {
array[i] = (int) randInRange(MIN, MAX);
}
printArray(&array[0], &array[MaxSize-1]);
printArray(&array[0], &array[MaxSize-1]);
}
CodePudding user response:
There is not much to it. Just increment the pointer. → p
void printArray(int *s_ptr, int *e_ptr) {
for (int *p = s_ptr; p <= e_ptr; p ) {
printf("%d\n", *p);
}
}
CodePudding user response:
How can I can print the whole array using only the addreses of the first element and the last element?
To start with, couple of things about array that you should know (if not aware of):
An array is a collection of elements of the same type placed in contiguous memory locations.
An array name, when used in an expression, will convert to pointer to first element of that array (there are few exceptions to this rule).
E.g.:
#include <stdio.h>
int main (void) {
int arr[] = {1, 2, 3, 4, 5};
// Hardcoding the size of array (5) in for loop condition
// Note that you can use expression - sizeof (arr) / sizeof (arr[0]) to get size
// of array instead of hardcoding it.
for (size_t i = 0; i < 5; i) {
printf ("%d\n", arr[i]);
}
return 0;
}
Here the arr[i]
, in printf()
statement, will be interpreted as1) -
(*((arr) (i)))
and, in this expression, the arr
will be converted to address of first element of array arr
i.e. &arr[0]
and to that address the value of i
will be added and the resultant pointer will be then dereferenced to get the value at that location. Since, the array element placed in contiguous memory locations, adding 0
, 1
, 2
, .., and so on, to the address of first element of array and dereferencing it will give the value of 1
st, 2
nd, 3
rd, .., and so on, elements of array respectively.
Now, you are passing the address of first and last element of given array to function printArray()
. Using the address of first element we can get the value of all the elements of array. The only thing we need is the size of array in printArray()
function.
From C Standard#6.5.6p9 [emphasis added]
When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements. The size of the result is implementation-defined, and its type (a signed integer type) is ptrdiff_t defined in the <stddef.h> header.
The two argument to printArray()
function are pointer to first and last element of same array. As you have shown in question, the printArray()
is called like this:
printArray(&array[0], &array[MaxSize-1]);
^ ^^^^^^^^^
| |
subscript subscript
of first of last
element element
When we subtract them the result we get is the difference of the subscripts of the first and last elements of that array. That means, in printArray()
function() this
e_ptr - s_ptr
will give result same as
MaxSize-1 - 0 = MaxSize-1
if we add one to this result, we will get size of array
MaxSize-1 1 = MaxSize
Now, we know how to get size of array in printArray()
and also know how to get the value of all elements of array using the address of first element. Lets implement printArray()
:
void printArray(int *s_ptr, int *e_ptr) {
// To get the size of array subtract e_ptr and s_ptr and add 1
ptrdiff_t size = e_ptr - s_ptr 1;
for (ptrdiff_t i = 0; i < size; i) {
printf ("%d\n", *((s_ptr) (i)));
}
}
This will output all the elements of array, provided s_ptr
should be pointer to first element and e_ptr
should be pointer to last element of same array. This *((s_ptr) (i))
looks bit cluttered. As per the definition of subscript operator []
1), *((s_ptr) (i))
is same as s_ptr[i]
. So, the printf()
statement in the for loop can be written as - printf ("%d\n", s_ptr[i]);
.
Alternatively, you can use the pointer of type same as type of array element. Assign the first element address to this pointer and dereferencing it to get value at that location. Increment the pointer and print till it reaches to address of last element of array. The implementation is shown in @Cheatah post.
1). C11 standard##6.5.2.1p2
The definition of the subscript operator [] is that E1[E2] is identical to (*((E1) (E2)))