By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,479 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,479 IT Pros & Developers. It's quick & easy.

pointer error...

P: n/a
what do you think is wrong with this?

#include <iostream>

int foobar (int *pi1)
{
*pi1 = 1024;
return *pi1;
}

int main ()
{
int *pi2 = 0;

int ival = foobar (pi2);

return 0;
}

Jul 26 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* a R T u n:
[homework]


See <url: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 26 '05 #2

P: n/a
a R T u n wrote:

what do you think is wrong with this?

#include <iostream>

int foobar (int *pi1)
{
*pi1 = 1024;
return *pi1;
}

int main ()
{
int *pi2 = 0;

int ival = foobar (pi2);

return 0;
}


dereferencing a null pointer has undefined behaviour.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 26 '05 #3

P: n/a
>>Is it homework or quiz?
int *pi2 = 0; Okey Your address is null. *pi1 = 1024; I am giving you 1024 doller at your address.(But Postmaster is not
accepting the letter with null address.)return *pi1;
int ival = foobar (pi2);

You haven't get it. Why?
You forget to given your address.
First construct your house either on stack or on heap, then give the
address of that.
int pi = 0; /// Now &pi is valid address of your house pi constructed
on stack.
int ival = foobar (&pi);

Jul 26 '05 #4

P: n/a
just none... only a newbie in pointers...

Jul 26 '05 #5

P: n/a
----- Original Message -----
From: "a R T u n" <mu********@gmail.com>
Newsgroups: comp.lang.c++
Sent: Tuesday, July 26, 2005 1:26 AM
Subject: Re: pointer error...

just none... only a newbie in pointers...


Did you understand the answers?

int *p = 0; // Defines a pointer pointing to "nothing" (a NULL pointer)

You can't use this before you point it to some place to actually store the
interger.

*p = 1024; // Error, p isn't pointing to anything your program owns.

p = new int; // allocates sizeof(int) memory, p now "points" to it.

*p = 1024; // Now 1024 goes into those sizeof(int) bytes that were
allocated.
Jul 27 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.