【发布时间】:2015-08-04 07:37:42
【问题描述】:
我正在尝试使用 scanf 函数将数据放入带有指针的结构数组中。然后我试图打印输出。但是在执行printf 之后,输出没有显示任何值。需要帮助我哪里出错了?
这是我的代码:
#include<stdio.h>
struct estore
{
int pid;
char category[20];
char brand[20];
char model[20];
int price;
}; //can also be 'stock[5];' The below line is not required in such case
struct estore stock[5];
struct estore *sptr=stock;
void main()
{
int j;
for(j=0;j<5;j++)
{
printf("\nEnter Product ID: ");
scanf("%d",&(sptr++)->pid);
printf("\nEnter Product Category: ");
scanf("%s",(sptr++)->category);
printf("\nEnter Product Brand: ");
scanf("%s",(sptr++)->brand);
printf("\nEnter Product Model: ");
scanf("%s",(sptr++)->model);
printf("\nEnter Product Price: ");
scanf("%d",&(sptr++)->price);
}
for(j=0;j<5;j++)
{
printf("\nThe Product ID is %d",sptr->pid);
printf("\nThe Product Category is %s",(*sptr).category);
printf("\nThe Product Brand is %s",sptr->brand);
printf("\nThe Product Model is %s",sptr->model);
printf("\nThe Product Price is Rs.%d/-",(*sptr).price);
sptr++;
}
}
【问题讨论】:
-
欢迎来到 SO。由于以下原因,您的“问题”可能不会得到回答。 1)你不问任何问题。 2)您只是复制粘贴了一些代码并且没有正确格式化(这读起来很不愉快)。 3)您没有正确使用标签:您至少应该为您的问题的编程语言添加一个标签。 4)评论您的代码,为任何愿意帮助您的人提供有用的信息。提问方式请参考this guide。