c指针常见错误
```c
#if 0
#include <stdio.h>
#include <stdlib.h>
int** sum(void)
{
int *result = (int*)malloc(sizeof(int));
(*result) = 200;
int **a = &result;
printf("result: %p\n", (void*)result);
return a;
}
int main() {
int **sum1 = sum();
printf("result: %p\n", (void*)*sum1);
printf("result: %d\n", **sum1);
free(*sum1);
return 0;
}
#endif
#if 1
#include <stdio.h>
#include <stdlib.h>
int** sum(void) {
int *result = (int*)malloc(sizeof(int));
if (result == NULL) {
return NULL;
}
*result = 200;
printf("result: %p\n", (void*)result);
int **ptr_to_result = (int**)malloc(sizeof(int*));
if (ptr_to_result == NULL) {
free(result);
return NULL;
}
*ptr_to_result = result;
return ptr_to_result;
}
int main() {
int **sum1 = sum();
if (sum1 != NULL && *sum1 != NULL) {
printf("result address: %p\n", (void*)*sum1);
printf("result value: %d\n", **sum1);
free(*sum1);
free(sum1);
} else {
printf("Memory allocation failed\n");
}
return 0;
}
#endif
#if 0
#include <stdio.h>
#include <stdlib.h>
int* sum(void)
{
int *result = (int*)malloc(sizeof(int));
if(!result)
{
printf("fail!\n");
return NULL;
}
(*result) = 200;
printf("result: %p\n", (void*)result);
return result;
}
int main() {
int *sum1 = sum();
printf("result: %p\n", sum1);
printf("result: %d\n", *sum1);
free(sum1);
sum1 = NULL;
return 0;
}
#endif
```c