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

int main(void) {

   const int NUM_VALS = 20;
   int i;
   int actualInput;
   char userString[actualInput][NUM_VALS];
   int matchCount = 0;

   scanf("%d", &actualInput);

   for (i = 0; i < actualInput; ++i) {
      scanf("%s", userString[i]);
      printf("%s", userString[i]);
   }



   return 0;
}

Output:

b'hellohi\x80\x07@\xd2\x05@\x9a\x16[\xea\xccp\xa6\x15\xf6\x18+\xbf\x87\x8a#\x14)\x05@\xfe\x7f'b'\x92\x1fk\xb3\xfe\x7f\xfe\x7f\x118\x08\xe8\x03\x0eY\x03k\xb3\xfe\x7f\xfe\x7f\xb2Y{\xe8C}8\r\x8b-u{\x8cx86_64'F-8sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin/usr/sbin:/usr/bin:/sbin:/binsbin:/binTF-88tf8RELOAD=/usr/lib/x86_64-linux-gnu/coreutils/libstdbuf.so64-linux-gnu/coreutils/libstdbuf.sols/libstdbuf.soout

I've tried some variations replacing userString[i] with userString in the scanf function. The result is outputting 50,000 inputs of my last string. I don't understand what's happening.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 3
    Currently your program exhibits undefined behavior since you read from `actualInput` before it is initialized. Also, do you mean to use variable-length arrays? – Brian61354270 Mar 19 '20 at 17:55
  • 1
    What do you expect `actualInput` value in `char userString[actualInput][NUM_VALS];` to be? The main problem is there. I'd suggest you reading some intro on memory management in C. – norok2 Mar 19 '20 at 17:55
  • 3
    At the time `char userString[actualInput][NUM_VALS];` is declared, `actualInput` is unitialized garbage, so there's no telliing how much memory you're allocating. Also, NUM_VALS is the max length of each string, so it should be MAX_LENGTH or something more meaningful. – Lee Daniel Crocker Mar 19 '20 at 17:55

2 Answers2

1

The problem is this sequence of code:

int actualInput;
char userString[actualInput][NUM_VALS];
int matchCount = 0; 

scanf("%d", &actualInput);

The first line declares a variable called actualInput but doesn't assign a value to that variable.

The second line declares a variable length array (VLA) using the value in actualInput. Using the value of an uninitialized variable results in undefined behavior, which basically means that after that point in the code, anything can happen. What's likely happening (based on your description of the problem) is that actualInput is either zero, or a small number, so you get an array that's too small to hold your input.

The last line (with the scanf) finally assigns a value to actualInput. You may be thinking that the array will resize itself when actualInput is changed. That definitely does not happen. In C, after a VLA is created, its size cannot be changed.

The solution is simple, rearrange the code so that things are done in the proper order:

int actualInput;
scanf("%d", &actualInput);
char userString[actualInput][NUM_VALS];

int matchCount = 0;

As a side note, you should really do some error checking to make sure that the user inputs a reasonable number, before using that number to create an array. For example

int actualInput;
if (scanf("%d", &actualInput) != 1 || actualInput < 1 || actualInput > 1000)
{
    printf("That is not a valid array size\n");
    return 1;
}
char userString[actualInput][NUM_VALS];
user3386109
  • 34,287
  • 7
  • 49
  • 68
0

you cant declare it as a 2D array then treat it as a normal array . each case should include only one letter but it can't be done automatically , I suggest you add this :

for (i = 0; i < actualInput; ++i) 
{
    gets(stri);
    for (k=0;k<strlen(stri);k++)
    userString[i][j]=stri[j];
}
Aguir
  • 1
  • 1
  • 2
    `gets(stri);` has not been in C since C11. – chux - Reinstate Monica Mar 19 '20 at 20:41
  • it gets the job done , what they at least say at UNI – Aguir Mar 19 '20 at 22:06
  • "it gets the job done" Which means they are teaching you per an old standard of C - not a professional approach to the language. UNI is likely behind the times when it comes to C. See [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/2410359) and [gets() function in C](https://stackoverflow.com/q/4346598/2410359). – chux - Reinstate Monica Mar 19 '20 at 22:48