Return string from function in C

I have a c function that I want to return a string.

If I print a line before it returns, I see croc_data_0186.idx

If I try to print a string that returns, then I see croc_data_รกโ˜ผ

Can anyone see what I'm doing wrong?

Problem Function:

char* getSegmentFileName(FILE *file, int lineLength, int lineNumber) { char* fileNameString; fseek(file, lineNumber * lineLength, SEEK_SET); char line[lineLength]; fgets(line, lineLength, file); char *lineElements[3]; lineElements[0] = strtok(line, ":"); lineElements[1] = strtok(NULL, ":"); lineElements[2] = strtok(NULL, ":"); fileNameString = lineElements[2]; printf ("getSegmentFileName fileNameString is: %s \r\n", fileNameString); return fileNameString; } 

Call Code:

 int indexSearch(FILE *file, char* value, int low, int high, char* segmentFileName) { ... segmentFileName = getSegmentFileName(file, lineLength, mid); printf ("indexSearch: segmentFilename3 is: %s \r\n", segmentFileName); ... } 
+4
source share
7 answers

You return a pointer to local data, which is not valid after the function returns. You must correctly distribute the string.

This can be done either in the calling function, by providing a buffer to the called function, and it copies the string to the supplied buffer. Like this:

 char segmentFileName[SOME_SIZE]; getSegmentFileName(file, lineLength, mid, segmentFileName); 

and getSegmentFileName :

 void getSegmentFileName(FILE *file, int lineLength, int lineNumber, char *segmentFileName) { /* ... */ strcpy(segmentFileName, fileNameString); } 

Another solution is to allocate memory for the string in getSegmentFileName :

 return strdup(fileNameString); 

but then you should remember the free string later.

+7
source

This is because you are returning a pointer to local. This behavior is undefined.

strtok returns a pointer to the line array of characters. You put this pointer in fileNameString and return to the caller. At this time, the memory inside line becomes invalid: any garbage can be written to it.

To avoid this problem, you must either pass a buffer / length pair for the return value, or use strdup() on the line you are returning. In the latter case, you must remember to free the memory allocated for the returned string, strdup() .

For a related object, you should avoid using strtok because it is not repetitive and can cause problems in multi-threaded environments. Use strtok_r .

+4
source

You are returning a pointer to a local variable that no longer exists when the function returns. For this you need malloc storage and return this. Alternatively, you can allow the caller to enter a buffer that needs to be filled. In any case, the caller is responsible for free to use the memory later.

+3
source

This is because you are returning invalid pointers.

  char* fileNameString; 

is just a pointer.

  char line[lineLength]; 

lives on the stack and populates with fgets() .

  char *lineElements[3]; lineElements[0] = strtok(line, ":"); lineElements[1] = strtok(NULL, ":"); lineElements[2] = strtok(NULL, ":"); 

Here you store pointers in this array. One of them -

  fileNameString = lineElements[2]; 

which you

  return fileNameString; 

then.

Solution would be

  • either malloc has enough space inside the function and copy your line to a new memory block or

  • the caller has a buffer in which you write data.

+2
source

The problem is that you are returning a stack variable lost when the function returns. One way to do this is to use the char * arg function parameter, with enough reserved space, and use it to store all the information and return.

+2
source

The string is a local variable and is deleted at the end of the function.

You should use malloc or strcpy it for the string pointer passed as an argument.

0
source

There are 3 ways to solve this problem.

1) Become a "fileNameString" static

 static char fileNameString[100]; 

2) The subscriber of the function 'getSegmentFileName' must pass the character buffer 'segmentFileName' to the called subscriber ie

 getSegmentFileName(file, lineLength, mid, segmentFileName); 

In this case, you need to change the function arguments

  char* getSegmentFileName(FILE *file, int lineLength, int lineNumber, char *segmentFileName) { ..... strcpy(segmentFileName, fileNameString); // copying the local variable 'fileNameString' to the function argument // so that it wont be lost when the function is exited. return fileNameString; // there is no need to return anything and you can make this function void // in order not to alter ur program I am putting the return also } 

3) This way you can dynamically allocate memory for the NameString file. Dynamic memory is allocated on the heap, and it will not be lost when the function returns. That way you can safely use it in the indexSearch function.

 char* getSegmentFileName(FILE *file, int lineLength, int lineNumber) { char *fileNameString = (char *)malloc(100 * sizeof(char)); // allocate memory for 100 character string ..... return fileNameString; } 

In this case, you need to free the memory indicated by fileNameString using free

0
source

Source: https://habr.com/ru/post/1397617/


All Articles