0

I'm trying to create an integer (maximum four digits to string). Here is my method:

char *ToInt( int Value) { char buffer[4]; sprintf(buffer, "%04d", Value); return buffer; } 

After that the string is separeted to each byte and send it into a 7 segment LCD. The problem is that i'm taking a warning

 warning: (365) pointer to non-static object returned 

and also all of this errors

 C:\Program Files (x86)\Microchip\xc8\v1.45\sources\common\doprnt.c:538: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.45\sources\common\doprnt.c:541: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.45\sources\common\doprnt.c:1259: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.45\sources\common\doprnt.c:1305: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.45\sources\common\doprnt.c:1306: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.45\sources\common\doprnt.c:1489: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.45\sources\common\doprnt.c:1524: warning: (373) implicit signed to unsigned conversion 
7
  • 1
    You return a pointer to buffer which gets destroyed at } Commented Oct 24, 2018 at 6:02
  • 2
    The object buffer is local to ToInt(), i.e. non-static. You're returning a pointer to it. Dereferencing that pointer would be undefined behavior, so the compiler is warning you. Commented Oct 24, 2018 at 6:02
  • 1
    And don't forget that char strings are really called null-terminated byte strings. That null-terminated bit is important, and you need space for that as well. Commented Oct 24, 2018 at 6:03
  • What they^ said, also it seems this function would be better named fromInt() or toString() Commented Oct 24, 2018 at 6:03
  • moreover buffer[4] can't store a 4-character string plus the null terminator Commented Oct 24, 2018 at 6:04

2 Answers 2

1

As it has already been mentioned in comments and other answers, returning a local variable (aka buffer) is something that you shall never do as local variables are destroyed once the function return.

Further you buffer is too small to hold the 4 characters as a string in C requires an extra character to zero terminate the string. So to hold 4 character you'll (at least) need buffer[5]. However, notice that %04d does not ensure that there will be printed exactly 4 characters. A high int value will produce more characters and result in (more) buffer overflow. So you'll need a buffer that can hold a print of the largest (perhaps negative) integer.

So what can you do instead?

You have two options. 1) Use dynamic memory allocation inside the function or 2) let the caller supply the destination buffer to the function.

It may look something like:

#include <stdio.h> #include <stdlib.h> // The maximum number of chars required depends on your system - see limits.h // Here we just use 64 which should be sufficient on all systems #define MAX_CHARS_IN_INT 64 char* intToMallocedString(int Value) { char* buffer = malloc(MAX_CHARS_IN_INT); // dynamic memory allocation sprintf(buffer, "%04d", Value); return buffer; } // This could also be a void function but returning the buffer // is often nice so it can be used directly in e.g. printf char* intToProvidedString(char* buffer, int Value) { sprintf(buffer, "%04d", Value); return buffer; } int main(void) { int x = 12345678; char str[MAX_CHARS_IN_INT]; // memory for caller provided buffer char* pStr = intToMallocedString(x); intToProvidedString(str, x); printf("%s - %s\n", str, pStr); free(pStr); // dynamic memory must be free'd when done return 0; } 

Output:

12345678 - 12345678 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but i'm still getting warning doprnt.c:538: warning: (373) implicit signed to unsigned conversion
@ddd that's another question. Please ask a new question.
0

You are trying to return buffer which has been allocated space on the stack.

Stack gets destroyed as soon as the function returns. So the returned value is now just a dangling pointer.

Also, the null terminator is missing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.