-1

This is my program for RunLength Decoding. But is is giving output as garbage values. The output in the char *decode_rle(char *a,int length) method is correct, but when it is returned to the main function it is wrong.

#include<stdio.h>
#include<string.h>

char *decode_rle(char *a,int length)
{
    char op[50];
    int i,j,k=0,count=0;
    for(i=0;i<length;i++)
    {
        if( a[i]=='a' || a[i]=='b' || a[i]=='c' || a[i]=='d' || a[i]=='e' || a[i]=='f' || a[i]=='g')
        {
            count = a[i+1] - '0';
            for(j=0;j<count;j++)
            {
                op[k]=a[i];
                k++;
            }
        }
    }
    op[k] = '\0';
printf("\n the decoded string is %s\n",op);
    return op;
}
int main()
{
    int i=0,j,length,count;
    char a[20],*output;
    printf("\n Enter a string ");
    gets(a);
    printf("\n The string you entered is %s",a);
    length = strlen(a);
    printf("\n length is %d\n",length);
    output = decode_rle(a,length);
    i=0;
    while(output[i]!='\0')
    {
        printf("%c",output[i]);
        i++;
   }
    getch();
    return 0;
}
Sarath S Nair
  • 603
  • 2
  • 14
  • 29

3 Answers3

1

the problem is that you are returning a pointer to a local variable of function decode_rle which does not exist anymore once you return from that function.

to start, i suggest you declare op as local variable of main and pass an extra parameter to decode_rle.

char *decode_rle(char *a,int length, char *op)
{
    ....
}

int main()
{
    ...
    char op[50];
   ...

    output = decode_rle(a,length, op);
}

this will work, but...there several other problems in this excercise if you need it for more than a limited proof of concept.

  • you are using fixed lengths for a and p, what happens if the user enters a string longer than 20 in gets? what if the decoded string is larger than 50? (remember that c does not do array bounds checkings, what happens if you write on memory you don't own?)

  • how do you deal with binary 0 ? (remember, strings in c are stored using asciiz convention, what happens if the data your are trying to compress/decompress contains itself binary 0s? how would you change the definition of the buffers to handle this situation?)

elnegro
  • 11
  • 1
0

You are trying to return a variable whose scope is only the function decode_rle. You can't do that and be safe. When you exit the function, the array op and its contents are no longer officially accessible to your program

You should compile with warnings -Wall (and you could add in -Werror to motivate you a bit).

Eregrith
  • 4,263
  • 18
  • 39
0

You return a pointer to op, which is a local variable in decode_rle(). This local variable goes out of scope when the function returns and its memory will get reused, so a pointer to that memory is not very useful.

Instead you could either allocate the required memory with malloc() and return a pointer to that, or add an additional parameter to decode_rle() where you pass a pointer to memory where the result should be written to.

sth
  • 222,467
  • 53
  • 283
  • 367