0

I am writing the function, getCString which takes in 3 parameters. The first parameter is the user inputted string. The second parameter is the minimum number of characters the string has to be and the third parameter is supposed to be the maximum number of characters allowed for the string. Below is the function which calls getCString: (Note that the first input 'horse' is a test input to show error messages and catch errors)

void test08_getCString(void) {
    char cstringValue[7];

    printf("TEST #8: - Instructions:\n"
           "1) Enter the word 'horse'  [ENTER]\n"
           "2) Enter the word 'BERLIN' [ENTER]\n"
           ":>");

    // You may want to comment the next line if you have not yet created the getInteger function:
    getCString(cstringValue, 6, 6);
    printf("////////////////////////////////////////\n");
    printf("TEST #8 RESULT: ");
    printf("%s (Answer: BERLIN)\n", cstringValue);
    printf("////////////////////////////////////////\n\n");
}

Basically this function wants the user to input a string and accept 6 as the minimum value and 6 as the maximum value for the number of characters allowed.

This is what I have so far:

Note that if the minimum and maximum are the same the error code:

**("Error: String length must be exactly %d chars: ", max)** will appear.

If the minimum and max are different and the user enters a value greater than the maximum. Then this will appear:

**("ERROR: String length must be no more than %d chars.", max)**

If the minimum and max are different and the user enters a value less than the minimum. Then this will appear: **("ERROR: String length must be between %d and %d chars: ", min, max)**

char getCString(char *text[], int min, int max) {
    int i = 0;
    scanf("%s", *text);
    
    while (*text[i] != '0') {
        i++;
    }

    while (min == max && (i > max || i < min)) {
        printf("Error: String length must be exactly %d chars: ", max);
    }

    while (min != max) {
        if (i > max) {
            printf("ERROR: String length must be no more than %d chars.", max);
        }

        if (i < min) {
            printf("ERROR: String length must be between %d and %d chars: ", min, max);
        }
    }
    
    return *text;
}

This is what it looks like in my header file:

char getCString(char*[], int, int);

However when I compile the program, I get this error code: **Exception thrown at 0x7B2C98F1 (ucrtbased.dll) in A1MS1.c.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

this error code is shown on this line:

scanf("%s", *text);

If anyone can explain to me where in my program there is an issue. Whether that be in the header file or if I am not using the pointer syntax correctly, please let me know.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • The declaration of the first parameter does not make a sense. It should be declared like char text[] or char *text – Vlad from Moscow Dec 13 '21 at 21:24
  • `char getCString(char *text[], int min, int max) {` should be `char getCString(char text[], int min, int max) {` shouldn't it? – Jerry Jeremiah Dec 13 '21 at 21:26
  • @ChecksOverStripes Also the function should not input a string. A string should be passed to the function through the first parameter. – Vlad from Moscow Dec 13 '21 at 21:27
  • Does this answer your question? [How to end up with a pointer to 0xCCCCCCCC](https://stackoverflow.com/questions/3618572/how-to-end-up-with-a-pointer-to-0xcccccccc) – BoP Dec 13 '21 at 21:45
  • `scanf("%s", *text);` puts no limit on the size of the input string. If the user types a 10-character string into a 6-character buffer the result is a buffer overflow. – stark Dec 13 '21 at 21:45
  • Suggestion: whether the string is too long or too short, report the acceptable range. There's nothing much more infuriating than being told "must be no more than 16 characters", trying 8 characters, and being told "must be between 12 and 16 characters". Why didn't you tell me that the first time?!!! I routinely have analogous problems with 'acceptable characters in passwords' — they say "at least one special character" and then they say "oh, but you're not allowed to use `<` or `>` or whatever". – Jonathan Leffler Dec 13 '21 at 23:29
  • I think `while (*text[i] != '0') { i++; }` should probably be using `'\0'` instead of `'0'`. I also think `while (min == max && (i > max || i < min)) { printf("Error: String length must be exactly %d chars: ", max); }` is an infinite loop waiting to happen – if only one length is OK and `i` is incorrect, nothing on the loop changes `i`, so it continues running until someone or something gets bored. The `while` should probably be an `if`. Similarly with the `while (min != max) {` loop — also needing an `if`. And you need some sort of loop around your `scanf()`. You need to reset `i` too. – Jonathan Leffler Dec 13 '21 at 23:32

0 Answers0