473,511 Members | 16,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printing a pointer's value

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!
Nov 14 '05 #1
9 56167

"weaselboy1976" <we***********@yahoo.com> wrote in message
news:c7**************************@posting.google.c om...
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);
Nov 14 '05 #2
On Thu, 11 Nov 2004 07:13:14 -0800, weaselboy1976 wrote:
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

Nov 14 '05 #3
weaselboy1976 wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #4
On Thu, 11 Nov 2004 15:13:14 UTC, we***********@yahoo.com
(weaselboy1976) wrote:
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.
printf ("value = |%n|\n", *zp);

return 0;
}

This code core dumps. Why? How can I accomplish this?

Thanks in advance!

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #5
weaselboy1976 wrote:
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
Nov 14 '05 #6
weaselboy1976 wrote:
...
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?

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #7
we***********@yahoo.com (weaselboy1976) wrote in message news:<c7**************************@posting.google. com>...
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
Thanks in advance!

Nov 14 '05 #8
weaselboy1976 wrote:
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.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #9
On 12 Nov 2004 08:04:33 -0800, sg*******@bloomberg.net (Stuart
Gerchick) wrote:
we***********@yahoo.com (weaselboy1976) wrote in message news:<c7**************************@posting.google. com>...
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


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

Thanks in advance!


<<Remove the del for email>>
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1914
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can...
33
4836
by: dough | last post by:
Is it possible in C to declare and initialize a pointer that points to itself? Why or why not?
69
5492
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
11
2798
alpnz
by: alpnz | last post by:
Hi, I am sure someone has managed this one before. I have a report, which I call from a button on a form, which invokes the printing of 4 copies of a report. I would like to have a box on the...
4
2345
by: Mark Ingram | last post by:
Hi, I have a function: void MyFunction(Int32^ outVar) { if (outVar != nullptr) { *outVar = 32; } }
6
9857
by: dtschoepe | last post by:
Hi all, Working on homework again... I've got a weird problem, I've been banging my head against the wall on what is causing it. I have a pointer to a typdef named Person. At one point in the...
2
5317
by: Smish | last post by:
hey all, AM using VS2003..n developing in asp.net using c# here i want to print a string value on the click of a button..i can do it in windows application.but whn am using the same code in...
2
2827
by: sharan | last post by:
like for a example xml program <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>...
1
3568
by: kumarboston | last post by:
Hi All, I have data file which has 6 column, I am trying to take the first value(-16.2577) of 6th column and substract it to the rest of the values of 6th column. I split the file on space and...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.