473,763 Members | 5,610 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 56195

"weaselboy1 976" <we***********@ yahoo.com> wrote in message
news:c7******** *************** ***@posting.goo gle.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?

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***********@y ahoo.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_z p_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***********@y ahoo.com (weaselboy1976) wrote in message news:<c7******* *************** ****@posting.go ogle.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*******@bloom berg.net (Stuart
Gerchick) wrote:
we***********@ yahoo.com (weaselboy1976) wrote in message news:<c7******* *************** ****@posting.go ogle.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
1935
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 help to clarify my doubts. I'm using g++ version 3.3.4. I created 3 classes as below for testing some concepts. The questions are written as comments in Bclass.h file. Thank you for your time and tips! ------------runtime errors------------
33
4884
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
5594
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 assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
11
2840
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 report which prints "Office Copy" on the first page, "P.O.B." on the second pages etc. The number of copies is set in the code on the Forms button. I have tried the following in the "On Print " Event on the report. PrintCount = 1 Me.cpi_own =...
4
2362
by: Mark Ingram | last post by:
Hi, I have a function: void MyFunction(Int32^ outVar) { if (outVar != nullptr) { *outVar = 32; } }
6
9889
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 code the pointer changes from storing the memory location of the Person and contains the value '1'. I have commented out several pieces of code in order to try to
2
5335
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 asp.net(web application)..its showin error on "show dialog" .so please could ne1 tell me hw to print a string value in asp.net using c#.. thanx a lot..
2
2843
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> out put should be :
1
3606
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 stored in an array but the problem is I am not able to pick the first value and substract it in while loop. DATA FILE: -16.2262 -6.01803 -25.5829 -19.4488 -14.0128 -16.2577 -18.6505 -23.0397 -26.0685 -24.5328 -21.9307 -22.8444 -30.1621 -22.6838...
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.