Yes, it is: your function takes a pointer argument, but you're not checking to make sure it's not a NULL-pointer. Dereferencing a null pointer is not allowed.
As others have pointed out: dereferencing an invalid pointer (out of bounds) results in undefined bahviour, which is bad.
Also, you are using the correct format string to print a pointer (%p
), but compile your code with -Wall -pedantic
. printing a pointer value is one of the few cases where you have to cast a pointer to void *
. IE change:
printf("%p : %.f\n", p, *p);
to
printf("%p : %.f\n", (void *) p, *p);
update:
In response to your comment: it would seem that you're actually trying to determine the length of an array that is passed as an argument. The simple fact of the matter is that you can't. An array decays into a pointer. A pointer is not an array, and therefore, so you can't determine the length of the original array. At least: not reliably.
If you are working on an array, and you want to know its length: have the caller of your function pass the length as an argument:
void f(float *arr, size_t arr_len)
{
if (arr == NULL)
exit( EXIT_FAILURE );//handle error
//do stuff
}
In short, your function relies heavily on there being a 0
after the array. The function itself can be invoked passing NULL
, too and dereferencing null is illegal. So yes, your code is dangerous.
//example setup
float foo = 123.4f
float *bar = malloc(123 * sizeof *foo);//<-- uninitialized memory, contains junk
//omitting if (bar == NULL) check, so bar might be null
//dangerous calls:
f(&foo);
f(bar);//<-- bar could be null if malloc failed, and contains junk if it didn't dangerous
f(NULL);