简体   繁体   English

为什么我的数组中没有打印字符串?

[英]Why are the strings not printing in my Array?

#include<stdio.h>
int main()
{
    char arr[100], i;
    int n;

    printf("Enter the size of the Array: ");
    scanf("%d", &n);

    printf("\nEnter the Strings in the Array\n");
    for(i=0; i<n; i++)
    {
        scanf("%s", &arr[i]); 
    }

    for(i=0; i<n; i++)
    {
        printf("%s", arr[i]);
    }
    return 0;
} 

Why does the execution of this code just stop after taking the input of string variables?为什么在输入字符串变量后这段代码的执行就停止了? Why isn't it printing the string in array?为什么不在数组中打印字符串?

You are overwriting the same string every time but starting one deeper in the array on every iteration.您每次都覆盖相同的字符串,但在每次迭代时在数组中更深地开始一个。 You can see the problem more easily when the array has a size of 4.当数组的大小为 4 时,您可以更容易地看到问题。

  char arr[4], i;
  int n = 4;

  printf("\nEnter the Strings in the Array\n");
  for(i=0; i<n; i++)
  {
      scanf("%s", &arr[i]); 
  }

Now the first time when you enter a string more than 3 characters your application will fault.现在,当您第一次输入超过 3 个字符的字符串时,您的应用程序会出错。 The second time more than 2 characters because you start in the same string +1 byte.第二次超过2个字符是因为你从同一个字符串开始+1个字节。 The address of arr + 1. arr + 1 的地址。

The third time only 1 bytes is still valid, etc.第三次只有 1 个字节仍然有效,等等。

Change your array into something like char arr[100][100];将您的数组更改为 char arr[100][100]; Then you have 100 char arrays of array of 100 big.然后你有 100 个大数组的 100 个字符 arrays。 Also remove ampersand from the scanf as at that point arr[i] already is an address.还要从 scanf 中删除与符号,因为此时 arr[i] 已经是一个地址。

scanf("%s", arr[i]);

Same as:如同:

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

Source:资源:

#include<stdio.h>
int main(){
    //stores the size of the array
    int n;

    printf("Enter the size of the Array: ");
    scanf("%d", &n);

    //n represents number of strings you want to store
    char arr[n][100], i;
    printf("\nEnter the Strings in the Array\n\n");
    for(i=0; i<n; i++){
        scanf("%s", arr[i]); 
    }

    printf("\nOutput:\n\n");
    for(i=0; i<n; i++){
        printf("%s\n", arr[i]);
    }
    return 0;
} 

Output: Output:

$ gcc array.c && ./a.out 
Enter the size of the Array: 3

Enter the Strings in the Array

first
second
third

Output:

first
second
third

If you want to scan spaces also:如果您还想扫描空间:

Source:资源:

#include<stdio.h>
int main(){
    //stores the size of the array
    int n;

    printf("Enter the size of the Array: ");
    scanf("%d", &n);

    //n represents number of strings you want to store
    char arr[n][100], i;
    printf("\nEnter the Strings in the Array\n\n");
    for(i=0; i<n; i++){
        //flushs the previous buffer
        getchar();
        //It will read spaces now
        scanf("%[^\n]s", arr[i]); 
    }

    printf("\nOutput:\n\n");
    for(i=0; i<n; i++){
        printf("%s\n", arr[i]);
    }
    return 0;
} 

Output: Output:

$ gcc array.c && ./a.out 
Enter the size of the Array: 3

Enter the Strings in the Array

first string
second 
third string...

Output:

first string
second
third string...

The line线

char arr[100];

will only allocate space for a single string of up to 100 characters (99 characters plus the terminating null character).只会为最多 100 个字符(99 个字符加上终止 null 字符)的单个字符串分配空间。 If you want to allocate space for 100 strings of 100 characters each, you should write this instead:如果要为 100 个字符串(每个字符串 100 个字符)分配空间,则应改为:

char arr[100][100];

Another problem is that using scanf with the %s conversion format specifier will only read a single word, not a whole line of input.另一个问题是使用带有%s转换格式说明符的scanf只会读取一个单词,而不是一整行输入。 If you want to read a whole line of input instead, you should use the fgets function instead.如果您想读取整行输入,则应改用fgets function。

Here is an example program:这是一个示例程序:

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

#define MAX_STRINGS 100
#define MAX_STRING_LENGTH 100

int main( void )
{
    char strings[MAX_STRINGS][MAX_STRING_LENGTH];
    int n;
    int c;

    //read number of strings
    printf( "Enter the number of strings: " );
    if ( scanf( "%d", &n ) != 1 )
    {
        printf( "input error!\n" );
        exit( EXIT_FAILURE );
    }

    //verify that number of strings is acceptable value
    if ( ! ( 0 <= n && n <= MAX_STRINGS ) )
    {
        printf( "Input must be between 0 and %d.\n", MAX_STRINGS );
        exit( EXIT_FAILURE );
    }

    //discard remainder of input line
    do
    {
        c = getchar();
    }
    while ( c != EOF && c != '\n' );

    //input all strings
    for ( int i = 0; i < n; i++ )
    {
        //prompt user for input
        printf( "Enter string #%d: ", i + 1 );

        //attempt to read one line of input
        if ( fgets( strings[i], sizeof *strings, stdin ) == NULL )
        {
            printf( "input error!\n" );
            exit( EXIT_FAILURE );
        }

        //remove newline character from input
        strings[i][strcspn(strings[i],"\n")] = '\0';
    }

    //output all strings
    printf( "\nOutput:\n\n" );
    for( int i = 0; i < n; i++ )
    {
        printf( "%s\n", strings[i] );
    }
}

This program has the following behavior:该程序具有以下行为:

Enter the number of strings: 3
Enter string #1: This is a test.
Enter string #2: This is another test.
Enter string #3: This is yet another test.

Output:

This is a test.
This is another test.
This is yet another test.

According to your code, you are trying to read an array of characters.根据您的代码,您正在尝试读取一个字符数组。

For reading a single character in C we should use the %c specifier.要读取 C 中的单个字符,我们应该使用%c说明符。

Suppose n=5 we can have 4 characters and implicit added '\0' which makes a string.假设n=5我们可以有 4 个字符并隐式添加'\0'来生成一个字符串。

#include <stdio.h>
#include<stdio.h>
int main()
{
    char arr[100], i;
    int n;

    printf("Enter the size of the Array: ");
    scanf("%d", &n);

    printf("\nEnter the Strings in the Array\n");
    for(i=0; i<n; i++)
    {
        scanf("%c", arr[i]); 
    }

    for(i=0; i<n; i++)
    {
        printf("%c", arr[i]);
    }
    return 0;
} 

Or an array of strings example.或字符串数组示例。

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


#define NUM_STRINGS 10

int main(){
    char *arr3[NUM_STRINGS] = { "first string",
                                "second string",
                                "third string",
                                "fourth string",
                                "fifth string" };
    for (int i = 0; i < NUM_STRINGS; ++i) {
        printf("%s, ", arr3[i]);
    }
    printf("\n");
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM