[Solved] impacting Strings with pointers [closed]


This

      scanf("%s",kar[1000]);

should trigger a compiler warning, as you pass a char where a char* is expected. You pass the 1001st element of kar (which is out of kar‘s bounds, BTW). In C array indices start with 0 for the 1st element.

To scan into kar, just pass the address of kar‘s 1st element.

      scanf("%s", &kar[0]);

As arrays get decayed to the address of their 1st element when being passed to a function the following statement is equivalent

      scanf("%s", kar);

Please note that the above two statements very well allow the user to enter more characters then kar can hold and with this make scanf() overflow kar, that is write beyond it bounds, invoking undefined behaviour, which might crash your program or whatever …

To avoid this tell scanf() the maximum to scan by doing:

      scanf("%999s", kar);

The maximum for chars a strings can hold in C is always one less then defined as C strings need one more char to have their end marked by a '\0'terminator.

solved impacting Strings with pointers [closed]