0

I'm receiving an uknown error found in the stdio.h library. Please can someone check it at tell me what is wrong with the code (But I thing it should work fine). P.S. I'm new here so please don't blame me if this is a bad question. Code:

   #include <stdio.h>
    #include <stdlib.h>

    // Conversion from a number to a string
    char *i2s(int broj);

    int main()
    {
        char string1;
        int br, n;
        do
        {
            printf("How much numbers?\n -"), scanf("%d", &n);
        } while (n < 1);
        for (int i = 0; i < n; i++)
        {
            printf("\nbr = "), scanf("%d", &br);
            string1 = *i2s(br);
            printf(" %s", string1);
        }
        free(string1);
        getch();
        return 0;
    }

    char *i2s(int broj)
    {
        char *pom;
        int z=0,br=0,p;
        if (broj < 0)
        {
            z = 1;
            broj = -broj;
        }
        p = broj;
        do
        {
            br++;
            p /= 10;
        } while (p);
        pom = (char *)calloc(br + 1 + z, sizeof(char));
        if (z)
            pom[0] = '-';
        do
        {
            pom[--br + z] = '0' + broj % 10;
        } while (broj /= 10);
        return pom;
}
zrilman
  • 81
  • 2
  • 8
  • `= *i2s(br);`...hmmmmm – Sourav Ghosh Jan 27 '16 at 17:29
  • You can start with getting your code compiling. And ask if there's any warning or error message that you don't understand. Here are the current errors with your code: http://ideone.com/l2YkrA. What error message don't you understand ? – mikedu95 Jan 27 '16 at 17:31
  • I'm getting these errors http://clip2net.com/s/3tvMfMN Sorry but I don't understand none of them @mikedu95 – zrilman Jan 27 '16 at 17:39
  • That's no problem if you understand none of them because SO is precisely for asking what you don't understand, but ignoring compiler messages suggests that you made no effort to understand the cause of your errors. Now you can forget this because you already got an answer, but remember for next time. Because most of the time, you can fix your errors yourself :). – mikedu95 Jan 27 '16 at 17:52
  • For the first error: [one](http://stackoverflow.com/q/10732010), and [two](http://stackoverflow.com/q/20109071). For the second error: You're converting an integer to a pointer without a cast, i.e. you're "free"ing a value, instead of the pointer itself, the note after the second error gives you a hint. Finally, [the third error](http://stackoverflow.com/q/29612201). A little searching goes a long way. –  Jan 28 '16 at 05:42

1 Answers1

0
char string1;
free(string1);

string 1 is not a pointer.

Also with the following section you overwrite string1 everytime you run through the loop. that way you have no pointer to free() the memory that you allocate inside your function unless you do it inside the loop.

for (int i = 0; i < n; i++)
    {
        printf("\nbr = "), scanf("%d", &br);
        string1 = *i2s(br);
        printf(" %s", string1);
    }
Flikk
  • 520
  • 3
  • 10