I wrote the following function. i want to return fbCodeAddr and tmpAddr to the main function.

How to do this?

Function is as follows;

void fbExeCode(int BldCnt)
{
//	for(int BldCnt=0;BldCnt<fbdStepno; BldCnt++)
	{
		char *fbCodeAddr;
		char *tmpAddr=0;
		char *buffer;
		int n;
		fbCodeAddr=(char *)malloc(1024);
		int stack=0;
		int totalPin=0;
		for(int fb=0;fb<fbnodeLst.size();fb++)
		{
			totalPin+=fbnodeLst[fb].fbInfo->fb_str->pinCnt;		
		}
		//printf("BLD:%d PIN:%d\n ", BldCnt,totalPin);
		memcpy(fbCodeAddr, (char *)&BldCnt,4);
		memcpy(fbCodeAddr+4, (char *)&stack,4);
		memcpy(fbCodeAddr+8, (char *)&totalPin,4);	
		tmpAddr = fbCodeAddr+12;
	//	printf("ORDER SIZE:%d\n", orderList.size());
		for(int OL=0; OL<orderList.size();++OL)
		{			
			tmpAddr+=4;
			memcpy(tmpAddr, (char *)&orderList[OL].fbCode,4);
			for(int ip=0; ip<orderList[OL].ipAdr.size();++ip)
			{
				tmpAddr+=4;				
				memcpy(tmpAddr, (char *)&orderList[OL].ipAdr[ip],4);
			//	printf("IP Adress %d:0x%x\n" ,ip+1, orderList[OL].ipAdr[ip]);
			}
			for(int op=0;op<orderList[OL].opAdr.size();++op)
			{
				tmpAddr+=4;				
				memcpy(tmpAddr, (char *)&orderList[OL].opAdr[op],4);
				//printf("O/P Adress :0x%x\n\n", orderList[OL].opAdr[op]);
			}
		}
		printf("Total bytes Used:%d Bytes\n", tmpAddr+4-fbCodeAddr);
		printf("fbcodeAddr:0x%x \n", *(fbCodeAddr+8));
	}
}

Recommended Answers

All 3 Replies

Pass them in as pointers from your calling function.

void fbExeCode(int BldCnt, char* fCodeAddr, char* tmpAddr){

}

A few things to be careful about. If you want to allocate them in fbExeCode, then initialize them to NULL to be safe. Also make sure you don't lose the starting memory location, so you may want to use a temp pointers variable to move through your buffers.

Pass them in as pointers from your calling function.

void fbExeCode(int BldCnt, char* fCodeAddr, char* tmpAddr){

}

A few things to be careful about. If you want to allocate them in fbExeCode, then initialize them to NULL to be safe. Also make sure you don't lose the starting memory location, so you may want to use a temp pointers variable to move through your buffers.

i tried that one, but it was giving error aschange of char ** to char* is not possible

post the line of code that gave you that error. If you want that function to allocate the memory for the pointers then the pointers will have to be passed by pointer, not by value void fbExeCode(int BldCnt, char** fbCodeAddr, char** tempAddr) -- note the two stars. You will have to make a number of other changes in that function, such as *fbCodeAddr= malloc(1024); . Also not that malloc() in C language does not require typecasting. If you put your code in a cpp file then it is being compiled as c++, not C.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.