473,666 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer return: address correct / value nonesense

I start hating RETURN!!!

list<happy> //#include <list>// is searched via iterator (ptr) so long
until the argument "number" of the class "happy" is equal now.
The function "findhappy" works fine, finds the right pointer "nownumber"
with correct values.

Behind "return" the memory address remains correct but the values are
wrong (a huge negative number and all the same).

Tried to define "nownumber" and "ptr" as locals OR
to submit them as parameter to the function "findhappy" OR
to define them globally. In any case I always have the same problem.
Any solutions????

Thanks in advance

//THIS WAS MY LAST TRY
happy* findhappy (list<happy> a, int now)
{
ptr=a.begin();
nownumber=ptr.o perator ->();
while (nownumber->number!=now)
{
ptr=ptr.operato r ++();
nownumber=ptr.o perator ->();
}
return nownumber;
}
Jul 22 '05 #1
8 1907
On Fri, 9 Apr 2004 18:43:15 +0200, "Huibuh" <no*****@t-online.de> wrote:
I start hating RETURN!!! I. Yay. Yay.

list<happy> //#include <list>// is searched via iterator (ptr) so long
until the argument "number" of the class "happy" is equal now.
The function "findhappy" works fine, finds the right pointer "nownumber"
with correct values.

Behind "return" the memory address remains correct but the values are
wrong (a huge negative number and all the same).

Tried to define "nownumber" and "ptr" as locals OR
to submit them as parameter to the function "findhappy" OR
to define them globally. In any case I always have the same problem.
Any solutions????

Thanks in advance

//THIS WAS MY LAST TRY
happy* findhappy (list<happy> a, int now)
{
ptr=a.begin();
nownumber=ptr.o perator ->();
while (nownumber->number!=now)
{
ptr=ptr.operato r ++();
nownumber=ptr.o perator ->();
}
return nownumber;
}


A bunch of issues, which at this point I actually find easier to cover in
code than in English, so here's something to nudge you in the right
direction, I hope. Note I typedef-ed "happy" as "int" in order to focus on
things other than the way you deal with the container's value_type.

I left the logic returning a pointer, the way you had it, but you might
want to look into using STL algorithms such as std::find, which return
iterators, as replacements for your findhappy-type functions. So here we
go:
// happy.cpp

#include <iostream>
#include <list>
using namespace std;
typedef int happy;

happy* findhappy (list<happy>& a, int now)
{
list<happy>::it erator ptr;

for (ptr = a.begin(); ptr != a.end(); ++ptr)
if (*ptr == now)
return &*ptr;
return 0;
}

int main()
{
list<int> li;
li.push_back(1) ;
li.push_back(5) ;
li.push_back(9) ;
li.push_back(19 );

if (happy *p = findhappy(li, 9))
cout << "found it! Value is:" << *p << endl;
else
cout << "didn't find it." << endl;

return 0;
}

HTH,
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
On Fri, 9 Apr 2004 18:43:15 +0200, "Huibuh" <no*****@t-online.de> wrote:
I start hating RETURN!!! I. Yay. Yay.

list<happy> //#include <list>// is searched via iterator (ptr) so long
until the argument "number" of the class "happy" is equal now.
The function "findhappy" works fine, finds the right pointer "nownumber"
with correct values.

Behind "return" the memory address remains correct but the values are
wrong (a huge negative number and all the same).

Tried to define "nownumber" and "ptr" as locals OR
to submit them as parameter to the function "findhappy" OR
to define them globally. In any case I always have the same problem.
Any solutions????

Thanks in advance

//THIS WAS MY LAST TRY
happy* findhappy (list<happy> a, int now)
{
ptr=a.begin();
nownumber=ptr.o perator ->();
while (nownumber->number!=now)
{
ptr=ptr.operato r ++();
nownumber=ptr.o perator ->();
}
return nownumber;
}


A bunch of issues, which at this point I actually find easier to cover in
code than in English, so here's something to nudge you in the right
direction, I hope. Note I typedef-ed "happy" as "int" in order to focus on
things other than the way you deal with the container's value_type.

I left the logic returning a pointer, the way you had it, but you might
want to look into using STL algorithms such as std::find, which return
iterators, as replacements for your findhappy-type functions. So here we
go:
// happy.cpp

