tags:

views:

93

answers:

3

I am having trouble with my Int2String method. I am new to C so I am not sure if this would be the correct way to go about it. I'm receiving a few error messages:

error: conflicting types for 'int2String' warning: return makes integer from pointer without a cast

Here is my method:

char int2String(int num, char intStr[10]) {
char itoa(num, intStr[10]);
return itoa;
}

Thanks for any help!

+4  A: 

In your code

char itoa(num, intStr[10]);
return itoa;

tells the compiler that you declare a function called itoa with specific signature, then you want to return (a pointer to) this function. Since you declared the return type as char, the compiler doesn't like this.

It should rather look something like

char int2String(int num, char intStr[10]) {
  char ch = itoa(num, intStr);
  return ch;
}

i.e. call the function itoa with given parameters, store its return value in a local variable of type char, then return it. (The local variable could be inlined to simplify the code.)

Now this still does not make the compiler happy, since itoa, although not part of the standard, is by convention declared to return char*, and to take 3 parameters, not 2. (Unless you use a nonstandard definition if itoa, that is.)

With the "standard" itoa version, this modification should work (in theory at least - I have not tested it :-)

char* int2String(int num, char intStr[10]) {
  return itoa(num, intStr, 10);
}
Péter Török
The return type of `itoa` is `char*` not `char`.
Nikolai N Fetissov
@Nikolai, I have dealt with that in the meantime :-)
Péter Török
I'm still receiving the same error with the compiler. Unable to resolve identifier.
Jogan
This may be due to the absence of itoa in your compiler. Simply use snprintf and go standard.
Matteo Italia
@Jogan, have you #included the header file which declares `itoa` in your specific C environment?
Péter Török
I included the stdlib.h which declares the itoa function.
Jogan
itoa is not C89/C99. Therefore your example is not strict ANSI C.
A: 

You should be returning char* rather than char.

Remy
+1  A: 

Aside from being curious as to why you are wrapping a non-standard function like this, there a couple things wrong with your function.

  1. Your int2String function should return a char * instead of char. As you have it you are saying "int2String returns a character" which I presume is not what you want. In C strings are represented as an array of characters so you should be returning an array which is a pointer to the type of element stored in the array (in this case pointer to char).

  2. Although technically valid, the intStr parameter is written in a rather bizarre manner. The reason this works is because the char intStr[10] notation is equivalent to char *intStr, which is what it really should be.

  3. First of all the itoa function is non-standard function, so it is very much implementation dependent. According to Wikipedia the declaration for itoa is void itoa(int input, char *buffer, int radix). That being said you need to allocate a buffer and fill it using itoa. I'll leave this as a little exercise :)

  4. Finally, your return statement is what is causing the error. Since itoa is a function what you have written says "return the function itoa". This means you are trying to return a function (which can be represented as a pointer) where the compiler is expecting a char.

I would highly recommend you check out The C Book, it provides a great introduction to the language and goes to great lengths to clarify what pointers are and how they are used. Another couple resources worth checking out are:

Dean Pucsek