weaselboy1976 said:
Hello
How can I print a pointer's value directly (without assigning the
value to another variable)? For example:
#include <stdio.h>
int main()
{
int *zp;
*zp = 10;
printf ("value = |%n|\n", *zp);
return 0;
}
This code core dumps. Why? How can I accomplish this?
You really need to try reading your text.
#include <stdio.h>
int main()
{
int *zp;
/* mha: your code has a wild pointer, zp, which points to your
toaster. You need to allocate some space for it to point to. Here
I just make it point to an actual int. */
int somewhere_for_zp_to_point_to; /* mha */
int c1, c2, c3;
zp = &somewhere_for_zp_to_point_to; /* mha */
*zp = 10;
printf("%s",
"You are confused about the function of the %n specifier.\n"
"It does not generate output and does require a pointer.\n"
"Here is an attempt to use\n"
" printf(\"c1 = |\\%n|\\n\", &c1);\n");
printf("c1 = |%n|\n", &c1);
printf("now c1 = |%d|\n\n", c1);
printf("Now watch:\n"
"the pointer zp is at %p (saving c1),%n\n"
" contains %p (saving c2),%n\n"
"and points to %d (saving c3)%n\n",
(void *) &zp, &c1, (void *) zp, &c2, *zp, &c3);
printf("c1 = %d, c2 = %d, c3 = %d\n", c1, c2, c3);
return 0;
}
You are confused about the function of the %n specifier.
It does not generate output and does require a pointer.
Here is an attempt to use
printf("c1 = |\%n|\n", &c1);
c1 = ||
now c1 = |6|
Now watch:
the pointer zp is at effdc (saving c1),
contains effd8 (saving c2),
and points to 10 (saving c3)
c1 = 50, c2 = 79, c3 = 108