我是C编程的初学者,我知道struct type declaration和typedef struct declaration之间的区别.我遇到了一个答案,说如果我们定义一个结构,如:
typedef struct {
some members;
} struct_name;
Run Code Online (Sandbox Code Playgroud)
然后它就像为匿名结构提供别名(因为它没有标记名称).所以它不能用于前瞻性声明.我不知道"前瞻性宣言"是什么意思.
另外,我想知道以下代码:
typedef struct NAME {
some members;
} struct_alias;
Run Code Online (Sandbox Code Playgroud)
有什么区别struct
和typedef
?或者两者都相等,因为struct_alias是struct NAME的别名?
此外,我们可以声明类似的变量struct
:
struct_alias variable1;
Run Code Online (Sandbox Code Playgroud)
和/或喜欢:
struct NAME variable2;
Run Code Online (Sandbox Code Playgroud)
或者喜欢:
NAME variable3;
Run Code Online (Sandbox Code Playgroud) 我试图将字符串重新分配给预初始化的数组a [],而我所能得到的只是一个错误
main()
{
char a[] = "Sunstroke";
char *b = "Coldwave";
a = "Coldwave";
b = "Sunstroke";
printf("\n %s %s",a,b);
}
Run Code Online (Sandbox Code Playgroud)
[错误]:从类型'char *'分配给类型'char [10]'时类型不兼容。我搜索了此内容,但找不到任何原因。
char a[] = "Sunstroke";
Run Code Online (Sandbox Code Playgroud)
但这没用...
但是如果是指针,则可以像上面的程序一样。
我正在尝试运行一个程序,实现一个带有c ...中结构的函数:
#include<stdio.h>
#include<conio.h>
struct store
{
char name[20];
float price;
int quantity;
};
struct store update (struct store product, float p, int q);
float mul(struct store stock_value);
main()
{
int inc_q;
float inc_p,value;
struct store item = {"xyz", 10.895 ,10}; //## this is where the problem lies ##
printf("name = %s\n price = %d\n quantity = %d\n\n",item.name,item.price,item.quantity);
printf("enter increment in price(1st) and quantity(2nd) : ");
scanf("%f %d",&inc_p,&inc_q);
item = update(item,inc_p,inc_q);
printf("updated values are\n\n");
printf(" name = %d\n price = …
Run Code Online (Sandbox Code Playgroud)