473,847 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A function that returns a pointer...??

Hi!

I am trying to write a function that goes through an array of
objects(class Student) and checks if
object name(student name)matches search string(char *). I want the
function to return a pointer to the object, if a match is found.

This is my code so far...(am I even close? :-))

//I want to return a ponter of type student
Student *List::find(cha r *chName) //chName is the name i want to look
for
{
//Check whole array
for(int i=0;i<100;i++)
{
Student *temp;//Create a temp pointer
temp = MyArray[i]; //Add current array value(object) to temp

char *name;//Create a char pointer
name = temp->get_name();//Get object name (public method of
Student)

if(name == chName)//Compare names
{
return temp; //return pointer to object if found
}
}
return NULL; //return NULL if it was not found
}
A correct call would be ...???
Student *s = find(NameToFind );
if(s!=NULL)
//do something....
Thx,
Tommy
Jul 22 '05 #1
5 1642

"Tommy Lang" <mu*****@yahoo. se> wrote in message
news:78******** *************** ***@posting.goo gle.com...
Hi!

I am trying to write a function that goes through an array of
objects(class Student) and checks if
object name(student name)matches search string(char *). I want the
function to return a pointer to the object, if a match is found.

This is my code so far...(am I even close? :-))

//I want to return a ponter of type student
Student *List::find(cha r *chName) //chName is the name i want to look
for
{
//Check whole array
for(int i=0;i<100;i++)
{
Student *temp;//Create a temp pointer
temp = MyArray[i]; //Add current array value(object) to temp

char *name;//Create a char pointer
name = temp->get_name();//Get object name (public method of
Student)

if(name == chName)//Compare names
Means: if (char * == char *)

That would be true if both strings WERE the same, I mean:
char name[] = "word";

char *test = name;

if (test == name) { //true

You probably want to use strcmp, look it up.

Or better yet, use std:string

if(strcmp(name, chName) == 0) { //Compare names

Watch out for whitespace too.

{
return temp; //return pointer to object if found
}
}
return NULL; //return NULL if it was not found
}



A correct call would be ...???
Student *s = find(NameToFind );
if(s!=NULL)
//do something....
Thx,
Tommy

Jul 22 '05 #2
mu*****@yahoo.s e (Tommy Lang) wrote in message news:<78******* *************** ****@posting.go ogle.com>...

Your code seems to be okay other than the name comparision. You should
use strcmp instead of using a double equals operator. But I have a few
suggestions to improve your code: Follows
Hi!

I am trying to write a function that goes through an array of
objects(class Student) and checks if
object name(student name)matches search string(char *). I want the
function to return a pointer to the object, if a match is found.

This is my code so far...(am I even close? :-))

//I want to return a ponter of type student
Student *List::find(cha r *chName) //chName is the name i want to look
When you are using C++, it's always better to use std::string than
using char* . std::string is a container of char* and provides you
lots of facilities and will free you from dealing with the pointer
stuff.
for
{
//Check whole array
for(int i=0;i<100;i++) Where did you get the upper limit of the interation; 100, from? Are
you limiting your list to contain just 100 students? I guess this
problem is because of the usage of arrays (I guess the below MyArray
stuff). Here goes the second suggestion: Always prefer using a
std::vector or anyother container rather than using arrays. This is a
strong suggestion. If you would have used a vector like
std::vector<Stu dent*> m_vecStudents, you can get the size of the
vector using vector.size(). This would help you in not letting an
upper limit to your List size. {
Student *temp;//Create a temp pointer
temp = MyArray[i]; //Add current array value(object) to temp

char *name;//Create a char pointer
name = temp->get_name();//Get object name (public method of
Student)

if(name == chName)//Compare names Use strcmp for the comparision. If you use std::string s, then you can
use the double equals operator for the comparision. {
return temp; //return pointer to object if found
}
}
return NULL; //return NULL if it was not found
}
A correct call would be ...???
Student *s = find(NameToFind );
if(s!=NULL)
//do something....
Thx,
Tommy

HTH,
Kalyan
Jul 22 '05 #3

"c wood" <re******@veriz on.net> wrote in message
news:4H******** ********@nwrddc 01.gnilink.net. ..

