Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

return string from function

hello evryone

Please can anyone tell me how I can edit this function to return string?

int linklist::find( int id )

{

node *q,*r;
q = p;

if (q->id == id)
{
p = q->link;
// return q->id; //intager value

return q->data; //string value

}

r = q;
while( q!=NULL )
{

if (q->id==id)

{

r->link = q->link;
// return q->id; //intager value

return q->data; //string value
}

r = q;
q = q->link;
}
cout<<"\n CAR BORDNUMBER ["<<id <<"] not Found.\n";
}

Thanks
[789 byte] By [Mansur] at [2007-11-11 8:38:08]
# 1 Re: return string from function
or this function

#include <iostream.h>
#include <stdio.h>

char test(int n)
{
char nm[]={'x'};
return *nm;
}

void main()
{
cout << test(10);
getchar();
}

thank
Mansur at 2007-11-11 21:01:11 >
# 2 Re: return string from function
This code is made in BCB where it's actually useless, but try to understand the code.
The button function is just for testing and showing how it works.

#define MAX_LENGTH 255
class MyStringHandle {
public:
bool operator == (char *p1);
char* operator = (char *p1);
char* operator + (char *p1);
char *c_str( void );
MyStringHandle( void );
~MyStringHandle( void );
private:
char *string;
};

char *MyStringHandle::c_str(void) {
return string;
}

MyStringHandle::MyStringHandle(void) {
string = new char[MAX_LENGTH];
}
MyStringHandle::~MyStringHandle(void) {
delete string;
}
bool MyStringHandle::operator == (char *p1) {
char *p2= this->string;
while ((*(p1++) == *(p2++)) && (*p1) && (*p2));
return !((*p1) || (*p2));
}

char *MyStringHandle::operator + (char *p1) {
char *temp = new char[MAX_LENGTH];
int start = (int)temp;
int tmp = (int)string;
while (*(char *)tmp) *(temp++) = *(char *)(tmp++);
while (*p1) *(temp++) = *(p1++);
*temp = 0;
return (char *)start;
}
char *MyStringHandle::operator = (char *p1) {
char *p2= this->string;
while (*p1) *(p2++)=*(p1++);
*p2 = 0;
return this->string;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
MyStringHandle MyString;
MyString = "Hello, World of strings";
if (MyString == "Hello, World of strings")
ShowMessage(MyString.c_str());
MyString = MyString + " and then some...";
ShowMessage(MyString.c_str());
delete &MyString;
Application->Terminate();
}
ungamed at 2007-11-11 21:02:17 >
# 3 Re: return string from function
or this function

#include <iostream.h>
#include <stdio.h>

char test(int n)
{
char nm[]={'x'};
return *nm;
}

void main()
{
cout << test(10);
getchar();
}

thank

This code is very dangerous. It returns an address of a local array, which has been destroyed by the time the function returns. If you need to return a char array from a function, either allocate it using new[], or use a string object.
Danny at 2007-11-11 21:03:16 >
# 4 Re: return string from function
Thank ungamed , Danny

I am try to create find function to find any node and return value of other field

Example:

struct node
{
int id;
char *data;
node *link;
}*p;

here i want pass id to find function to return data. this is like "search" ... i thank

please if anyone have any idia tell me .

Thank for all active member.
Mansur at 2007-11-11 21:04:15 >
# 5 Re: return string from function
struct node
{
int id;
MyStringHandle data;
node *link;
}*p;

c'mon man, dont disable your brain.. write you own code, and then ask why it doesnt work.
ungamed at 2007-11-11 21:05:21 >
# 6 Re: return string from function
class linklist
{
private:

struct node
{
int id;
char *data;
node *link;
}*p;

public:
linklist();
void append(int,char *num );
void del( int );
void display();
int count();
int find( int id );
~linklist();
};

linklist::linklist()
{
p=NULL;
}

// add function
void linklist::append(int id,char *num)
{

node *q,*t;

if( p == NULL )
{

p = new node;
p->id =id;
p->data = new char[255];
strcpy(p->data, num);
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;

t = new node;
t->id =id;
t->data = new char[255];
strcpy(t->data, num);
t->link = NULL;
q->link = t;
}
}

//delete fiunction
void linklist::del( int id )
{

node *q,*r;
q = p;

if (q->id == id)
{
p = q->link;
delete q;
return;
}

r = q;
while( q!=NULL )
{

if (q->id==id)

{

r->link = q->link;
delete q;
return;
}

r = q;
q = q->link;
}
cout<<"\n CAR BORDNUMBER ["<<id <<"] not Found.\n";
}

char* carname;
int linklist::find( int id )

{

node *q,*r;
q = p;

if (q->id == id)
{
p = q->link;
carname=q->data;
return q->id;
}

r = q;
while( q!=NULL )
{

if (q->id==id)

{

r->link = q->link;
carname=q->data;
return q->id ;
}

r = q;
q = q->link;
}
cout<<"\n CAR BORDNUMBER ["<<id <<"] not Found.\n";
}
int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;

return c;
}

void linklist::display()
{
node *q;

for( q = p ; q != NULL ; q = q->link )
cout << "ID :"<<q->id <<" Car Name : "<< q->data << endl;

}



linklist::~linklist()
{
node *q;
if( p == NULL )
return;

while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}
Mansur at 2007-11-11 21:06:20 >