初始化用C二维字符数组

初始化用C二维字符数组

本文介绍了初始化用C二维字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似:

我需要帮助初始化一个二维字符数组,将所有被初始化到某一值(在这种情况下,'0')。我已经尝试了许多不同的方法,我拉我的头发。请让我知道我做错了。这code不起作用。谢谢!

 的char ** init_array(){
        炭newarray [5] [10];
        INT I,J;
        对于(I = 0; I&小于5;我++){
                为(J = 0; J&小于10; J ++){
                        newarray [I] [J] ='0';
                }
        }
        返回newarray;
}
字符数组** = init_array();

我从GCC中得到的错误,当我尝试编译:

  test.c的:在函数'init_array:
test.c以12:2:警告:从回报不兼容的指针类型[默认启用]
  返回newarray;
  ^
test.c以12:2:警告:函数返回局部变量的地址[-Wreturn-本地地址]
test.c的:在顶层:
test.c以14:1:错误:初始元素不是常数
 字符数组** = init_array();

它应该是这样吗?

 字符newarray [5] [10];
焦炭** init_array(){
        INT I,J;
        对于(I = 0; I&小于5;我++){
                为(J = 0; J&小于10; J ++){
                        newarray [I] [J] ='0';
                }
        }
        返回newarray;
}
字符数组** = init_array();


解决方案

我觉得图片的帮助。这里是字符newarray [5] [10] 。它是一个单存储块组成的10个字符的阵列,并且这些五个数组。
你可以只用一个 memset的通话将其清除。

下面是的char **数组。它说阵列是一个指针。
这是什么一个指针?
的指针的一个字符。

请记住指针运算。
如果阵列是恰好指向指针的指针,
那么(*数组)等于数组[0] ,那就是指针数组点。

什么是数组[1]
这是阵列指向数组中的第二个指针

什么是阵列[0] [0]
它是由阵列指向第一个指针的第一个字符指出。

什么是数组[I] [J]
它是的Ĵ的日的字符的 I 的日指针阵列点。

那么,如何是 newarray 及相关阵列?结果
简单。
newarray [I] [J] Ĵ的日的字符的 I 的次的子阵 newarray

因此​​,在这个意义上说,它就像阵列,但没有下的所有指针。

有什么区别?
好吧,阵列的缺点是你必须一块建立起来,一块。
OTOH,好处是你可以把它一样大,只要你想,当你建立起来。
它不必事先已知的固定尺寸内活

清除泥?

Similar to this question: 2d array, using calloc in C

I need help initializing a 2D char array that will all be initialized to some value (in this case '0'). I have tried many different methods and I am pulling my hair out. Please let me know what I am doing wrong. This code doesn't work. Thanks!

char** init_array() {
        char newarray[5][10];
        int i, j;
        for (i = 0; i < 5; i++) {
                for (j = 0; j < 10; j++) {
                        newarray[i][j] = '0';
                }
        }
        return newarray;
}
char **array = init_array();

The errors I get from gcc when I try to compile:

test.c: In function ‘init_array’:
test.c:12:2: warning: return from incompatible pointer type [enabled by default]
  return newarray;
  ^
test.c:12:2: warning: function returns address of local variable [-Wreturn-local-addr]
test.c: At top level:
test.c:14:1: error: initializer element is not constant
 char **array = init_array();

Should it be like this?

char newarray[5][10];
char** init_array() {
        int i, j;
        for (i = 0; i < 5; i++) {
                for (j = 0; j < 10; j++) {
                        newarray[i][j] = '0';
                }
        }
        return newarray;
}
char **array = init_array();
解决方案

I think pictures help. Here is char newarray[5][10]. It is a single memory block consisting of an array of 10 characters, and an array of five of those.You could just clear it with a single memset call.

Here is char **array. It says array is a pointer.What is it a pointer to?a pointer to a character.

Keep in mind pointer arithmetic.If array is a pointer that happens to point to a pointer,then (*array) equals array[0], and that is the pointer that array points to.

What is array[1]?It is the second pointer in the array that array points to.

What is array[0][0]?It is the first character pointed at by the first pointer that array points to.

What is array[i][j]?It is the jth character of the ith pointer that array points to.

So how are newarray and array related?
Simple.newarray[i][j] is the jth character of the ith subarray of newarray.

So in that sense, it's just like array, but without all the pointers underneath.

What's the difference?Well, the disadvantage of array is you have to build it up, piece by piece.OTOH, the advantage is you can make it as big as you want when you build it up.It doesn't have to live within a fixed size known in advance.

Clear as mud?

这篇关于初始化用C二维字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:35