This is task;
Define a function that accepts two integer parameters. The function returns the pointer to the integer value that it will dynamically allocate. Implement a function to dynamically allocate memory for a integer variable if the arguments values are the same. The function when allocating should initialize the variable to 0. If arguments do not have the same values, the memory is not allocated and the function returns NULL.
I create my code but i think that it's not good:
#include <stdio.h>
#include <stdlib.h>
int* funk(int a, int b ){
int *p;
if(a == b){
p = (int*)calloc(a,sizeof(int));
return p;
}
else{
return NULL;
}
}
int main(void){
int *p = NULL;
p = funk(4 , 4);
printf("%d", *p);
free(p);
return 0;
}
So, can somebody check this code and help me?