0

I'm trying to build up my own small library to deal with calculations of huge numbers over the limit of int or double. I know there are some useful libraries for this purpose, but I am just having a try to do it myself.

The code below, I cannot find an error and neither can the compiler (gcc of MinGW). It simply does not run. I re-read the code multiple times, but still cannot understand why my computer refuses to run it. I am using Windows 7, by the way. Any help or suggestions would be grateful.

(The code is intended to print 11111............111111111.11111)

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

#define PLUS 10
#define MINUS 11

void myprint(char f[]);
int getlen(char f[]);
void daeip(char f[], char g[]);

/*
(len: the length of the array f[])
f[len - 3]: start of the number; the number part is from f[f[len - 3]] to f[0]
f[len - 2]: number of decimals
f[len - 1]: plus or minus
*/

int main(){
    char f[100], g[100];
    int i;

    for(i=0;i<50;i++){
        f[i] = '1';
    }
    f[100 - 3] = 49;
    f[100 - 2] = 5;
    f[100 - 1] = MINUS;
    daeip(f, g);
    myprint(g);

    return 0;
}

void myprint(char f[]){
    int i;
    int len = getlen(f);

    if(f[len - 1] == MINUS){
        printf("-");
    }
    for(i = f[len - 3]; i >= 0; i--){
        printf("%s", f[i]);
        if(i == f[len - 2]) printf(".");
    }
}

int getlen(char f[]){
    int len = sizeof(f)/sizeof(f[0]);
    return len;
}

void daeip(char f[], char g[]){
    int flen = getlen(f);
    int glen = getlen(g);
    int i;
    for(i=0; i <= f[flen - 3]; i++){
        g[i] = f[i];
    }
    g[glen - 3] = f[flen - 3];
    g[glen - 2] = f[flen - 2];
    g[glen - 1] = f[flen - 1];
}
  • 9
    Note: `int len = sizeof(f)/sizeof(f[0]);` does not do what you intend. Note2: the f[] array may not be null-terminated. – wildplasser Jul 06 '14 at 18:40
  • You should learn how to use a debugger, just reading the code is often not enough to find bugs. – Étienne Jul 06 '14 at 18:44
  • What @wildplasser means is that when you pass an array to a function, it becomes a pointer to the first element of the array. You need to pass the number of elements to the function as well. It may not fix everything, but it should help a lot with debugging. –  Jul 06 '14 at 18:48
  • 3
    Welcome to SO. For a question like this, it would be useful to clarify what "does not run" means - does it indicate the program starts but does not output anything, or it crashes, or something else? – mc110 Jul 06 '14 at 19:10
  • 1
    You're overlooking that `char f[]` in a function parameter list [actually means](http://stackoverflow.com/a/22677793/1505939) `char *f`. Arrays cannot be passed by value. There is no way for `getlen` to know the original length of `main`'s `f[]`. – M.M Jul 06 '14 at 21:57

1 Answers1

1

You are using %s in the printf statement, which expects a pointer to a string (char array) as the argument. You should use %c. which expects a char as an argument. In other words, change:

printf("%s", f[i]); to printf("%c", f[i]);

Details:

char[] test = "This is a test";
char firstCharacter = test[0];// 'T'

// The following will print out: This is a test
printf("%s", test);
// The following will be unpredictable, and may cause a runtime error. (even though it compiles)
printf("%s", firstCharacter);
// The following will print out 'T'
printf("%c", firstCharacter);

I'd also recommend creating a struct or class, so that you can separate the length, decimal location, and sign from the actual number.

jazeee
  • 492
  • 5
  • 8