[SNIP]
{
//Check whole array
for(int i=0;i<100;i++)
{
Student *temp;//Create a temp pointer
temp = MyArray[i]; //Add current array value(object) to temp
This does not add the value to temp but causes the pointer temp to point to
the i'th element of MyArray.

char *name;//Create a char pointer
name = temp->get_name();//Get object name (public method of
Student)

if(name == chName)//Compare names


Means: if (char * == char *)

That would be true if both strings WERE the same, I mean:


Just to get things straight. This were true if the pointers were the same
and not the strings!

[SNIP]

Regards
Chris
Jul 22 '05 #4
Ever heard of map? You're in a C++ newsgroup, so the best advice I can
give you is to go for map.

It's easier too...

Here's an example...

#include <map>
#include <string>
#include <iostream>

using namespace std;
map<string,stri ng> data;
void AddData(string Key, string Value)
{
data[Key] = Value;
}

void RemoveData(stri ng Key)
{
data[Key] = "";
}

bool Search(string KeyToFind)
{
if (data[KeyToFind] != "")
return true;
return false;
}

int main()
{
...
...
//use those functions
...
...

return 0;
}

-

It's really simple. Check it out. Of course you can have whatever kind
of map you want. For example map<string,int> (empty field value is 0),
map<int,int> (same empty field value) etc. etc.

-cmad
Jul 22 '05 #5

"Chris Mantoulidis" <cm****@yahoo.c om> wrote in message
news:a8******** *************** ***@posting.goo gle.com...
Ever heard of map? You're in a C++ newsgroup, so the best advice I can
give you is to go for map.

It's easier too...

Here's an example...

#include <map>
#include <string>
#include <iostream>

using namespace std;
map<string,stri ng> data;
void AddData(string Key, string Value)
You might want to change the string parameters to const string& to avoid
unnecessary copies! This applies to all of these functions.
{
data[Key] = Value;
}

void RemoveData(stri ng Key)
{
data[Key] = "";
}


I'm not sure this is very useful because what do you need the key for if
there is no data attached to it?

[SNIP]

Regards
Chris
Jul 22 '05 #6

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

Similar topics

5
2159
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the called function, I am using p1->find(1) which is returning a valid iterator and not going to the end. I am returning p1 from the called function. But in the calling function, find(1) is going to the end, i.e unable to find the key 1, which was...
10
2354
by: Dirk Vanhaute | last post by:
I have only small knowledge of c++, but I would like to compile the example in http://support.microsoft.com/kb/q246772/ HOWTO: Retrieve and Set the Default Printer in Windows I included "#include <Windows.h>" at the start, and the following goes wrong : BOOL DPGetDefaultPrinter(LPTSTR pPrinterName, LPDWORD pdwBufferSize) { ....
8
29076
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If possible how could one pass these seven values in the array to a function that would check the values. I tried return y but it didn't work
11
4745
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to create content to put at the address pointed to by f. In particular, I'd like to avoid/replace the memcpy line. Possible application (inspired by Paul Graham, "ANSI Common Lisp",
17
2219
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
4
403
by: ranjmis | last post by:
Hi all, I have come across a piece of code wherein a function returns a function pointer as it seems to me but not very clear from the prototype. As shown below - although return type is void - (*master())() returns a function pointer some_func & meanwhile calls that function and prints the message "hi from some_func"
17
3275
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);
3
3663
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
4
1865
by: Jeffrey Spoon | last post by:
Hello, I am trying to make a simple function that returns a pointer to another function. In my header file I've declared my function pointer: void (*pStateFunction) (void); //assume the function pointed to returns void and the actual function that returns the pointer: void* getState(CString myString);
11
1965
by: Antoninus Twink | last post by:
What's the correct syntax to define a function that returns a pointer to a function? Specifically, I'd like a function that takes an int, and returns a pointer to a function that takes an int and returns a string. I tried this: gchar *(*f(gint n))(gint) { /* logic here */ }
0
9732
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10988
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10712
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
10344
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...
1
7886
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5723
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...
1
4536
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
2
4126
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.