473,406 Members | 2,387 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,406 software developers and data experts.

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.operator ->();
while (nownumber->number!=now)
{
ptr=ptr.operator ++();
nownumber=ptr.operator ->();
}
return nownumber;
}
Jul 22 '05 #1
8 1874
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.operator ->();
while (nownumber->number!=now)
{
ptr=ptr.operator ++();
nownumber=ptr.operator ->();
}
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>::iterator 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.operator ->();
while (nownumber->number!=now)
{
ptr=ptr.operator ++();
nownumber=ptr.operator ->();
}
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>::iterator 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>::iterator iter = a.begin ();

// nownumber=ptr.operator ->();
// *** 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.operator ++(); // *** Just "++ ptr;"
// nownumber=ptr.operator ->(); // *** 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>::iterator iter = a.begin ();

// nownumber=ptr.operator ->();
// *** 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.operator ++(); // *** Just "++ ptr;"
// nownumber=ptr.operator ->(); // *** 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
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...
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. ...
7
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...
69
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...
8
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...
17
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: ...
17
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...
19
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.