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
Mike
  • 4,041
  • 6
  • 20
  • 37
ddd
  • 85
  • 2
  • 10
  • 1
    You return a pointer to `buffer` which gets destroyed at `}` – Gaurav Sehgal Oct 24 '18 at 06: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. – DevSolar Oct 24 '18 at 06: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. – Some programmer dude Oct 24 '18 at 06:03
  • What they^ said, also it seems this function would be better named fromInt() or toString() – visibleman Oct 24 '18 at 06:03
  • moreover `buffer[4]` can't store a 4-character string plus the null terminator – phuclv Oct 24 '18 at 06:04
  • Thank you for the answers,is there any other way to convert my int value to string? – ddd Oct 24 '18 at 06:06
  • I will handle numbers bigger than 4 digits but how will convert my integer to string? – ddd Oct 24 '18 at 06:15

2 Answers2

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
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
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.

Sandy
  • 895
  • 6
  • 17