简体   繁体   English

在C中,如何从多维数组中打印字符串?

[英]In C, how to print a string from a multidimensional array?

I have the following program to take five user entered names and print them out. 我有以下程序来取五个用户输入的名称并打印出来。

I need to ask for each name one by one, then prompt the user to either print the list of names or add another name to the list. 我需要逐个询问每个名称,然后提示用户打印名称列表或在列表中添加其他名称。 The names must be stored in a two dimensional array, though I don't see why it couldn't be done with a regular array. 名称必须存储在二维数组中,但我不明白为什么不能使用常规数组。

My code accepts the names with no issues, but fails to print anything. 我的代码接受没有问题的名称,但无法打印任何内容。 It includes print tests to monitor where the error happens. 它包括用于监视错误发生位置的打印测试。 Test number 6 does not print, so there must be an issue with printf("Name: %s", names[x][y]); 测试编号6不打印,因此printf("Name: %s", names[x][y]);必须存在问题printf("Name: %s", names[x][y]);

What is the error? 错误是什么?

#include <stdio.h>

    int main() {
    int x;
    int y;
    char names[5][51] = {{'\0'},{'\0'}};

    printf("Enter the names: ");
    for (x = 0; x <5; x++) {
        printf("\nPrintTest 1");
        for (y = 0; y < 1; y++) {
        printf("\nPrintTest 2");
            scanf("%50s",&names[x][y]);
        }
    }
    printf("\nPrintTest 3");

    for (x = 0; x < 5; x++) {
        printf("\nPrintTest 4");
        for (y = 0; y < 1; y++) {
            printf("\nPrintTest 5");
            printf("Name: %s", names[x][y]);
            printf("\nPrintTest 6");
        }
    }
}

You do not need the nested loop on y : 你不需要y上的嵌套循环:

char names[5][51];
printf("Enter the names: ");
for (int x = 0; x <5; x++) {
    printf("\nPrintTest 1");
    scanf("%50s", names[x]);
    printf("\nPrintTest 2");
}
printf("\nPrintTest 3");
for (int x = 0; x < 5; x++) {
    printf("\nPrintTest 4");
    printf("Name: %s\n", names[x]);
    printf("\nPrintTest 5");
}
printf("\nDone.\n");

Demo. 演示。

So here's my analysis 所以这是我的分析

Your Mistake: 你的错误:

In fact you declared a 2D array where each xth index is basically the char* . 实际上你声明了一个2D array ,其中每个xth索引基本上都是char*

  • If you use names[x] , it will get the char* pointer at the xth element of names . 如果使用names[x] ,它将在namesxth元素处获取char*指针。
  • If you use names[x][y] it will get the pointer at the xth element and then will access it's yth element which is a character and a character datatype is printed by %c not by %s . 如果使用names[x][y] ,它将获得指向第xth元素的指针,然后将访问它的yth元素,该元素是一个字符,而字符数据类型由%c打印,而不是由%s打印。

Possible Solution: 可能解决方案

If you want to print the arrays character by character, then you need to iterate the inner loop over the size of array which is 51 in your case, and then you can print the array by using %c instead of %s . 如果你想逐个字符地打印数组,那么你需要在你的情况下迭代数组大小为51的内部循环,然后你可以使用%c而不是%s来打印数组。

Or you can print the whole array using %s but in that case inner loop is not required because you will be printing the whole array at a time. 或者您可以使用%s打印整个数组, 在这种情况下不需要内部循环,因为您将一次打印整个数组。

Updated Code: 更新的代码:

Method # 01: 方法#01:

//Iterating over all the char*
for (x = 0; x < 5; x++)
{
        printf("\nPrintTest 4");

        //Use of inner loop - Printing the arrays character by character
        for (y = 0; y < 51; y++)
        {
            printf("\nPrintTest 5");
            printf("Name: %c", names[x][y]);
            printf("\nPrintTest 6");
        }
}

Method # 02: 方法#02:

//Iterating over all the char*
for (x = 0; x < 5; x++)
{
        printf("\nPrintTest 4");

        //Printing the arrays without the loop
        printf("\nPrintTest 5");
        printf("Name: %s", names[x]);
        printf("\nPrintTest 6");
}

Hope its clear now. 希望现在清楚。

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

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