Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int* cut(int* x, int n);
int main() {
int *x= NULL,n;
x = malloc(sizeof(int));
printf("Amount of elements: ");
scanf("%d", x);
x = realloc(x, (*x 1) * sizeof(int));
assert(x != NULL);// allocate memory and ensure that this was successful
for (int i = 1; i <= *x; i ) {
printf("%d Element: ", i);
scanf("%d", x i);
}
printf("pper barrier: ");
scanf("%d", &n);
x = cut(x,n);
for (int i = 1; i <= *x; i ) {
printf("%d ", *(x i));
}
printf("\n");
free(x);
}
int* cut(int *x, int n) {
int a = 1;
for (int i = 1; i <= *x; i ) {
if(*(x i) < n) {
*(x a) = *(x i);
a ;
}
}
*x = a-1;
x = realloc(x, (a 1) * sizeof(int));
return x;
}
The code works fine however I do not unterstand the line x = realloc(x, (*x 1) * sizeof(int));
Furthermore I don't get why the first x
has no *
but the second (*x 1)
has one. Does this mean a pointer of the pointer?
I think it just means that the array malloc that was made is getting bigger by one value However I'm not sure and a still a bit confused what that actually means, could someone please help me out and clarify my missunderstanding?
CodePudding user response:
x = realloc(x, (*x 1) * sizeof(int));
Let's start with this part:
x = realloc(x, ...
The code is calling realloc
and passing as the first parameter whatever x
points to and storing the result in x
. That means that the block currently pointed to by x
will be resized and the result stored in x
. So x
will point to a resized version of whatever x
pointed to before.
The (x 1) * sizeof(int)
is the size of the block. Let's start with the right side, * sizeof(int)
. That means enough space to store some number of int
s. How many int
s? *x 1
of them.
So this means to change x
to point to a resized version of the block x
already pointed to, resizing the block to hold one more integer than the value of the integer x
points to.
CodePudding user response:
scanf("%d", x);
This sets the integer at the address x
to the value that the user typed. For example, if the user types 3
, this line does the equivalent of *x = 3;
.
x = realloc(x, (*x 1) * sizeof(int));
*x
is dereferencing x
, not making a pointer to x
(that would be &x
). So it's the value that the user typed. So this line makes x
point to an array of N 1 elements, where N is the value that the user typed. (Assuming the memory allocation succeeds.)
Later, in cut
,
*x = a-1; x = realloc(x, (a 1) * sizeof(int));
these two lines again maintain the invariant that the first element of the array is the number of elements that follow.
CodePudding user response:
I do not unterstand the line
x = realloc(x, (*x 1) * sizeof(int));
x
is an int*
. *x
dereferences x
(so that you get the value entered by the user in the previous scanf("%d", x);
). So, (*x 1)
means, "the value entered by the user plus one". Take that and multiply it with the size of an int
. The x
argument to realloc
is the pointer with a prior allocation that you want to increase the allocated memory for. The returned pointer (that may be the same or a new value as the prior x
) is then assigned to x
.
Furthermore I don't get why the first
x
has no*
but the second (*x 1) has one. Does this mean a pointer of the pointer?
x = ...
means "assign a value to x
". realloc
returns a void*
which can be implicitly converted to any pointer type, like an int*
, and x
is an int*
.
In (*x 1)
the *
is used to dereference x
, that is, fetch the value of the int
that x
is pointing at.