Int x = 35;
Char STR [10];
// Q: What are the values of strlen (STR) and sizeof (STR?
// The strlen (STR) value is unknown. strlen determines whether the string ends Based on '/0.
// Sizeof (STR) = 10 sizeof an array is the length of the array.
Strcpy (STR, "www.it315.org"/* A total of 13 letters */);
// Q: What are the values of x and strlen (STR?
// The value of X is 35.
// Strcpy (char * DEST, const char * SRC)
// Copy DEST Based on SRC. The length of the replication is determined based on the '/0' value of SRC, And the Dest must provide sufficient length. This will cause overflow. strlen returns 13, however, the external data of the array has been damaged.
// (The author's note: I have provided a more accurate answer below)
STR = "it315.org"; // can I compile it?
// Arrays cannot be assigned values and can only be initialized. Char STR [10] = "it315.org ";
// During initialization, the compiler checks whether the array length matches the initialization String Length.
Char * pstr;
Strcpy (pstr, "http://www.it315.org ");
// Can the above sentence be compiled? Is there a problem when running?
// It can be compiled, but pstr points to the constant zone. It is recommended that you only perform read operations during the runtime, and write operations are not safe.
// (The author's note: I have provided a more accurate answer below)
Const char * P1;
Char * const P2;
// What is the difference between the above two statements?
// Const char * and char const * Both indicate character pointers to constants.
// Char * const indicates the constant pointer to a character
P1 = (const char *) STR;
// If it is p1 = STR; can the compilation be passed? Why type conversion? What is the essence of type conversion?
// You can compile it. The relationship between constants and constants is as follows:
// The const pointer can point to the const or non-const region without any problems.
// Non-const pointer cannot point to the const region, which may cause an error.
Strcpy (P1, "ABC"); // can the compilation be passed?
// Cannot pass, strcpy (char *, const char *); char * cannot point to const char *
Printf ("% d", STR); // is there a problem?
// No problem. The STR address is output.
Pstr = 3000; // can it be compiled? If not, how can I modify it to ensure the compilation is successful?
// It cannot pass. char * pstr indicates that pstr is a character pointer and cannot point to an integer variable of 3000.
// If you modify the value, you can: pstr = (char *) 3000 and point pstr to the address 3000;
Long y = (long) pstr; // can this be done?
// Yes. The value of Y is the address indicated by pstr. However, if it is purely an address, it is best to use unsigned long.
Int * P = STR;
* P = 0x00313200;
Printf ("% s", STR); // What is the result? The following message is displayed: 0x31 corresponds to '1' and 0x32 corresponds to '2 '.
// Compilation may not pass first. Some compilers may not allow int * to direct to char *. It is best to change it to int * P = (int *) STR;
// If the pass is successful, nothing is found. Int * P = STR; P indicates the address indicated by STR, and * P indicates modifying the memory pointed by Str.
// Because sizeof (INT) is on a 32-bit machine, int has four bytes (in fact, it depends on the compiler configuration file, as if it was limit. h, usually 4 bytes) So STR [0]-str [3] is modified.
// Because 0x00313200 starts with 0, the string starts with '/0' and cannot print anything. Here is a question about big-endin and little-endin. Take 0x31323334 as an Example
// On the little-endin machine, 0x31323334 is arranged in the memory in the order of 34 33 32 31, and the output is 4321, such as the Intel chip pc
// The Big-endin host is 31 32 33 34 and the output is 1234, for example, IBM powerpc
P = 3000; // What is the result of p + 1?
// 3000 + sizeof (INT); pointer + 1 is the original address plus sizeof (the data type indicated by the pointer)
Char * Pc = new char [100]; // The preceding statement occupies several memory blocks in the memory. What is the layout?
// The PC itself occupies a 4-byte pointer length of the function stack (whether it is 4 bytes depends on the machine and compiler ).
// New will apply for a continuous space of 100 bytes of sizeof (char) on the stack.
Void test (char ** P)
{
* P = new char [100];
} // Is there a problem with this compilation function? How do I pass parameters to call this function?
// No problem with the program. The address indicated by the pointer must be changed in the function.
// The reason is as follows: if the passed pointer is itself, when the function is called, the real parameter will be copied to an instance, so it is not the original pointer, any changes made to the pointer itself cannot be passed back.
// It can be understood that if the input parameter is int, the change to the int value will not be passed back, and the addition of * is the same.
// Do you understand the functions of typedef int (* pfun) (int x, int y?
// Defines a macro of the function pointer type. In this way, pfun indicates the function pointer type that points to the return value as int and has two int parameters at the same time.
// This variable can be defined:
// For example, a function is int fun (int x, int Y );
// Pfun P = fun;
// (The author's note: I have provided a more accurate answer below)
Bytes --------------------------------------------------------------------------------------------------------------
Here is my supplement:
Question 2:
Int x = 35;
Char STR [10];
Strcpy (STR, "www.it315.org"/* A total of 13 letters */);
// Q: What are the values of x and strlen (STR?
A: The strlen value is 13. In the VC ++ environment, the value of X must be changed (other compilers did not try it ,). although it seems that the value of X has not been modified in the program, the actual running result is that the value of X has been modified. This is because after strcpy, copy the excess data to the STR neighbor (X of the int type), so the data of x changes. this is an unforgettable problem that I encountered when I first debuted. Although I solved this problem with the help of my friends, I have never understood why the value of X has changed, only when I finally took the post of Training Instructors did I start to sort out my previous puzzles and summarize my previous experiences for reference. I think the value of this question is very great. It can attract students to pay enough attention to the cross-border copying of strings, and through this question, they can better understand how the string is handled, better understanding of the relationship between a string and a character array: A string is a character array, but this character array is used in the processing of string functions, these functions do not consider the length of the array, just remember the first address of the array, start processing from the first address, and end processing when 0 is encountered,
Question 4:
Char * pstr;
Strcpy (pstr, "http://www.it315.org ");
// Can the above sentence be compiled? Is there a problem when running?
A: The compilation is successful, but pstr is not effectively initialized. It points to an uncertain memory zone, and a non-writable memory error will occur during runtime!
Last question:
// Do you understand the functions of typedef int (* pfun) (int x, int y?
A: The biggest use of a function pointer is that it can be called by a template method, which I learned when learning the Java design pattern. for example, the process structure of two functions is exactly the same, but the specific functions called internally are different, as shown below:
Void func1 ()
{
// A piece of Process Code and an object-oriented proxy, such as security check and logging
Int sum = add (x, y );
// A piece of Process Code and an object-oriented proxy, such as security check and logging
}
Void func2 ()
{
// Process Code and Aspect-Oriented proxy identical to func1, such as security check and logging
Int Difference = sub (x, y );
// Process Code and Aspect-Oriented proxy identical to func1, such as security check and logging
}
You can define only one function as follows:
Void func (pfunc P)
{
// Process Code and Aspect-Oriented proxy identical to func1, such as security check and logging
Int Difference = p (x, y );
// Process Code and Aspect-Oriented proxy identical to func1, such as security check and logging
}
When calling a program, let the parameter P point to the ADD and sub functions respectively.
My current focus on writing and work is no longer in the C language, and I have no time to answer them one by one. I will use the answers released by jackie214 to respond to you.