scanf and the p conversion specifier - c

In the C11 specification is said that argument type of %p must be void ** in case of scanf() function but I can't figure how to input an address and store it into a void **.
Infact if I try to make:
void **p;
scanf("%p", p);
I get a segmentation fault.
P.S.
C11 specification:
The corresponding argument shall be a pointer to a pointer to void

void **p;
scanf("%p", p);
doesn't work for the same reason that
int *i;
scanf("%i", i);
doesn't work - you're writing to an uninitialized pointer (or telling scanf to write to one, at least).
This works:
int i;
scanf("%i", &i);
and so does this:
void *p;
scanf("%p", &p);

NOTE: Original post did not have a mention for c11 standard.
Well, as per the c99 specification document, chapter 7.19.6. 1 2, paragraph 8 12,
p
Matches an implementation-defined set of sequences, which should be the
same as the set of sequences that may be produced by the %p conversion of
the fprintf function. The corresponding argument shall be a pointer to a
pointer to void. The input item is converted to a pointer value in an
implementation-defined manner. If the input item is a value converted earlier
during the same program execution, the pointer that results shall compare
equal to that value; otherwise the behavior of the %p conversion is undefined.
So, it is not void **, rather , a void *.
Next, Your reason for a segmentation fault is the use of uninitialized pointer p in scanf(). You did not allocate memory to p before using [passing it to scanf() as argument] it.
As others' suggested, you can nicely use something like
void *p;
scanf("%p", &p);
here, the &p value is actually pointer to a pointer to void and have a defined address.

You didn't initialize p. Try:
void *p;
scanf("%p", &p);
This takes the address of p, which is valid.

Standard says that %p should have void * type argument. You need to declare p as void * and then
scanf("%p", &p);
The reason that you are getting segfault is because in void **p, p is not pointing anywhere.

Related

Formatted printing pointer value

I found that we can print the pointer value with %p format specifier. N1570 7.21.6.1(p8):
p The argument shall be a pointer to void. The value of the pointer is
converted to a sequence of printing characters, in an
implementation-defined manner
Since the pointer to void can be converted to pointer to any other object type I'm curious would the conversion by hand is necessary. Example:
struct test_t{
int a;
}
void foo(){
struct test_t *test_ptr = malloc(sizeof(*test_ptr));
printf("Pointer test_ptr = %p\n", test_ptr);
}
Here I did not convert it to void * assuming that the compiler does this for me. Is it conforming? Or I should convert such pointers to void * on my own like
printf("Pointer test_ptr = %p\n", (void *) test_ptr);
The Standard specifies that the representation of and alignment of void * is the same as char *. It is not specified if the void * representation/alignement is must be the same as any object type.
The version with (void *) is correct, and your original code causes undefined behaviour. (You already provided the relevant Standard quote so I have no need to add further quotes).
Since the pointer to void can be converted to pointer to any other object type I'm curious would the conversion by hand is necessary
"can be" means there is potential for a conversion to happen. Such a conversion only actually happens when it was requested , with the language standard specifying which constructs request a conversion.
For the arguments to printf, only the default argument promotions are applied. There are not any other conversions.

What does (void*) mean in context of typecasting? [duplicate]

This question already has answers here:
printf("%p") and casting to (void *)
(7 answers)
Closed 5 years ago.
What does (void*) mean in the following code? I tried removing (void*) typecast but it still works just fine and prints the address of usrInt variable. Can you explain this, please?
#include <stdio.h>
int main(void) {
int usrInt = 0; // User defined int value
int* myPtr = NULL; // Pointer to the user defined int value
// Prompt user for input
printf("Enter any number: ");
scanf("%d", &usrInt);
// Output int value and location
printf("We wrote your number into variable usrInt.\n");
printf("The content of usrInt is: %d.\n", usrInt);
printf("usrInt's memory address is: %p.\n", (void*) &usrInt);
printf("\nWe can store that address into pointer variable myPtr.\n");
// Grab location storing user value
myPtr = &usrInt;
// Output pointer value and value pointed by pointer
printf("The content of myPtr is: %p.\n", (void*) myPtr);
printf("The content of what myPtr points to is: %d.\n", *myPtr);
return 0;
}
Normally, a cast to or from void * is simply redundant, because this conversion is implicit in C for any data pointer type.
Passing a pointer to a variadic function is a special case that requires the cast.
printf() is a variadic function. The printf() prototype looks like this:
int printf(const char *format, ...);
This means the compiler doesn't see any type declarations for parameters that come after format. Therefore, it doesn't know the type passed should be void *, so the conversion doesn't happen automatically.
Often, omitting the cast won't have any visible effect because in most implementations, all pointers have the exact same internal representation. But the C standard doesn't guarantee that, so implementations are possible where e.g. an int * would have a different representation when it is converted to a void *. If you omit the cast, your code is technically incorrect (it invokes undefined behavior for passing the wrong type for the conversion %p) and would lead to wrong results on platforms where representations of pointers to different types are different.
c11: 7.21.6 Formatted input/output functions (p8):
p - The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

