Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Gibberish returned when trying to return a char pointer

Posted on 2011-02-24
2
Medium Priority
?
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

705 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