[英]C Programming: declaring string and assignment
int main(){
char inputType[5] = "hello you"; //a string
char *word[5]; //a string that will contain 4 character + the null
word = get_word(inputType);
return 0;
}
char *get_word(char inputType[]){ //inputType is a string
char *array[2]; //an array that will store ["hello", "you"]
// got correct code to make it ["hello", "you"]
return array[1] //want to return "you"
}
I get this error message: 我收到此错误消息:
Error: word = get_word (inputType) assingment to expression with array Type------------------------------
Questions: 问题:
*word[5]
vs inputType[5]
char *word[5]
与inputType[5]
之间的区别 This is wrong 这是错的
char inputType[5] = "hello you";
hello you
is 9 characters long and you also need the null terminator so you need... hello you
是9个字符长,你还需要空终止符,所以你需要...
char inputType[10] = "hello you";
This is wrong because the comment is wrong 这是错误的,因为评论是错误的
char *word[5]; //a string that will contain 4 character + the null
char *word[5]
is not a C string, it's an array of 5 pointers to C strings that have not yet been allocated. char *word[5]
不是C字符串,它是一个包含5个指向尚未分配的C字符串指针的数组。
Yes it declares a char array of size 5 (including the NUL character)where you are storing more than 5 characters. 是的它声明了一个大小为5的字符数组(包括NUL字符),您可以在其中存储超过5个字符。
First one is array of char*
and second one array char
s. 第一个是char*
数组,第二个是char
数组。
Yes char*word[5] is not what you are thinking..unless you allocate memory it can just hold addresses of memory locations where characters are stored. 是char * word [5]不是你在想的......除了你分配内存,它只能保存存储字符的内存位置的地址。
char* arr[5] char * arr [5]
| ---------------------------------- ~~ | | | | | char* | | | ---------------------------------- ~~ arr[0] arr[1] arr[2]
you are basically initializing the char array. 你基本上是在初始化char数组。 So do this char inputType[]= "Hello You";
这样做char inputType[]= "Hello You";
The error message is basically shown due to this line word = get_word(inputType);
由于此行word = get_word(inputType);
基本上显示错误消息 word = get_word(inputType);
well what you are doing? 那你在做什么? You are assigning char*
to an array variable which is supposed to contain char*
-s 您将char*
分配给一个应该包含char*
-s的数组变量
So that will be word[0]=get_word()....
所以这将是word[0]=get_word()....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.