How works pointer on function in C
they work well if they are set to a valid address, this is not the case in your program.
In
vec *new_vec(double x, double y) {
vec* vector;
vector->x = x;
vector->y = y;
return vector;
}
you missed to allocate the vector, vector is not set but you dereference it, the behavior in undefined (typically can be a crash)
do
vec* vector = malloc(sizeof(vec));
Same problem in
vec *add_vec(const vec *const v1, const vec *const v2) {
vec* vector_result;
vector_result->x = v1->x;
vector_result->y = v2->y;
return vector_result;
}
do
vec* vector_result = malloc(sizeof(vec));
And again same problem in main :
int main(int argc, char *argv[]) {
vec* vec1;
vec* vec2;
vec1->x = 2.0;
vec1->y = 3.0;
vec2->x = 4.0;
vec2->y = 7.0;
vec* vector = add_vec(vec1, vec2);
probably you wanted to do
vec * vec1 = new_vec(2.0, 3.0);
vec * vec2 = new_vec(4.0, 7.0);
vec * vector = add_vec(vec1, vec2);
but you can also do not allocate the vec1 and vec2 because they are temporary and just used to initialize vector :
int main(int argc, char *argv[]) {
vec vec1;
vec vec2;
vec1.x = 2.0;
vec1.y = 3.0;
vec2.x = 4.0;
vec2.y = 7.0;
vec* vector = add_vec(&vec1,&vec2);
...
Note your code never free the allocated blocks, to do that and also modifying the printf for a more readable result and removing the useless parameters :
int main(void) {
vec vec1;
vec vec2;
vec1.x = 2.0;
vec1.y = 3.0;
vec2.x = 4.0;
vec2.y = 7.0;
vec* vector = add_vec(&vec1,&vec2);
printf("%f %f\n", vector->x, vector->y);
free(vector);
}
Compilation and execution:
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra -g v.c
pi@raspberrypi:/tmp $ ./a.out
2.000000 7.000000
Execution under valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out
==3154== Memcheck, a memory error detector
==3154== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3154== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3154== Command: ./a.out
==3154==
2.000000 7.000000
==3154==
==3154== HEAP SUMMARY:
==3154== in use at exit: 0 bytes in 0 blocks
==3154== total heap usage: 2 allocs, 2 frees, 1,040 bytes allocated
==3154==
==3154== All heap blocks were freed -- no leaks are possible
==3154==
==3154== For counts of detected and suppressed errors, rerun with: -v
==3154== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $
Or allocating vec1 and vec2 :
int main(void) {
vec * vec1 = new_vec(2.0, 3.0);
vec * vec2 = new_vec(4.0, 7.0);
vec* vector = add_vec(vec1, vec2);
// vec1 and vec2 useless from here
free(vec1);
free(vec2);
printf("%f %f\n", vector->x, vector->y);
free(vector);
}
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra -g v.c
pi@raspberrypi:/tmp $ ./a.out
2.000000 7.000000
pi@raspberrypi:/tmp $
Execution under valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out
==3191== Memcheck, a memory error detector
==3191== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3191== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3191== Command: ./a.out
==3191==
2.000000 7.000000
==3191==
==3191== HEAP SUMMARY:
==3191== in use at exit: 0 bytes in 0 blocks
==3191== total heap usage: 4 allocs, 4 frees, 1,072 bytes allocated
==3191==
==3191== All heap blocks were freed -- no leaks are possible
==3191==
==3191== For counts of detected and suppressed errors, rerun with: -v
==3191== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $