printing a pointer's value

W

weaselboy1976

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?

Thanks in advance!
 
D

dandelion

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?

Thanks in advance!

printf ("value = |%d|\n", *zp);
 
L

Lawrence Kirby

Hello

How can I print a pointer's value directly (without assigning the
value to another variable)? For example:

You need to make a clear distinction between the value of a pointer and
the thing it is pointing at.
#include <stdio.h>

int main()
{
int *zp;

Here you have defined zp as a pointer to an int. As yet the value of the
pointer hasn't been set - it doesn't point at anything.
*zp = 10;

*zp tries to dereference zp i.e. access what zp is pointing at. But zp
isn't pointing at anything so this operation is invalid. You need to
create an int for zp to point at. A simple way is to define an int
variable and set it to point at that e.g.

int i;
int *zp = &i;
*zp = 10; /* i is now set to 10 */
printf ("value = |%n|\n", *zp);

Use %d to convert an int, %n does something very different.

printf ("value = |%d|\n", *zp);
return 0;
}
}

You have an extra } here
This code core dumps. Why? How can I accomplish this?

C doesn't magically allocate things for pointers to point at, it
is up to you to do this. In the code above there isn't any advantage
to using *zp over i directly, but there are other cases where
pointers are useful/necessary, e.g. for passing to other functions,
dynamic datastructures and so on.

Lawrence
 
E

Eric Sosman

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?

The crash is the subject of Question 7.1 in the
comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

However, there's a strange disconnect between what
you're doing and what you're asking about, so I
suspect your confusion may run too deep for the
unaided FAQ to cure. A reading of Sections 4 and 7,
with 5 and 6 thrown in for good luck, might help --
but I have a feeling that what you really need to do
is re-open your C textbook at or near page 1.
 
H

Herbert Rosenau

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;

Assigning a value to an indetermined pointer is invoking undefined
behavior. You ties to dereference a pointer that contains an
untetermined value as it is not initialised.
 
M

Martin Ambuhl

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
 
A

Andrey Tarasevich

weaselboy1976 said:
...
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?
...

Accomplish what exactly? You message is self-contradictory. Initially
you state that you want to print a _pointer's_ value. Then in the code
you make an attempt to assign and print the value of the object
"pointed" to by the pointer. (The program crashes because you haven't
initialized the pointer, i.e. it points nowhere).

You need to decide first what value you actually are trying to print.
The pointer's? Of the pointed object's?
 
S

Stuart Gerchick

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?

I dont really understand. Are we trying to print the value of the
pointer (ie and address) or what the pointer points to. Also, either
way you should use %d in the printf. also you shouldnt *zp something
where zp hasnt been assigned to any memory
 
C

Chris Dollin

weaselboy1976 said:
How can I print a pointer's value directly (without assigning the
value to another variable)? For example:

You mean, write the pointer value? The address-like thing?

Use the %p format specifier.
#include <stdio.h>

int main()
{
int *zp;
*zp = 10;

Undefined behaviour. zp doesn't point anywhere (it's uninitialised).
printf ("value = |%n|\n", *zp);

return 0;
}

This code core dumps. Why?

Because you are lucky today: the implementation has admitted that
your code is wrong. On a bad day, it would have just printed 10,
and you'd have thought what you'd done was OK.
 
B

Barry Schwarz

I dont really understand. Are we trying to print the value of the
pointer (ie and address) or what the pointer points to. Also, either
way you should use %d in the printf. also you shouldnt *zp something

If you are trying to print the value of the pointer, then you should
be using %p, not %d. You also need to cast the pointer to void*.
where zp hasnt been assigned to any memory



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top