Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 26th, 2005, 07:55 AM
a R T u n
Guest
 
Posts: n/a
Default pointer error...

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;
}

  #2  
Old July 26th, 2005, 08:05 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: pointer error...

* a R T u n:[color=blue]
> [homework][/color]

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?
  #3  
Old July 26th, 2005, 09:05 AM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: pointer error...

a R T u n wrote:[color=blue]
>
> 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;
> }[/color]

dereferencing a null pointer has undefined behaviour.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
  #4  
Old July 26th, 2005, 09:25 AM
upashu2
Guest
 
Posts: n/a
Default Re: pointer error...

>>Is it homework or quiz?[color=blue][color=green]
>> int *pi2 = 0;[/color][/color]
Okey Your address is null.[color=blue][color=green]
>> *pi1 = 1024;[/color][/color]
I am giving you 1024 doller at your address.(But Postmaster is not
accepting the letter with null address.)[color=blue][color=green]
>>return *pi1;
>>int ival = foobar (pi2);[/color][/color]
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);

  #5  
Old July 26th, 2005, 09:35 AM
a R T u n
Guest
 
Posts: n/a
Default Re: pointer error...

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

  #6  
Old July 27th, 2005, 09:25 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: pointer error...

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

[color=blue]
> just none... only a newbie in pointers...
>[/color]

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.


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles