Solved

Gibberish returned when trying to return a char pointer

Posted on 2011-02-24
2
429 Views
Last Modified: 2012-06-22
Hello,

This code:
char *GetID()
{
	FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "c:\\program files\\example\\example.log" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  buffer = (char*) malloc (sizeof(char)*lSize);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

  result = fread (buffer,1,lSize,pFile);
//  buffer[lSize] = { '\n' };
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  fclose (pFile);
  char * pch;
//  char * nonNull;
  char finalsid[256] = {' \0 '};
  int i = 0;
  char  finalbuff[256] = { '\0' };
  if(strstr(buffer, "0:1:") == NULL && strstr(buffer, "0:0:") == NULL)
  {
	  puts("Not Installed");
	  exit(0);
  }
  if(strstr (buffer,"0:1:") == NULL)
  {
	pch = strstr (buffer,"0:0:");
  }
	else{
	  if(strstr(buffer, "0:0:") == NULL)
	  {
	pch = strstr (buffer,"0:1:");
	  }
  }


  for(i = 0; i < 256; ++i)
  {

	if(pch[i] == '\n')
	{
		
		strncpy((char*)finalbuff, (const char*)finalsid, strlen(finalsid) -1);
		
		break;

	}
	else
	{
		strncat((char *)finalsid, (const char*)pch + i, 1);
		
	}

  }
 
  return finalbuff;

}

Open in new window


when I call for this function from main as so:
int main()
{
char *result;
result = GetID();
return 0;
}

Open in new window


"result" is all gibberish, even though "finalbuff" that is being returned isn't.

So basically I need to know how to return a char pointer properly

Thanks!
0
Comment
Question by:JoeD77
2 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 500 total points
ID: 34972408
Aren't you effectively returning a pointer to a local variable.  Make finalbuff static as a quick sanity check.
0
 

Author Comment

by:JoeD77
ID: 34972445
ha that fixed it. Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question