小编r_g*_*yal的帖子

什么是'前向声明'以及'typedef struct X'和'struct X'之间的区别?

我是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)

有什么区别structtypedef?或者两者都相等,因为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)

c struct typedef

50
推荐指数
4
解决办法
6万
查看次数

为什么我们不能将新字符串分配给数组,而是分配给指针

我试图将字符串重新分配给预初始化的数组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 arrays pointers

3
推荐指数
2
解决办法
2834
查看次数

结构变量不会在c中初始化

我正在尝试运行一个程序,实现一个带有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)

c struct initialization char

2
推荐指数
1
解决办法
229
查看次数

标签 统计

c ×3

struct ×2

arrays ×1

char ×1

initialization ×1

pointers ×1

typedef ×1