简体   繁体   English

在此C程序示例中,如何在不使用指针的情况下获取数组输出?

[英]How to get array output without using pointers in this C program example?

Here is the C code. 这是C代码。

#include <stdio.h>
#include <conio.h>

main()
{
    int i,n;
    char a[100];

    printf("Enter number of teams");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
        printf("\nenter team %d",i);
        scanf("%s",a);
    }

    for(i=1;i<=n;i++)
    {
        printf("%s",a[i]);
    }
}

My program is simple, but its crashing. 我的程序很简单,但是崩溃了。 i just want to load in a few names into array and then print them back. 我只想将几个名称加载到数组中,然后将它们打印回来。 I am able to do that but at last line, its crashing. 我能够做到这一点,但最后一行崩溃了。

A char[] is equivalent to a string in C when it is terminated with a NUL character ( \\0 ). char[]以NUL字符( \\0 )终止时,它等效于C中的字符串。 You need to create a char[][] , or an array of char[] s. 您需要创建一个char[][]char[]的数组。

So, an array of strings in C can be defined as: 因此,C语言中的字符串数组可以定义为:

char arr[5][100];
//The 5 is the number of strings, and 100 is the length of a single string.

Also, in C, array indexing starts from 0 , not 1 . 同样,在C中,数组索引从0开始,而不是1

Code: 码:

#include<stdio.h>
int main()
{
    int i,n;
    printf("Enter number of teams");
    scanf("%d",&n);
    char a[n][100];
    for(i=0;i<n;i++)
    {
        printf("\nenter team %d",i);
        scanf("%s",a[i]);
    }
    for(i=0;i<n;i++)
    {
        printf("%s\n",a[i]);
    }
}

You have a few errors in your code 您的代码中有一些错误

  1. main() must return int main()必须返回int

  2. a should be an array of strings, and it's an array of char , you can do it with an array of char arrays, declare it as a应该是一个字符串数组,它是一个char数组,您可以使用char数组数组来完成它,将其声明为

     char a[100][100]; 
  3. Arrays in c are indexed from 0 to n - 1 and not from 1 to n . c中的数组从0索引到n - 1而不是从1索引到n

  4. You must check if the first scanf() worked, furthermore, you must check that n is not too big so this part does that 您必须检查第一个scanf()起作用,此外,您必须检查n是否不太大,以便本部分能够做到这一点

     if (scanf("%d", &n) != 1) return 1; if (n > 100) n = 100; 
  5. You should prevent a buffer overflow, for that you can use the length specifier for scanf() , if your array can store 100 char 's then you need to use 您应该防止缓冲区溢出,因为您可以将长度说明符用于scanf() ,如果您的数组可以存储100个char ,那么您需要使用

     scanf("%99s", a[i]); 

    because you also need the terminating '\\0' . 因为您还需要终止符'\\0'

  6. Your printf() statement is using the wrong specifier, because you are passing a char and the "%s" is specting ac string. 您的printf()语句使用了错误的说明符,因为您传递的是char ,而"%s"则指定了ac字符串。

Try this 尝试这个

#include <stdio.h>

int
main()
{
    int i, n;
    char a[100][100];

    printf("Enter number of teams");
    if (scanf("%d", &n) != 1)
        return 1;
    if (n > 100)
        n = 100;

    for (i = 0 ; i < n ; i++)
    {
        printf("\nenter team %d",i);
        scanf("%99s", a[i]);
    }

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

    return 0;
}

also, even though you are not explicitly manipulating pointers, you can't avoid that completely, because for example printf() takes a pointer parameter for the "%s" specifier, it's just that in this case you don't explicitly pass a pointer, but the i-th array of the array of char arrays will automatically decay to a pointer in 同样,即使您没有显式操作指针,也无法完全避免这种情况,因为例如printf()接受了"%s"说明符的指针参数,只是在这种情况下,您没有显式传递一个指针,但是char数组i-th数组将自动衰减为

printf("%s", a[i]);

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

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