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!

This is tough one, Need "C" guru brain !! 1

Status
Not open for further replies.

perlguy

Programmer
May 17, 2001
26
US
This is very tough for me, but I did my great attempt to solve but FAILED !! It would be great if someone has good stong alternative for the below C prg. :
I am trying return a file record data from the function file_process() but it does not work properly. I mean everthing is ok inside function, It does print the correct value. But when I say return(cdata); I am getting junk chars along with a
warning: "unction returns address of local variable"

What is wrong with following code?
What is the best way to retun data.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* function prototype */
char file_process(char *flname, int rnd_num);

int main(void)
{
/* variable declaration */
FILE *out_bg;
char *temp, trdata[150];

temp = file_process(&quot;list.txt&quot;, 5);
strcpy(trdata, temp);
printf(&quot;Return Data = %s&quot;, trdata);

return 0;
}

char file_process(char *flname, int rnd_num)
{
/* variable declaration */
FILE *fp;
char cdata[150];
char recflag = 'N';
int rec_number, rnd_number;
time_t t;

srand((unsigned) time(&t));
rnd_number = rand() % rnd_num+1;

if ((fp = fopen(flname, &quot;r&quot;)) == NULL) exit(1);
fscanf(fp, &quot;%d&quot;, &rec_number);

while( !feof(fp))
{
fgets(cdata, 150, fp);
if(rec_number == rnd_number)
{
recflag = 'Y';
break;
}
fscanf(fp, &quot;%d&quot;, &rec_number);
}
fclose(fp);

if(recflag == 'N') exit(1);

if(recflag == 'Y')
{
printf(&quot;SP RANDOM = %d \n&quot;, rnd_number);
printf(&quot;RECNO :: %d \n&quot;, rec_number);
printf(&quot;DATA :: %s \n&quot;, cdata);
}

return(cdata);
}
 
The warning clearly indicates the error. The cdata is local variable [ automatic variable ]. Memory for this variable will be allocated automatically when then control enters into the function and the allocated memory will be automatically removed at the end of the function [ here file_process ].

You are returning the address of the automatic, whose memory will be removed at the end of the function file_process. so, while printing the string in the function main you will not get the required string [ you may get segmentation fault error also ].

You can solve this problem in variety of ways.

1. Declare the variable cdata before the main function and just print it in the main function [ no need of the temp variable] and change the return type of the function file_process to void or int.

2. declare the cdata as a pointer variable in the function file_process and allocate memory for that dynamically.
for example

char *cdata;
cdata = (char *) malloc( sizeof(char) * 150);

and at the end of the function main, just put
free(temp);

3. declare the temp as character array [ or use dynamic memory manipulation ] and pass its address as a argument to the function file_process and change the return type of the function to void or int.

for example:

char temp [150]; // in function main

file_process(&quot;list.txt&quot;, 5, temp); //function call

void file_process(char *flname, int rnd_num, char *cdata) // function definition and no need for the cdata declaration within this function

Regards
Maniraja S







 
u r trying to return a character array and a character array without any subscipt ref. means it is an address. It is not that tough a program its a mandatory warning to be thrown by the compiler since in the prototype u said that u will be returning a character whereas u r trying to return an address of character array.

Check these are smal things but may affect ur programs heavily.

Regards,
SwapSawe.
|-0
 
Thanks for all the suggestion. KUDOS !! It worked I have made the changes acc. to suggestion no. 2 of &quot;smaniraja&quot; , and it DID solved my problem. Appreciated. I really like this forum , it's LIVE here. Good &quot;C&quot; gurus like you guys are here. keep it up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top