结构体赋值

loveqise 2016-10-10 05:10:04
图中直接给结构体赋值为什么报错!
...全文
191 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-10-11
  • 打赏
  • 举报
回复
Initializers Declarators can specify the initial value for objects. The only way to specify a value for objects of const type is in the declarator. The part of the declarator that specifies this initial value is called the “initializer.” Syntax initializer : = assignment-expression = { initializer-list ,opt } ( expression-list ) initializer-list : expression initializer-list , expression { initializer-list ,opt } There are two fundamental types of initializers: The initializer invoked using the equal-sign syntax The initializer invoked using function-style syntax Only objects of classes with constructors can be initialized with the function-style syntax. The two syntax forms also differ in access control and in the potential use of temporary objects. Consider the following code, which illustrates some declarators with initializers: int i = 7; // Uses equal-sign syntax. Customer Cust( "Taxpayer, Joe", // Uses function-style "14 Cherry Lane", // syntax. Requires presence "Manteca", // of a constructor. "CA" ); Declarations of automatic, register, static, and external variables can contain initializers. However, declarations of external variables can contain initializers only if the variables are not declared as extern. These initializers can contain expressions involving constants and variables in the current scope. The initializer expression is evaluated at the point the declaration is encountered in program flow, or, for global static objects and variables, at program startup. (For more information about initialization of global static objects, see Additional Startup Considerations in Chapter 2.)
loveqise 2016-10-10
  • 打赏
  • 举报
回复
引用 8楼paschen 的回复:
引用 5 楼 loveqise的回复:
[quote=引用 1楼赵4老师 的回复:]编译器不支持。
这个是C99吗?
VS不支持C99标准 像C99中的VLA也是不支持的[/quote]我用三楼方法,{}里面为空报错,里面有参数才行,这是为什么
loveqise 2016-10-10
  • 打赏
  • 举报
回复
引用 3楼_zSir 的回复:
如果是要初始化第二个元素,可以这样写:

#include <stdio.h>
#include <string.h>

typedef struct lens {
    float foclen;
    float fstop;
    char brand[30];
}LENS;

int main(void)
{
    LENS str1[10] = {
        {},
        {},
        {500, 2, "Remarkatar"}
    };

    int i;
    for(i = 0; i < 10; i++) {
        printf("[%d] %.2f, %.2f, %s\n",
                i,
                str1[i].foclen,
                str1[i].fstop,
                str1[i].brand);
    }

    return 0;
}
测试结果:

[0] 0.00, 0.00, 
[1] 0.00, 0.00, 
[2] 500.00, 2.00, Remarkatar
[3] 0.00, 0.00, 
[4] 0.00, 0.00, 
[5] 0.00, 0.00, 
[6] 0.00, 0.00, 
[7] 0.00, 0.00, 
[8] 0.00, 0.00, 
[9] 0.00, 0.00,
我用vs,{}里面为空编译出错,里面
paschen 2016-10-10
  • 打赏
  • 举报
回复
引用 5 楼 loveqise的回复:
引用 1楼赵4老师 的回复:
编译器不支持。
这个是C99吗?
VS不支持C99标准 像C99中的VLA也是不支持的
loveqise 2016-10-10
  • 打赏
  • 举报
回复
引用 4楼AlbertS 的回复:
没有这种写法,要是想初始化第3个元素,后面可以不写,前两个必须补全,参考一下3楼哦
应该是C99吧
loveqise 2016-10-10
  • 打赏
  • 举报
回复
引用 2楼小灸舞 的回复:
没有这种用法
应该是C99吧
loveqise 2016-10-10
  • 打赏
  • 举报
回复
引用 1楼赵4老师 的回复:
编译器不支持。
这个是C99吗?
AlbertS 2016-10-10
  • 打赏
  • 举报
回复
没有这种写法,要是想初始化第3个元素,后面可以不写,前两个必须补全,参考一下3楼哦
_zSir 2016-10-10
  • 打赏
  • 举报
回复
如果是要初始化第二个元素,可以这样写:

#include <stdio.h>
#include <string.h>

typedef struct lens {
    float foclen;
    float fstop;
    char brand[30];
}LENS;

int main(void)
{
    LENS str1[10] = {
        {},
        {},
        {500, 2, "Remarkatar"}
    };

    int i;
    for(i = 0; i < 10; i++) {
        printf("[%d] %.2f, %.2f, %s\n",
                i,
                str1[i].foclen,
                str1[i].fstop,
                str1[i].brand);
    }

    return 0;
}
测试结果:

[0] 0.00, 0.00, 
[1] 0.00, 0.00, 
[2] 500.00, 2.00, Remarkatar
[3] 0.00, 0.00, 
[4] 0.00, 0.00, 
[5] 0.00, 0.00, 
[6] 0.00, 0.00, 
[7] 0.00, 0.00, 
[8] 0.00, 0.00, 
[9] 0.00, 0.00,
小灸舞 版主 2016-10-10
  • 打赏
  • 举报
回复
没有这种用法
赵4老师 2016-10-10
  • 打赏
  • 举报
回复
编译器不支持。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