#include <iostream>
#include <list>
using namespace std;
typedef int happy;

happy* findhappy (list<happy>& a, int now)
{
list<happy>::it erator ptr;

for (ptr = a.begin(); ptr != a.end(); ++ptr)
if (*ptr == now)
return &*ptr;
return 0;
}

int main()
{
list<int> li;
li.push_back(1) ;
li.push_back(5) ;
li.push_back(9) ;
li.push_back(19 );

if (happy *p = findhappy(li, 9))
cout << "found it! Value is:" << *p << endl;
else
cout << "didn't find it." << endl;

return 0;
}

HTH,
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
On Fri, 9 Apr 2004 18:43:15 +0200 in comp.lang.c++, "Huibuh"
<no*****@t-online.de> wrote,
I start hating RETURN!!!
I think that's a new one.
Behind "return" the memory address remains correct but the values are
wrong (a huge negative number and all the same). //THIS WAS MY LAST TRY
happy* findhappy (list<happy> a, int now)


You do NOT want to make a copy of your list to search! Should be

happy* findhappy (list<happy> & a, int now)

You are returning a pointer to something in the COPY of your original
list, and all results from there on are junk. See the thread "Strange
pointer results" from yesterday for more discussion of the same trouble!

Jul 22 '05 #4
On Fri, 9 Apr 2004 18:43:15 +0200 in comp.lang.c++, "Huibuh"
<no*****@t-online.de> wrote,
I start hating RETURN!!!
I think that's a new one.
Behind "return" the memory address remains correct but the values are
wrong (a huge negative number and all the same). //THIS WAS MY LAST TRY
happy* findhappy (list<happy> a, int now)


You do NOT want to make a copy of your list to search! Should be

happy* findhappy (list<happy> & a, int now)

You are returning a pointer to something in the COPY of your original
list, and all results from there on are junk. See the thread "Strange
pointer results" from yesterday for more discussion of the same trouble!

Jul 22 '05 #5
Huibuh wrote:
I start hating RETURN!!!
You'd be screwed without it.
[...]


// What you are about to see is a reconstruction.
// It's also a complete, compilable translation unit.
// Please check "How to post" in the FAQ.

#include <list>
#include <iostream>
#include <ostream>

struct happy
{
int number;
happy (int n = 0) : number (n) { }
};

// //THIS WAS MY LAST TRY
// // *** I hope you can follow my comments.

// happy* findhappy (list<happy> a, int now)
// // *** This copies the argument into the parameter element-
// // *** by-element. We often pass large objects _by reference_
// // *** rather than _by value_ (unless we need a local _copy_

happy * findhappy (std::list <happy> & a, int now)
{

// {
// ptr=a.begin();
// // *** We shan't need this iterator outside this scope
// // *** so we can declare it here:

std::list <happy>::iterat or iter = a.begin ();

// nownumber=ptr.o perator ->();
// *** Whoah there! "happy * nownumber = & * ptr;" would be easier
// *** to read; but wait a minute, we're not going to do it like this.

// while (nownumber->number!=now)
// {
// ptr=ptr.operato r ++(); // *** Just "++ ptr;"
// nownumber=ptr.o perator ->(); // *** Just "nownumber = & * ptr;"
// }
// return nownumber;
// }
// // *** The logic of this loop is all wrong. Get out a pencil
// // *** and paper and work out what would happen if the value
// // *** "now" is not present in a happy object in the list.
// // ***
// // *** The following works OK. We don't need to transform the
// // *** iterator into a pointer until just before we return
// // *** from the function, so that's what I do here.
// // *** I notice that we're simulating a "for" loop here.
// // *** Maybe you can change it for the real thing.

while (iter != a.end ())
{
if (iter->number == now)
return & (* iter );
++ iter;
}
return 0;
}

int main ()
{
std::list <happy> a;
a.push_back (happy (2));
a.push_back (happy (3));
a.push_back (happy (5));

if (happy * p = findhappy (a, 3))
{
std::cout << "Found a happy object with number "
<< p->number << ".\n";
}
else
{
std::cout << "Didn't find a matching happy object.\n";
}
}

