Solved

Gibberish returned when trying to return a char pointer

Posted on 2011-02-24
2
423 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
Comment Utility
Aren't you effectively returning a pointer to a local variable.  Make finalbuff static as a quick sanity check.
0
 

Author Comment

by:JoeD77
Comment Utility
ha that fixed it. Thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now