开发者

C Programming Help with Int2String method

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!


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);
}


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:

  • C Programming
  • The GNU C Library


You should be returning char* rather than char.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