--
Regards,
Buster.
Jul 22 '05 #6
Huibuh wrote:
I start hating RETURN!!!
You'd be screwed without it.
[...]


// What you are about to see is a reconstruction.
// It's also a complete, compilable translation unit.
// Please check "How to post" in the FAQ.

#include <list>
#include <iostream>
#include <ostream>

struct happy
{
int number;
happy (int n = 0) : number (n) { }
};

// //THIS WAS MY LAST TRY
// // *** I hope you can follow my comments.

// happy* findhappy (list<happy> a, int now)
// // *** This copies the argument into the parameter element-
// // *** by-element. We often pass large objects _by reference_
// // *** rather than _by value_ (unless we need a local _copy_

happy * findhappy (std::list <happy> & a, int now)
{

// {
// ptr=a.begin();
// // *** We shan't need this iterator outside this scope
// // *** so we can declare it here:

std::list <happy>::iterat or iter = a.begin ();

// nownumber=ptr.o perator ->();
// *** Whoah there! "happy * nownumber = & * ptr;" would be easier
// *** to read; but wait a minute, we're not going to do it like this.

// while (nownumber->number!=now)
// {
// ptr=ptr.operato r ++(); // *** Just "++ ptr;"
// nownumber=ptr.o perator ->(); // *** Just "nownumber = & * ptr;"
// }
// return nownumber;
// }
// // *** The logic of this loop is all wrong. Get out a pencil
// // *** and paper and work out what would happen if the value
// // *** "now" is not present in a happy object in the list.
// // ***
// // *** The following works OK. We don't need to transform the
// // *** iterator into a pointer until just before we return
// // *** from the function, so that's what I do here.
// // *** I notice that we're simulating a "for" loop here.
// // *** Maybe you can change it for the real thing.

while (iter != a.end ())
{
if (iter->number == now)
return & (* iter );
++ iter;
}
return 0;
}

int main ()
{
std::list <happy> a;
a.push_back (happy (2));
a.push_back (happy (3));
a.push_back (happy (5));

if (happy * p = findhappy (a, 3))
{
std::cout << "Found a happy object with number "
<< p->number << ".\n";
}
else
{
std::cout << "Didn't find a matching happy object.\n";
}
}

--
Regards,
Buster.
Jul 22 '05 #7
happy* findhappy (list<happy> a, int now)


"list<happy > a" is a COPY, how could I forget about the &?!!!
Thanks to all of you, now I'm working on the rest of your tips.

Happy eastern, mine is safed now. :-)

Jul 22 '05 #8
happy* findhappy (list<happy> a, int now)


"list<happy > a" is a COPY, how could I forget about the &?!!!
Thanks to all of you, now I'm working on the rest of your tips.

Happy eastern, mine is safed now. :-)

Jul 22 '05 #9

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

Similar topics

8
410
by: Huibuh | last post by:
I start hating RETURN!!! list<happy> //#include <list>// is searched via iterator (ptr) so long until the argument "number" of the class "happy" is equal now. The function "findhappy" works fine, finds the right pointer "nownumber" with correct values. Behind "return" the memory address remains correct but the values are wrong (a huge negative number and all the same).
3
2348
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
7
2343
by: Fatted | last post by:
I'm trying to learn how to create arrays dynamically. But its just not happening. Have a look at code below and point and laugh where appropriate... First part of program, I'm using an array of pointers, which seems to go ok. I want to then take it one step further and dynamically create the array (pointers to pointers). I try to print out the data again, retrieve the first 3 values (probably by luck), then maybe a pointer value, and then...
69
5550
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
8
2395
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct Employee { char employeeid; /* id of employee*/
17
3241
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
17
2715
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This compiler I use for the Samsung 16 bit Smartcard chips and I want to know if it is compliant with the ANSI standard or if it violates it. Have a look at this super simple example, where the value of b is incorrect:
19
2461
by: mail1779205 | last post by:
I (certainly) hope I know what this function does: char *fun(void){ char *ptr = "Hello World"; return ptr; } It returns a pointer to a string stored somewhere in the memory and is read-only. Does the following function do the same thing and is the
2
35563
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
0
8449
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
8784
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8642
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
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...
0
5666
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
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
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 we have to send another system

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.