Hello,

I am trying to pass in a long int value and return an array of pointers which is processed in a function that converts long to hex. The array doesn't seem to return from the function. I get an error of the type " type mismatch in redeclaration of long2hex". Please do give me pointers on how to go about the issue.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main()
{
	long a;
	char *b[5];
	a=229605;
	*b=long2hex(a);
	printf("\n%s\n",b[0]);
	printf("%s",b[1]);
}
char *long2hex(long c)
{
	int i,rem;
	char hex_val[8],hex_valr[8],*ptr,*ptr1,temp,temp0,temp1,temp4,temp5;
	char *putval[5];
	i=0;
	while(c>0)
	{
		rem=c%16;
		c=c/16;
		if(rem>9)
		{
			hex_val[i]=(char)rem+55;
		}
		else
		{
			hex_val[i]=(char)rem+48;
		}
		i++;
	}
	hex_val[i]='\0';


      //	printf("\n %ld",fchksz);
	printf("\n %s",hex_val);

	ptr=hex_val;
	ptr1=hex_valr;
	ptr+=strlen(hex_val)-1;
	for (i=0;i<strlen(hex_val);i++)
	{
		*ptr1=*ptr;
		ptr1++;
		ptr--;
	}
	hex_valr[5]='\0';
	printf("\n %s",hex_valr);
	for(i=strlen(hex_valr);i>0;i--)
	{
		temp=hex_valr[i];
		hex_valr[i]=hex_valr[i-1];
		hex_valr[i+1]=temp;
	}
	hex_valr[0]='0';
	printf("\n");
	for (i=0;i<strlen(hex_valr);i++)
	{
		printf(" %c",hex_valr[i]);
	}
	printf("\n");
	temp0=hex_valr[0];
	temp1=hex_valr[1];
	temp4=hex_valr[4];
	temp5=hex_valr[5];
	hex_valr[0]=temp4;
	hex_valr[1]=temp5;
	hex_valr[4]=temp0;
	hex_valr[5]=temp1;
	printf("\n");
	for (i=0;i<strlen(hex_valr);i++)
	{
		printf(" %c",hex_valr[i]);
	}
	sprintf(putval[0],"%c%c",hex_valr[0],hex_valr[1]);
	sprintf(putval[1],"%c%c",hex_valr[2],hex_valr[3]);
	sprintf(putval[2],"%c%c",hex_valr[4],hex_valr[5]);
	sprintf(putval[3],"00");
	printf("\n");
	printf("%s ",putval[0]);
	printf("%s ",putval[1]);
	printf("%s ",putval[2]);
	return (char*)putval;
 }

Recommended Answers

All 6 Replies

two problems:

1) You have to prototype that function -- meaning you have to declare it before using it. Add this somewhere above function main() char *long2hex(long c) 2) line 11 is incorrect: it should be this: b[0]=long2hex(a); But why did you declar b as an arry of 5 character pointers when all you needed is one?

Thank you very much for the prompt reply. It does store the element "putval[0]" in "b[0]", but how should i approach, if I want to return the whole "pulval" array into "b"

Thank you very much for the prompt reply. It does store the element "putval[0]" in "b[0]", but how should i approach, if I want to return the whole "pulval" array into "b"

I'm not sure what you're trying to do - seems awfully convoluted for a "long to hex" conversion. So at this juncture, I'll just add to Ancient Dragon's list of issues you have:

1) Never ever ever ever use void main(). At the very least declare the main function as:
int main() or
int main(void).

2) You should include the string.h header file for the strlen() function.

3) Your long2hex function is returning the address of a local variable - this is going to give you grief.

And as AD mentioned in his post, why do you want to return an array of 5 pointers?

thank you for the suggestions. i will take care with regards to main() declaration. The reason I have used return array of 5 pointers is because the I want to split the hex value into separate pairs. E.g. hex value of E58003 that is generated i'd like to return it as putval[0]=E5 , putval[1]=80, putval[2]=03 and so on. So I am trying to return a pointer array to its counterpart in the main function, where I can then use these generated values.

1) Never ever ever ever use void main(). At the very least declare the main function as:
int main() or
int main(void).

I hope the OP has read the first word: 'never', otherwise it could have turned out else :P

I suppose, my code isn't very clear. Perhaps that's why i'm struggling. My intention is to return a pointer array from the long2hex function into the main(), so that the values in the array can be accessed by another array in main(). I hope this makes sense.

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.