【问题标题】:Issue with printing a pointer of array in c在c中打印数组指针的问题
【发布时间】:2022-01-05 08:44:53
【问题描述】:

我现在正面临这个问题,我希望找到澄清和解决方案。 实际上,我的代码运行没有任何问题,只是当我想打印 指向 char 数组的指针,它没有以所需的方式打印。 我应该使用多维数组吗?我很困惑。

我是 c 语言的新手,所以我的代码可能有点乱:>

#include<stdio.h>
#define SIZE 10

int main() {
        int no_of_customers;
        char customer_name[SIZE];
        char* customer_namePtr[SIZE];
    
        printf("enter number of customers you wish to add ");
    
        scanf_s("%d", &no_of_customers);
    
        for (int i = 0, j = 0; i < no_of_customers; ++i, ++j) {
            printf("Enter %d customer's name ", j + 1);
            scanf_s("%9s", &customer_name[i], 10);
            customer_namePtr[i] = &customer_name[i];
        }
        puts("");
    
        printf("customer name's are:\n");
    
        for (size_t i = 0; i < no_of_customers; ++i) {
            printf("%9s", customer_namePtr[i]);
        }
        return 0;
}

【问题讨论】:

  • “它没有以所需的方式打印”不是一个充分的问题描述。编辑问题以提供minimal reproducible example,包括重现问题的示例输入、观察到的输出和所需的输出。确定哪个特定的 printf 没有按您的意愿工作。
  • 我已编辑并尽可能清晰地编写代码!
  • 当我需要打印客户姓名时,我的意思是最后一个 printf
  • 例如,如果我想打印 Ali 和 Zayn 的名字。它将第一次迭代打印为 AZayn,第二次打印为 Zayn。 @EricPostpischil
  • 您的代码只有一个char 数组作为客户名称,即用char customer_name[SIZE]; 定义的数组。所以它只能记住一个名字。在scanf("%9s", &amp;customer_name[i]); 中,您告诉scanf 从位置i 开始将输入读入数组。这就是为什么 Zayn 从位置 1 开始放入数组中,覆盖从位置 0 开始的大部分 Ali。你的班级最近学到了什么?指针? malloc?二维数组?使用最近教授的概念来记住多个名称。

标签: arrays c pointers char


【解决方案1】:
char customer_name[10];
for (int i = 0, j = 0; i < no_of_customers; ++i, ++j)
{
    scanf_s("%9s", &customer_name[i], 10);
    ...
}

您现有的代码可能存在缓冲区溢出。请注意,customer_name 只是一个一维字符数组。在第一次迭代中,它写入零索引,这很好。但在第 10 次迭代中,它会写入 customer_name[9],然后再添加 10 个字符。这是写入customer_name 之外的一些随机内存地址。调试器也应该抱怨,因为正在使用scanf_s,它应该在第二次迭代中检测到内存损坏。

scanf_s的正确用法可以简单写成

scanf_s("%9s", customer_name, 10);

如果要将“字符串”分配给“字符串数组”,那么它应该是

customer_namePtr[i] = customer_name;

但这不会给出期望的行为,因为所有数组元素都指向同一个缓冲区。

相反,您可以声明一个二维数组,即字符串数组,如示例所示。

理想情况下,您应该声明char** customer_name = NULL; 并使用malloc 进行分配,并为customer_name 中的每个元素再次使用malloc 进行分配

#define MAX_NAMESIZE 10
#define MAX_CUSTOMERS 10

int main()
{
    char buffer[MAX_NAMESIZE];
    char customer_name[MAX_CUSTOMERS][MAX_NAMESIZE];

    printf("enter number of customers you wish to add\n");
    int no_of_customers = 2;
    if (scanf_s("%d", &no_of_customers) != 1)
        return 0; //print input error
    if (no_of_customers < 0 || no_of_customers >= MAX_CUSTOMERS)
        return 0; //print input error

    for (int i = 0; i < no_of_customers; i++) 
    {
        printf("Enter %d customer's name ", i + 1);
        fgets(buffer, sizeof(buffer), stdin);
        buffer[strcspn(buffer, "\n")] = 0; //remove the trailing '\n'
        strcpy(customer_name[i], buffer);
    }

    printf("customer name's are:\n");
    for (int i = 0; i < no_of_customers; ++i) 
        printf("%9s\n", customer_name[i]);
    
    return 0;
}

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多