新手求教一个C语言二维数组初始化的问题

xxxxloli 2016-08-01 10:58:11
我要初始化一个二维数组为0,有以下几种方法:
int arr[4][4] = {};
int arr[4][4] = {{0}};
int arr[4][4] = {0};
第一种和第二种编译器不会报错,而第三种就会出警告,有朋友可以解答一下吗?不胜感激!
...全文
396 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
无语_ 2016-08-13
  • 打赏
  • 举报
回复
大括号初始化比较好用,但是据说速度比较慢,推荐定义完之后直接用memset来初始化,注意如果是非char类型最好只用0和-1来初始化。否则会产生比较大的值
赵4老师 2016-08-09
  • 打赏
  • 举报
回复
仅供参考:
//在堆中开辟一个4×5的二维int数组
#include <stdio.h>
#include <malloc.h>
int **p;
int i,j;
void main() {
    p=(int **)malloc(4*sizeof(int *));
    if (NULL==p) return;
    for (i=0;i<4;i++) {
        p[i]=(int *)malloc(5*sizeof(int));
        if (NULL==p[i]) return;
    }
    for (i=0;i<4;i++) {
        for (j=0;j<5;j++) {
            p[i][j]=i*5+j;
        }
    }
    for (i=0;i<4;i++) {
        for (j=0;j<5;j++) {
            printf(" %2d",p[i][j]);
        }
        printf("\n");
    }
    for (i=0;i<4;i++) {
        free(p[i]);
    }
    free(p);
}
//  0  1  2  3  4
//  5  6  7  8  9
// 10 11 12 13 14
// 15 16 17 18 19
「已注销」 2016-08-09
  • 打赏
  • 举报
回复
报错这种情况我也遇到过,明明是对的,却出错了。在类型前加上一个static就可以了。错误好像是内存问题,编译链接都没出错,只在执行的时候出错。
S_million 2016-08-09
  • 打赏
  • 举报
回复
三个都是一样的,数组规定初始化未赋值部分都会计为0,所以三个都是16个零,没有会报错的,一般初始化好像也没有会报错的,你的报错应该是编译器问题吧
zycxnanwang 2016-08-07
  • 打赏
  • 举报
回复
第三种的二维数组初始化,可以,VS2013上没有警告!
qq_23303427 2016-08-05
  • 打赏
  • 举报
回复
GNC无警告,能得到16个0,啥警告额
赵4老师 2016-08-01
  • 打赏
  • 举报
回复
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.)
xxxxloli 2016-08-01
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
static int arr[4][4];
您好,我是想了解一下这三种写法来初始化是否有问题,为什么int arr[4][4] = {0}会出警告呢?
赵4老师 2016-08-01
  • 打赏
  • 举报
回复
static int arr[4][4];

69,369

社区成员

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

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