Home > Back-end > C at the beginning of introduction, solve the following program! Thank you!!!!!!
C at the beginning of introduction, solve the following program! Thank you!!!!!!
Time:10-10
#include using namespace std; Int main () { Char c1; C1='a'; CoutChar c2 [10]. C2 [10]="a"; Coutreturn 0; }
CodePudding user response:
C2 [10]="a"; ============ C2 [0]='a';
CodePudding user response:
A single character can't be string
CodePudding user response:
If you want to initialize so write char c2 [10]={' a ', 'b'}; If you want to a one assignment is a [0]='a', a [1]='b' Must be a single quotes, double quotes are used to make the string And not as a [10]='a', so write already crossed, because there are hidden character '\ 0', '\ 0' is the terminator
CodePudding user response:
Individual we called character, string is the result of superposition of multiple characters, and character array is the superposition of multiple characters, so we usually think that character array=string About why wrong, explain here Must first know a character variable is a byte of memory, is a string enclosed in double quotation marks, single quotation marks are characters And will the end of the string with a null character
You can try Sizeof function is used to return parameter of memory We can test: coutThen you will be surprised to find that the result is 4, it is said above, the string will automatically add a null character (' \ 0 '), the null character also takes up one byte So c2 [10] is a character array of variables, the system only it provides one byte of memory, and you will be "a" to it, the "a" for the 2 bytes, nature is not achieve
So if you want to give it an initial value, a character array has the following several methods: 1. The c2="string";//here understand that array name is an address, and is the first item of the array; It is convenient in assignment, every characters in the string starting from 0 subscript assigned to each in turn space 2. C2={' 1 'character,' 2 'characters, characters' 3'}; Must have this method should be//don't have to explain, is the way of general array initialization [in addition to a point, we are operating character array, such as we read in a string, use a character array to store, but for convenience, you don't want to start the subscript 0 read, do you want to start from 1, this is we can use the array name + do you want to start the subscript, specific see the pointer manipulation]
CodePudding user response:
C2 10 element, the subscript biggest is 9
CodePudding user response:
A single character can be a string, the key is your statement is char instead of string