我怎样才能在C中初始化char类型数组 [英] How can I intialise char type array in C

查看:130
本文介绍了我怎样才能在C中初始化char类型数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以很容易地做到:char arr [3] = {'O','a','b'};

但是我可以把它写成:char arr [3] = { 怎么样,是};

请帮助我,我是一名初学者



我是什么尝试过:



我试过输入

char arr [3] = {'o','a', 'b'}
但对上面提到的另一种类型有疑问

解决方案

问题是你必须将变量声明为正确的键入以使用它们:

 char arr [3] 

为3 char items - 和C char 几乎可以肯定是ASCII,这意味着它是8位宽,所以即使在64位系统上, arr 数组只占用三个字节的内存。

但是:

 {hi,how,are} 

不是三个字符,它是三个字符串,它们要大得多。

hi需要3个字节:'h','i'和空终止符。 
how需要4个字节:'h','o','w'和null终止符。
are需要4个字节:'a','r','e'和null终止符。

此外,您不会将数据存储在<$ c $中c> arr 本身, arr 需要三个指向char 值的指针才能访问字符串,每个指针在64位系统上需要8个字节!如果它没有那样工作,你就无法有效地索引字符串,因为每次两个字符串之间的距离不一样。

要使用字符串初始值设定项,你需要声明 arr 以便它有足够的空间来存储它们,这意味着将它声明为指向char的指针值:

  char  * arr [ 3 ] = {  hi 如何  }; 


先生。格里夫是对的。还要注意,通常不指定数组的大小。这是一个例子:

  const   char  * strings [] = 
{
第一个字符串
第二个字符串
第三个字符串
};

并且有时会添加一个终结符,因此您可以循环直到找到null:

 < span class =code-keyword> const   char  * strings [] = 
{
第一个字符串
第二个字符串
第三个字符串
NULL
};

for int n = 0 ; strings [n]!= NULL; ++ n)
{
// 在这里用字符串做点什么
}


答案已得到纠正,但为了更好地理解那些非常重要的东西你应该从这个教程中学习。



最后,大多数程序员使用某些类来处理文本。它们通常具有有用的功能,如查找或替换。有关章节的 std:string 简介。


I can easily do: char arr[3]={'O','a','b'};
but can i write it as: char arr[3]={"hi","how","are"};
please help me , i am a begginer

What I have tried:

I have tried entering
char arr[3]={'o','a','b'}
but doubt for the other type as mentioned above

解决方案

The problem is that you have to declare variables as teh correct type in order to use them:

char arr[3]

Allocates enough space for 3 char items - and in C char is almost certainly ASCII which means it is 8 bits wide, so even on a 64 bit system the arr array occupies only three bytes of memory.
But this:

{"hi","how","are"}

is not three characters, it's three strings, which are much larger.

"hi"   requires 3 bytes: 'h', 'i', and a null terminator.
"how"  requires 4 bytes: 'h', 'o', 'w', and a null terminator.
"are"  requires 4 bytes: 'a', 'r', 'e', and a null terminator.

In addition, you don't store the data in arr itself, arr requires three pointer to char values in order to access the strings, and each pointer needs 8 bytes on a 64 bit system! If it didn't work like that, you couldn't efficiently index through the strings as the "distance" between two strings would not be the same each time.
To use the string initializer, you need to declare arr so that it has enough space to store them, and that means declaring it as an array of pointer to char values:

char* arr[3] = {"hi","how","are"};


Mr. Griff is correct. Also be aware that often the size of the array is not specified. Here's an example :

const char *strings[] =
{
   "first string",
   "second string",
   "third string"
};

and sometimes a terminator is added so you can loop until the null is found:

const char *strings[] =
{
   "first string",
   "second string",
   "third string",
   NULL
};

for( int n = 0; strings[n] != NULL; ++n )
{
   // do something with the string here
}


The answers are corrected, but to better understand that very important stuff you should learn from this tutorial.

In the end most coders are using some classes for dealing with texts. They have often useful functions like finding or replace. An introduction into std:string with some chapters.


这篇关于我怎样才能在C中初始化char类型数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