1. What is the output result of running C code?
[Cpp]
Int main (int argc, char ** argv [])
{
Char str1 [] = "hello world ";
Char str2 [] = "hello world ";
Char * str3 = "hello world ";
Char * str4 = "hello world ";
If (str1 = str2)
Printf ("str1 and str2 are same. \ n ");
Else
Printf ("str1 and str2 are not same. \ n ");
If (str3 = str4)
Printf ("str3 and str4 are same. \ n ");
Else
Printf ("str3 and str4 are not same. \ n ");
Return 0;
}
Int main (int argc, char ** argv [])
{
Char str1 [] = "hello world ";
Char str2 [] = "hello world ";
Char * str3 = "hello world ";
Char * str4 = "hello world ";
If (str1 = str2)
Printf ("str1 and str2 are same. \ n ");
Else
Printf ("str1 and str2 are not same. \ n ");
If (str3 = str4)
Printf ("str3 and str4 are same. \ n ");
Else
Printf ("str3 and str4 are not same. \ n ");
Return 0;
}
Answer: two rows are output. The first line is str1 and str2 are not same, and the second line is str3 and str4 are same.
Str1 and str2 are two string arrays. We will allocate two 12-byte spaces for them, and copy the "hello world" contents to the array respectively. These are two arrays with different initial addresses. Therefore, the values of str1 and str2 are different. Str3 and str4 are two pointers. Instead of allocating memory to them to store string content, we only need to point them to the address in the memory of "hello world. Because "hello world" is a constant string and has only one copy in the memory, str3 and str4 point to the same address. Therefore, comparing str3 and str4 values is the same.
2. What is the result of running C ++ code?
[Cpp]
Int Fun1 (char * str)
{
Printf ("% s \ n", str );
Return 0;
}
Int Fun2 (char * str)
{
Printf ("% s \ n", str );
Return 1;
}
Int main (int argc, char ** argv [])
{
Int res1, res2;
Res1 = (Fun1 ("a") & Fun2 ("B") | (Fun1 ("c") | Fun2 ("d "));
Res2 = (Fun1 ("a") & Fun2 ("B") & (Fun1 ("c") | Fun2 ("d "));
Return res1 | res2;
}
Int Fun1 (char * str)
{
Printf ("% s \ n", str );
Return 0;
}
Int Fun2 (char * str)
{
Printf ("% s \ n", str );
Return 1;
}
Int main (int argc, char ** argv [])
{
Int res1, res2;
Res1 = (Fun1 ("a") & Fun2 ("B") | (Fun1 ("c") | Fun2 ("d "));
Res2 = (Fun1 ("a") & Fun2 ("B") & (Fun1 ("c") | Fun2 ("d "));
Return res1 | res2;
}
Answer: print out four rows: a, c, d, and.
In C/C ++, and or operations are executed from left to right. When calculating rest1, calculate Fun1 ("a") & Func2 ("B") first "). First, Func1 ("a") prints a row whose content is. Since Fun1 ("a") returns false, whether the return value of Func2 ("B") is true or false, Fun1 ("a") & Func2 ("B ") the result is false. Because the results of Func2 ("B") are irrelevant, Func2 ("B") will be omitted without computation. Next, calculate Fun1 ("c") | Func2 ("d") and print the two rows of content c and d respectively.
When calculating rest2, Func1 ("a") First prints a row whose content is. Since Func1 ("a") returns false, Func2 ("B") will skip the calculation in the same way as above. Since the result of Fun1 ("a") & Func2 ("B") is false, no matter what the result of Fun1 ("c") & Func2 ("d") is, the result of the entire expression is false, so Fun1 ("c") & Func2 ("d") will be ignored.
3,
[Cpp]
Int SizeOf (char pString [])
{
Return sizeof (pString );
}
Int main (int argc, char ** argv [])
{
Char * pString1 = "google ";
Int size1 = sizeof (pString1 );
Int size2 = sizeof (* pString1 );
Char pString2 [100] = "google ";
Int size3 = sizeof (pString2 );
Int size4 = SizeOf (pString2 );
Printf ("% d, % d", size1, size2, size3, size4 );
Return 0;
}
Int SizeOf (char pString [])
{
Return sizeof (pString );
}
Int main (int argc, char ** argv [])
{
Char * pString1 = "google ";
Int size1 = sizeof (pString1 );
Int size2 = sizeof (* pString1 );
Char pString2 [100] = "google ";
Int size3 = sizeof (pString2 );
Int size4 = SizeOf (pString2 );
Printf ("% d, % d", size1, size2, size3, size4 );
Return 0;
}
Answer: 4, 1,100, 4. PString1 is a pointer. On 32-bit machines, any pointer occupies 4 bytes of space. * PString1 is the first character of the string pstring1. A character occupies one byte. PString2 is an array, and sizeof (pString2) is used to evaluate the size of the array. The array contains 100 characters, so the size is 100 bytes. In the SizeOf function, although the input parameter is a character array, when the array is passed as a function parameter, the array will automatically degrade to the same type of pointer.