• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Returning an array in C

dude8604

Platinum Member
Is there a way to have a function return an array in C? It gives me a syntax error when I try to. I tried returning a pointer to an array, but I can't figure out exactly how to do it. Any help would be greatly appreciated!
 
#include <iostream.h>

char DecimalToHex(int num)[]
{
char hex[] = "";
if (num < 16)
{
if (num < 10)
hex[0] = num + 48;
else
hex[0] = (num-10) + 65;
return hex;
}
else
return hex + DecimalToHex(num/16);
}

int main()
{
int num = 1164;
char hex[] = DecimalToHex(num);
cout << "Decimal: " << num << "." << endl;
cout << "Hexadecimal: " << hex << "." << endl;
return 0;
}
 
I havnt used C in a while but I am pretty sure...
char DecimalToHex(int num)[]
should be something like
char[] Decimal ToHex(int num)
 
Originally posted by: JavaMomma
I havnt used C in a while but I am pretty sure...
char DecimalToHex(int num)[]
should be something like
char[] Decimal ToHex(int num)

That doesn't work. It gives a syntax error.
 
No, you cannot return arrays in C. What you can do, however, is return a pointer to an array. e.g.:

int *foo(int count)
{
int *p = malloc(sizeof(int) * count);
int x;
for (x = 0; x < count; x++)
p[x] = i;
return p;
}

int *pseudoArray = foo(2);
// do something w/ it
free(pseudoArray);

Note that I returned a pointer instead of an actual array. Had I declared an actual array inside of foo I would be unable to return a pointer to it as it would cease to exist outside of the scope of foo (i.e. because, in C vernacular, it would be an automatic variable -- allocated on the stack).

Are you familiar with the syntactic similarities between using pointers and arrays? If so, the above should be immediately obvious. If not, post back for clarification. An array is what? A continguous allocation of memory *on the stack*. As the stack is limited to scope, you need mallocated space for it to persist beyond a given scope.

Originally posted by: JavaMomma
I havnt used C in a while but I am pretty sure...
char DecimalToHex(int num)[]
should be something like
char[] Decimal ToHex(int num)

Your Java roots are shining through 🙂

Arrays in C are declared like: type name[] ...

[edit]I always forget not to use [ i ] 🙂[/edit]
 
Originally posted by: dude8604
I know about how they are similar, but I have never seen/used malloc(). Please explain to me what it does.
malloc is used to dynamically allocate memory. Instead of being created on the STACK, it's created on the HEAP. You the programmer are required to FREE UP memory that's allocated on the HEAP.

Anyway, there's an easier way to accomplish what you're trying to do:

void DecimalToHex(int num, char *hexBuffer)
{
if (num < 16)
{
if (num < 10)
{
hexBuffer[0] = num + 48;
}
else hexBuffer[0] = (num-10) + 65;
}
else DecimalToHex(num/16, hexBuffer+1);

/* Note: the "hexBuffer+1" call above causes DecimalToHex to be passed "hexBuffer" */
/* such that hexBuffer[0] is the next character over to the right. Thus, by virtue of */
/* your recursive algorithm, your hex value will actually be populated RIGHT TO LEFT */
}

int main()
{
/* Define a HEX BUFFER up to 16 characters long (plus 1 character for NULL terminator) */
char hex[16+1];

int num = 1164;

/* NULL out the "hex" storage buffer */
memset(hex, NULL, 16+1);

/* Convert the decimal number to hex. Result gets stored in "hex" storage buffer. */
DecimalToHex(num, hex);

/* Now output the results. Note: cout is a c++ construct. Thus, this code is really ENHANCED C 😀 */
cout << "Decimal: " << num << "." << endl;
cout << "Hexadecimal: " << hex << "." << endl;
return 0;
}

Hit Quote to see the FORMATTED version of the code above.

*EDIT* BTW, there's IMO a major flaw w/ the recursive logic above. Any time DecimalToHex gets called w/ a num value > 16, no data will be written to the passed in hexBuffer at hexBuffer[0]. It will immediately be sent back into DecimalToHex w/ hexBuffer+1 (which is the same thing as the previous call's hexBuffer[1]). To make your logic correct, right after that recursive call to DecimalToHex, you need to populate hexBuffer[0] with SOMETHING./
 
BTW, logic that I know (which works) for converting Decimal to Hex is:

a) Set an initial buffer to ""
b) If num > 15:
[*] num = num / 15
[*] append REMAINDER to buffer
[*] Repeat step b until num <= 15
c) Append num to buffer
d) end

The problem w/ the pseudo-code above is that your HEX # is generated BACKWARDS. You either have to reverse the contents of your buffer, or PREPEND the data to your buffer (i.e. start at the END OF YOUR BUFFER and move backwards). If you do that, you need to track where your HEX VALUE actually begins within your buffer.

Coded in C (working in reverse and tracking the start of the buffer), it would be:

char* DecimalToHex(int num, char *hexBuffer)
{
int remainder = num;
char *startOfBuffer = hexBuffer;

if (num > 15)
{
remainder = num % 16;
startOfBuffer = DecimalToHex(num/16, hexBuffer-1);
}

hex[0] = (remainder < 10) ? remainder+48 : remainder+65;
return startOfBuffer;
}

#define BUF_LEN 16
int main()
{
/* Define a HEX BUFFER up to 16 characters long (plus 1 character for NULL terminator) */
char hexBuffer[BUF_LEN+1], *hex;

int num = 1164;

/* NULL out the "hex" storage buffer */
memset(hex, NULL, BUF_LEN+1);

/* Convert the decimal number to hex. */
/* Result gets stored in "hexBuffer" storage buffer (starting in the last position moving backwards). */
/* Actual Hex Value starting position within buffer gets returned by function */
hex = DecimalToHex(num, hexBuffer+BUF_LEN);

/* Now output the results. */
cout << "Decimal: " << num << "." << endl;
cout << "Hexadecimal: " << hex << "." << endl;

return 0;
}

NOTE: Again, hit Quote to see the formatted code.
 
Back
Top