I saw the comment, but I forgot the size is specified in the declaration, so I basically was like "you give me an array, but the memory is not allocated yet", which is wrong, thanks!
I am doing codewars fundamentals only right now, in between reading "The C Programming Language", and I am only on chapter 2, so yeah I know nothing about how all this stuff works.
your malloc() doesnt do anything, it just leaks memory. it is overwritten by the first pass of your for loop. Re-assigning integers inside your function will not affect its value outside of your function. Besides, your are assigning an address to an integer, the compiler most likely warned you about this:
warning: assignment to ‘int’ from ‘void *’ makes integer from pointer without a cast [-Wint-conversion]
This is because *integers points to the first element of the array; i.e. to an int. But even if you had written integers = malloc(...), this would make no sense, because the caller would not see the change: through the function call your are given a copy of the address of integers. If you wanted to change what integers point to, your function would need to take an int **, not an int *.
its not specified
It is specified by a comment in the initial code, which is still there in your solution:
There are warnings in sample test from C compiler:
fixture.c:104:14: warning: passing 'char []' to parameter of type 'unsigned char *' converts between pointers to integer types with different sign [-Wpointer-sign]
imageTest (codewars_rgb,
^~~~~~~~~~~~
fixture.c:18:32: note: passing argument to parameter 'imageData' here
void imageTest (unsigned char *imageData, int h, int w, void* weights, int n, const unsigned char *expected) {
^
fixture.c:107:14: warning: passing 'char []' to parameter of type 'const unsigned char *' converts between pointers to integer types with different sign [-Wpointer-sign]
codewars_laplacianFiltered);
^~~~~~~~~~~~~~~~~~~~~~~~~~
fixture.c:18:100: note: passing argument to parameter 'expected' here
void imageTest (unsigned char *imageData, int h, int w, void* weights, int n, const unsigned char *expected) {
^
2 warnings generated.
It may be fixed by explicit conversion to (unsigned char *):
Thanks for your explanation.
I saw the comment, but I forgot the size is specified in the declaration, so I basically was like "you give me an array, but the memory is not allocated yet", which is wrong, thanks!
I am doing codewars fundamentals only right now, in between reading "The C Programming Language", and I am only on chapter 2, so yeah I know nothing about how all this stuff works.
@lowlight
your
malloc()
doesnt do anything, it just leaks memory. it is overwritten by the first pass of yourfor
loop. Re-assigningintegers
inside your function will not affect its value outside of your function. Besides, your are assigning an address to an integer, the compiler most likely warned you about this:This is because
*integers
points to the first element of the array; i.e. to anint
. But even if you had writtenintegers = malloc(...)
, this would make no sense, because the caller would not see the change: through the function call your are given a copy of the address ofintegers
. If you wanted to change whatintegers
point to, your function would need to take anint **
, not anint *
.It is specified by a comment in the initial code, which is still there in your solution:
bruh. its not specified so i calculated the size and allocted that much with malloc
integers
is already preallocated by the tests and it is ensured that it is big enough to hold the result.The
++
just moves the pointer forward.are you dynamically adding more space to the array with the *integers++?
Too easy for 6 kyu.
fixed
This comment is hidden because it contains spoiler information about the solution
There are warnings in sample test from C compiler:
It may be fixed by explicit conversion to (unsigned char *):
Seems too easy for 5 kyu.