Unable to enter.

Asked 1 years ago, Updated 1 years ago, 382 views

When I ran the following program, I wanted to enter two arrays, but I couldn't enter the second array, and the if statement was executed as it is.
Is there something missing?
please tell me。Thank you for your cooperation.

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

#define N128
int main (void)
{
    char str_input [N];
    char str_new [N];
    
    printf("Key in a letter >>");
    str_input[N] = getchar();
    printf("%d\n", strlen(str_input));
    
    printf("Key in a letter >>");
    str_new[N] = getchar();
    
    if(strcmp(str_input,str_new)==0)
    {
        printf("string str_input and str_new match\n";
    }
    else
    {
        printf("String str_input and str_new do not match\n";
    }
    
    strcpy(str_input, str_new);
    
    if(strcmp(str_input,str_new)==0)
    {
        printf("string str_input and str_new match\n";
    }
    else
    {
        printf("String str_input and str_new do not match\n";
    }
    return 0;
}

c

2022-09-30 21:54

2 Answers

The questioner's code has been substituted beyond the valid range of the array.

#define N128
 int main (void)
 {
     char str_input [N];

char str_input [N]; declares an array of 128 elements of type char, but

str_input[N]=getchar();

In this statement, getchar() replaces one character read from the standard input with str_input[N] or str_input[128].
The suffixes in the C language array begin with zero, so you are substituting the 129th element.
This is a bug because it is outside the array.

str_new has the same problem.

 if(strcmp(str_input,str_new)==0)
    {
        printf("string str_input and str_new match\n";

If you look at this code, we are comparing str_input and str_new strings, so I think you want to enter a string for str_input.
The current code only contains one character entered in str_input, so I think you need to change it to enter a string or repeat a single character entry.


2022-09-30 21:54

Again, I don't know the cause of Segmentation fault and Segmentation fault answers to Segmentation fault questions. Check out the same cause as before.ndexOutOfBounds) indicates an error.

$cppcheck-v --enable=all input_string.c
Checking input_string.c...
Defines:
Undefines:
Included:
Platform: Native
input_string.c:11:12: error: Array'str_input [128] 'accessed at index 128, which is out of bounds. [arrayIndexOutOfBounds]
  str_input[N] = getchar();
           ^
input_string.c:15:10:error:Array'str_new[128]'accessed at index 128, which is out of bounds.[arrayIndexOutOfBounds]
  str_new[N] = getchar();
         ^
input_string.c:12:3:portability:%d in format string(no.1) requirements 'int' but the argument type is 'size_t {aka unsigned long}'. [invalidPrintfArgType_sint]
  printf("%d\n", strlen(str_input));
  ^

The last invalidPrintfArgType_sint is described in printf(3).

printf(3)

Length modifier
   Here, "integer conversion" stands for d, i, o, u, x, or X conversion.

zA following integer conversion responses to a size_torsize_target, or a following n conversion responses to a pointer to a size_target.

In this case, specify unsigned int (%zu) because it is the length of the string.

printf("%zu\n", strlen(str_input));

In addition, getchar() is used to load input, but only one character (1 byte) is loaded, so if you want to load more than one character string, you should use fgets(3).

fgetc(3)

NAME
   fgetc, fgets, getc, getchar,ungetc-input of characters and strings

DESCRIPTION
   fgetc()reads the next character from stream and returns it as an unsigned character cast to an int, or EOF on end of file or error.

getc()is equivalent to fgetc() except that that may be implemented as a macro which values stream more than once.

getchar()is equivalent to getc(stdin).

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s.Reading stops after an EOF or a newline.If a newline is read, it is stored into the buffer by string.


2022-09-30 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.