473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 26 '05 #1
5 1269
* 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
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
>>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
just none... only a newbie in pointers...

Jul 26 '05 #5
----- 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
19
by: Lucas Machado | last post by:
i'm doing some Linux Kernel hacking for a course i'm currently taking. there is a pointer to a struct (struct example_struct *ex_ptr) in a .c that i want to access in a system call. i defined a...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
5
by: Amit_Basnak | last post by:
Dear Friends I have been getting the following error Error 185: "WorkFlow_dce.cpp", line 58 # Left side of '->' requires a pointer to class; type found was 'struct WF_SEARCH_WU'....
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
1
by: urkel | last post by:
Hi everyone, I critically need help to solve this problem related to pointer in C++ Basically, I have a C/C++ program "retardselfenerg" calling a Fortran 90 subroutine "surfGF4.f90". i am so...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...
0
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
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,...

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.