What's the point of writing (void*) in front of integer pointer? [duplicate]

This question already has answers here:
When printf is an address of a variable, why use void*?
(4 answers)
Closed 6 years ago.
I read the following program code:
#include <stdio.h>
#include <stdlib.h>
int main () {
int *p;
p= malloc(sizeof(int));
*p=42;
printf("Pointer p has address %p and points to %p\n",
(void*)&p, (void*)p);
free(p);
}
My question refers to the following part:
printf("Pointer p has address %p and points to %p\n", (void*)&p, (void*)p);
I don't understand what the (void*) is doing. Is this a cast? What's the point of doing this?
What is the difference of the above to simply writing the following?
printf("Pointer p has address %p and points to %p\n", &p, p);
This is because, %p explicitly expects an argument of type void *.
Quoting C11, chapter ยง7.21.6.1, fprintf()
p The argument shall be a pointer to void. [....]
Since printf() is a variadic function and there is no default argument promotion for pointers, the cast is required.
Strictly speaking, as per the standard mandate, from same chapter, paragraph 7
[...] If any argument is
not the correct type for the corresponding conversion specification, the behavior is
undefined.
That said, to make the answer complete,
p = malloc(sizeof(int)); can be rewritten as p= malloc(sizeof*p); to make it more robust.
You should always check against the success of malloc() before using the returned pointer.
I don't understand what the (void*) is doing. Is this a cast? What's the point of doing this?
Yes, it's a cast. It doesn't seem to be necessary with my compiler (llvm), which gives the following output even if I remove the casts:
Pointer p has address 0x7fff501357d8 and points to 0x7fcb38c00390
Other compilers may not be so forgiving.

why do we need to use (void*)&a instead of &a

int main()
{
int a;
int* b;
a = 40;
b = &a;
printf("the address of a is %p, and the value of a is %d \n",&a, a);
return 0;
}
I find that both (void*)&a and &a print the same thing. So why do people still add (void*)? Is it just a habit?
You use specifier %p to print address stored in pointer, and this format specifier expects type to be void * . And as &a is of type int * , cast void * is used .
The printf() format specifier %p expects a void* type pointer. Since what you are passing might not be a void* type pointer, and the standard does not mandate for all pointers to have the same format, is is important that you cast a pointer to void* before passing it to printf.
For instance:
int* a = malloc(sizeof(int));
printf("a is %p",(void*)a);
Is done as best practice incase int* and void* are not similar
C Standard says that using an incorrect specifier for an argument in printf will result in undefined behavior.
7.21.6.1 The fprintf function
If a conversion specification is invalid, the behavior is undefined. 282) If any argument is
not the correct type for the corresponding conversion specification, the behavior is
undefined.
Since pointer to int is not the same type as pointer to void, and %p may only be used for a pointer to void and even if some other rules says that any pointer may be converted to pointer to void and back, that doesn't change the fact that the behavior is undefined, because of the quoted rule.
Firstly, different pointer types are not necessarily interchangable.
Secondly, for varargs no implicit conversion takes place as the compiler does not know the expected type.

What is printed when I write function name in C using printf() function

When I'm writing this code:
#include <stdio.h>
int main()
{
printf("%p\n",main);
printf("%d\n",main);
return 0;
}
my compiler shows me this output:
00401318
4199192
I'm interested to know what actually is printed. I googled my question, but have found nothing. :(
Thanks in advance.
This is not well-defined.
You're using %p, which expects an argument of type void *, but you're actually passing it a value of type int (*)(), i.e. your (also badly defined) main() function.
You cannot portably cast a function pointer to void *, so your code can never be correct.
On most typicaly systems, sizeof (void *) == sizeof main, so you simply get the value interpreted as a void * which probably will simply be the address of the function.
Passing a function address to printf() with a format specifier of %d is even worse, since it's quite likely that sizeof (int) != sizeof main and then you get undefined behavior.
This is not good code.
main is a function pointer of type int(*)(void)
printf("%p\n", main);
You are printing the address of that pointer, which, on your platform has been successfully cast to a void*. This will be fine if sizeof(main) == sizeof(void*).
printf("%d\n", main);
This will give you undefined behaviour since %d is not a good format specifier for a pointer type.

Resources