0

I'm creating a program that convert a binary number to a hexadecimal one. This is my function

int cnt(int num)
{
    int cn=0;
    while (num!=0)
    {
        cn++;
        num=num/10;
    }
    return cn;
}
int bintodec(int bin)
{
    int cn=cnt(bin),result,a=1,i;
    int A=0;
    for (i=1;i<=cn;i++)
    {
        A=A+(bin%10)*(a);
        a=a*2;bin=bin/10;
    }
    return A;
}
int *dectohex(int dec)
{
    int cn=cnt(dec),i;
    int A[cn];
    for (i=1;i<=cn;i++)
    {
        A[cn-i]=dec%16;
        dec=dec/16;
    }
    return A;
}

This is my caller:

int main()
{
    int i,x;
    printf("x = ");scanf("%d",&x);
    for (i=1;i<=5;i++)
    {
        if (dectohex(x)[i]<10)
        {
            printf("\t%d",dectohex(x)[i]);
        }
        else if (dectohex(x)[i]==10)
        {
            printf("\tA");
        }
        else if (dectohex(x)[i]==11)
        {
            printf("\tB");
        }
        else if (dectohex(x)[i]==12)
        {
            printf("\tC");
        }
        else if (dectohex(x)[i]==13)
        {
            printf("\tD");
        }
        else if (dectohex(x)[i]==14)
        {
            printf("\tE");
        }
        else if (dectohex(x)[i]==15)
        {
            printf("\tF");
        }
    }
return 0;
}

It worked well together in an empty file, no error when built, and it printed correct results. But just when I add them to my project, errors appear everywhere I called the function dectohex() like in this image building logs. Did I do something wrong???

  • What do you mean by "worked well together in an empty file"? What empty file? How was that "together" with your program? Please provide complete code as a [mcve]. – kaylum May 01 '17 at 22:49
  • 2
    `return A;` at `dectohex` : `A` is Invalid outside the scope of the function with the address of the automatic variable that is local to the function. – BLUEPIXY May 01 '17 at 22:50
  • In addition to what @BLUEPIXY wrote, it seems you have invoked `dectohex` before declaring or defining it. That will result in the compiler assuming the function returns an `int` and hence the errors shown in your log. – kaylum May 01 '17 at 22:50
  • @kaylum I meant when I put them into an external file, build an run, it works well, but just when I put them in my project: functions in bintodex.c and the caller in main.c it comes up with errors. – Lâm Chí Thành May 01 '17 at 22:58
  • Ok, then you may want to be a bit clearer in your question - "empty" file means nothing is in the file. Which does not make sense right? Anyway, looks like you have answers to your question. – kaylum May 01 '17 at 22:59
  • @kaylum and it ran well when I tried returning an `int`, error only appeared when I returned a `*int` hoping it would return an array – Lâm Chí Thành May 01 '17 at 23:03
  • Did you declare the function in a header file? – user253751 May 01 '17 at 23:34

1 Answers1

0

Your function does not return an array . It returns a pointer. The pointer points into an array which stops existing when the function returns. When you try to read through the pointer in the calling code, this causes undefined behaviour.

You need to fix your code so that you do not try to access arrays after they have been destroyed.

M.M
  • 138,810
  • 21
  • 208
  • 365