Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I return a string from a function? 1

Status
Not open for further replies.

rsteffler

Programmer
Aug 2, 2001
10
US
I am trying to write a function that will return a string to the main program. Basically, I am building a Perl-type hash in C to use zip code as my key and a string as the stored value. The function below is supposed to search the linked list and return the data string that is associated with the key.

Below is the function that I have now. The variable not_found is a defined as char not_found[] = "NOT_FOUND";. The structure is merely a linked list with 2 character variables and a pointer for the linked list.

How can I return a string to the main program without using global variables? How can I take the value in the main program and assign it to a string for output to a file?

Thanks.

Current Function:
int FindAS(char *zip)
{

struct linked_as_list *current_as_ptr;

current_as_ptr = first_as_ptr;

while ((strcmp(current_as_ptr->zip,zip) != 0) && (current_as_ptr != NULL)) {
current_as_ptr = (*current_as_ptr)->next_ptr;
}

if (current_as_ptr != NULL) {
return (current_ans_ptr->as);
}
else {
return (not_found);
}
}
 
hi,

actually this should be okay and work.
Having a string like:
char not_found[] = "NOT_FOUND";
means you have a variable not_found that is
actually a pointer, pointing to the adress
where "NOT_FOUND" is written.

Thus you would use scanf to store a string in
the variable not_found with the '&'
scanf("%s", & not_found);
means you write the string to the adress of
not_found you earlier declared with
char not_found[12]; or
char *not_found;
not_found = (char *)malloc(12);

When returning not_found, you return a pointer.
By the way, in your function I miss the declaration
for the variable not_found. If you declared it outside
the function, it might be possible that the variable
is not visible in your function.

;)
netcomander
 
I could be wrong but maybe you should look again at your return type, which if you declared it as an int and passing a char string back to main or calling function. Hoping to get certified..in C programming.
 
First you have to change the return type of the function to

char * FindAS(char *zip)

If you want to return a String Constant then you can give it directly by return "Not_Found".

If you want to return the address of char (array) variable then that variable should not be a local variable.

Better thing is you can use dynamic memory manipulation within the FindAS() itself.

Like char * fas;
fas = (char *) malloc(strlen("Not_Found"));
...
return fas.

remember while doing like this you have remove the allocated memory by using free, when that is not necessay ( may be in the main function).

The better way is, you can pass one more char * variable to the FindAS function and you can assign the resultant string to that variable.

Like

int main()
{
char result[20];
FindAS(zip, result);
...
}

void FindAS(char *, char *not_found);
{
....
strcpy(not_found, "String");
}

and there is no need to return any value from the fucntion FindAS so, you can change the return type of the fucntion FindAS to void.

Maniraja S
 
I would still use global variable, but use this way:
char *strMy()
{
static char strLine[256] = "Not Found";
.....
return strLine;
}
In this case strLine is a global and address will be valid when you return from the function.
In this case this function easy to use and I don't have to deallocate memory.
 
This function would be used repeatedly as I read through a file and pass the zip code to the function. The not_found string is only sent back if I can't find the zip code in the linked list. My hope is that most of the zip codes will be found and that I will return a string value pulled from the linked list. If I set a constant in the function, could I use the function over and over?

Here's the basic flow:

Read in zip code information and data point to a linked list.
Read in data file that contains zipcode information. For each record, use find function to determine data point associated with the zip code. Print the information to a file and repeat the read process.

I have this programmed. I just can't get the string information back to the main program. I will try your suggestions this afternoon and see how they go.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top