0

I wrote this function which is supposed to read a string into an array up to the NULL char which represents the end of the string in the line. But it somehow doesn't quite work.

int main(void){
    int MAX = 39;
    char number1[MAX + 1];
    int i;

    read_array(number1, MAX);

    for(i = 0; i <= MAX; i++)
        printf("%c", number1[i]);

    return 0;
}

int read_array(char* number, int size) {
    printf("\nEnter an integer number at a maximum of 39 digits please.\n");

    int result = 0;
    char* i;
    for (i = number; *i != NULL; i++)
        scanf("%c", i);

    return result;
}

No matter how many chars I type, as I print the result it just gives me the first 3 chars and I don't understand why. Any idea? THX

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • 2
    The null terminating character is `'\0'`, not `NULL` (which is the value of a null pointer). They just so happen to both equal zero, though. – Cornstalks Nov 17 '13 at 02:44
  • tried '\0' too with the same result.. –  Nov 17 '13 at 02:51
  • 2
    `'\0'` vs `NULL` isn't your problem; I was just correcting a misnomer. Your problem is that `scanf` doesn't insert the null terminating character, so your check isn't going to work. When the user is done entering the number, do you want them to hit enter/return? If so, check for that character, not `'\0'`. If you want to read stdin until EOF, [you can do that too](http://stackoverflow.com/questions/3764014/how-do-you-read-scanf-until-eof-in-c). – Cornstalks Nov 17 '13 at 02:54
  • Also, you should include more information. For example, you say "it just gives me 3 chars" but you don't say what input you're giving it. Try stepping through it with a debugger. – Cornstalks Nov 17 '13 at 02:55
  • I thought a string is terminated by the '\0' char. thats why im checking for that. (I edited my post for more info) –  Nov 17 '13 at 03:00
  • A proper C-string is terminated by `'\0'`, but this is C, you can do whatever you want. As it currently is, `number`, `number1`, and `i` are *not* strings. They are just pointers to `char`. A "string" is a higher level concept than these data types. – Cornstalks Nov 17 '13 at 03:21

2 Answers2

1

As I said earlier, scanf doesn't null-terminate your strings for you. If you want to read until the user hits enter/return, check for that. You can do that by replacing your for-loop with this do-while-loop:

do {
    scanf("%c", i); // read the data into i *before* the loop condition check
} while (*i++ != '\n'); // check for '\n' (unless you expect the user to
                        // actually type the null character)

@NedStark point about i pointing to junk memory is correct. The data in number1 is never initialized, so it's just filled with junk. Your loop condition (*i != NULL) is checked before the scanf call, which means the loop condition is just checking old, junk data (and not the correct value).

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • makes sense to me but my program crashes when I try it –  Nov 17 '13 at 03:38
  • @ChrisBo: Then you're doing something else that you haven't posted, because I'm running this code in my debugger and it works just fine. Time to learn to use your debugger. – Cornstalks Nov 17 '13 at 03:40
  • Ur right. My bad. Actually Im just using tcc. Got no debugger. Works now: Thank you! –  Nov 17 '13 at 03:43
0

The problem is in your loop

for (i = number; *i != NULL; i++)
    scanf("%c", i);

After incrementing i, i points to the next memory location which contains garbage data because it hasn't been properly initialized. Probably you want to something like:

char c;
i = number;
do
{
    scanf("%c", &c);
    *i = c;
    ++i;
} while (c!='\n')
*i = '\0';
benipalj
  • 704
  • 5
  • 9
  • if I change my condition to `i < number + MAX` it works fine but then it just wont stop reading before reaching 39 chars –  Nov 17 '13 at 03:06
  • ur loop doesnt terminate at all btw –  Nov 17 '13 at 03:18