I am learning C from the book C-Programming: A Modern Approach. Right now I'm going to do array exercises. One exercise is to write a filter that prints an input message in different ways.
I got it so far (see code below), everything works fine as long as the number of characters is more than 44, then it prints random characters. If the number of characters is less than 44, everything works fine. I totally donβt understand why he does it. Where is the problem and what could be the solution?
int i = 0, k = 0; char message[k],ch; printf("Enter a message: "); while(toupper(ch = getchar()) != '\n') { message[k] = ch; k++; } printf("In B1FF-speak: "); for (i = 0; i <= k - 1; i++) { switch(toupper(message[i])) { case 'A': printf("4"); break; case 'B': printf("8"); break; case 'E': printf("3"); break; case 'I': printf("1"); break; case 'O': printf("0"); break; case 'S': printf("5"); break; default: printf("%c", toupper(message[i])); break; } }
int i = 0, k = 0; char message[k],ch;
message (VLA). ( , 1990 C, ββ 1999 ββ 2011 .)
message
k 0, message 0. C . ( , ). undefined.
k
0
VLA , . k . , , , .
44. undefined. , , - , , ""; , , .
, , ( , ), realloc() . (realloc() , . , , , , , .)
realloc()
Source: https://habr.com/ru/post/1569082/More articles:Match string of type java [] with type of oracle - javaHow to run a program on a different screen session? - command-lineLaravel TokenMismatchException session timeout - phpReading passwords using System.console () - javaHow do Rust Arc and Rc types differ from garbage collection? - garbage-collectionHow to find and replace a data string in a C ++ text file - c ++C ++ Finding a string from a text file and updating / writing a string - c ++Transform from nested to flattened XML using XSLT - xmlYou have problems with WebMock, - ruby ββ| fooobar.comHow to parse XML with German umlauts! names? - javaAll Articles