today's pointers practice
One
Char *const p; P cannot be changed
char Const *P; *p can not be changed
const char *p; *p can not be changed, ditto
Just start thinking since P is immutable, then I first define P = &a, and then define P = &b, naturally it will go wrong, indeed.
That *p immutable, I define P = &a, and then define P = &b, also can make mistakes, because its *p still changed.
Similarly, I define P = &a, and then change the value of a, it should also be wrong!
But not so.
Only when you write *p = xxx, the compiler will not report an error. Other ways to change *p indirectly will not be an error.
int main ()
{
int a[5] = {1, 2, 3, 4, 5};
int *ptr = (int *) (&a + 1);
printf ("%d%d\n", * (A + 1), * (ptr-1));
return 0;
Well, just beginning to think this code output should be 2 1, but not.
The output turned out to be 2 5, and it was amazing.
Explain:
&a is an array pointer of type int (*) [5];
The pointer plus 1 depends on the pointer type plus a certain value, the different type of pointer +1 after the increase of the size is different .
A is a 5-length int array pointer, so add 5*sizeof (int)
So ptr is actually a[5]
But PRT and (&a+1) types are not the same (this is important)
So prt-1 will only subtract sizeof (int*).
**a,&a's address is the same, but it doesn't mean the same thing:
A is the address of the first address of the array, which is a[0], &a is the first address of an object (array),
A+1 is the address of the next element in the array, that is, the a[1],&a+1 is the address of the next object, that is, a[5]. **
Two
#incldue <stdio.h>
#include <string.h>
int main ()
{
char A;
Char *str = &a;
strcpy (str, "Hello");
printf (str);
return 0;
}
This one is better judged. The space allocated for STR is insufficient because the internal reading and writing across the bounds causes the program to crash segmentation fault (core dumped).
Three
#include <stdio.h>
int main ()
{
char *str = "AAA";
printf ("%s", str);
Str[0] = ' B ';
printf ("%s", str);
return 0;
}
Because STR is a string constant, it cannot be partially modified, that is, it is illegal to assign operations to str[0. Runtime segmentation Fault (core dumped).
Four
what the Int (*s[10]) (int) represents.
Emmmm, read the answer suddenly enlightened (laugh). The first is an array of pointers, which contains a pointer, and each pointer points to a function of int func (int param).
Five
Has the following expression:
int a = 248,b = 4;
int const C =;
const int *d= &a;
int *const e= &b;
int const *F = &a;
Which of the following expressions will be prohibited by the compiler? Why.
*c = d = &b; *d = n = e = e = &a; f = 0x321f;
Haha, for *c unexpectedly did not respond to come over, do not know what is going on.
The answer is decisive:
*c is what Dongdong, forbid.
*d = 43, forbidden.
E = 34, forbidden.
E = &a, forbidden.
f = 0x321f, forbidden. (This one remains to be discussed, the answer says no, but my compiler is just warning warning:assignment makes pointer from integer without a cast [-wint-conversion]
Six
#include <stdio.h>
#include <string.h>
void getmemory (char *p)
{
p= (char *) malloc (100) ;
strcpy (P, "Hello World");
}
int main ()
{
char *str = NULL;
GetMemory (str);
printf ("%s/n", str);
Free (str);
return 0;
}
Answer: program crashes, malloc in GetMemory cannot return dynamic memory, free () is dangerous for STR operations
Bo main: GetMemory P is a parameter, is a pointer variable, getmemory (str) After the call, passed is the pointer variable saved object address, p= (char *) malloc (100) In effect, it is wrong to pay the first address of the requested dynamic memory space to the address that P points to (that is, the address that Str points to). You should modify the pointer void GetMemory (char **p) that points to the pointer so that the address returned by malloc is assigned to *P (the STR variable itself).
Seven
Char szstr[10];
strcpy (Szstr, "0123456789″");
What results. Why.
Answer: The length is not the same, will result in illegal OS.
Emmmm, but I did it with no errors and I could output str.
Eight
To assign values to the absolute address 0x100000, we can use (unsigned int*) 0x100000 = 1234;
So if you want the program to jump to the absolute address is 0x100000 to execute, what should be done.
Answer: * (void (*) ()) 0x100000) ();
You first want to cast the 0x100000 into a function pointer, which is:
(Void (*) ()) 0x100000
And then call it again:
* (void (*) ()) 0x100000) ();
Use a typedef to see more intuitive:
typedef void (*) () voidfuncptr;
* ((VOIDFUNCPTR) 0x100000) ();
冏, this is still not read ...
Nine
#include <stdio.h>
#include <string.h>
void GetMemory (char **p, int num);
int main ()
{
char *str = NULL;
GetMemory (&STR);
strcpy (str, "Hello");
Free (str);
if (str!= NULL)
strcpy (str, "World");
printf ("Str is%s\n", str);
return 0;
}
void GetMemory (char **p, int num)
{
*p = (char *) malloc (num);
}
Answer: Output str is world.
Free is only the memory space that the STR points to, and its value still exists. So after free, there's a good habit of having str = NULL.
At this point, the STR-pointing memory has been reclaimed, and if the output statement is preceded by an allocation space operation, this storage space may be reassigned to other variables.
Although the program does have a big problem (as you have made it clear), it usually prints out the world.
This is because memory management in the process is typically not done by the operating system, but by the library function itself.
when you malloc a piece of memory, the management library applies a space to the operating system (which may be larger than your request), and then records some management information in this space (usually in front of the memory you requested) and returns the address of the available memory. But when the memory is freed, the admin library usually does not return the memory to the operating system, so you can continue to access the address.
Well, I don't know why I was performing segmentation fault (core dumped).
Ten
char (*STR) [20];/*str is an array pointer, that is, a pointer to an array. */
Char *str[20];/*str is an array of pointers whose elements are pointer-type data. */
Well, actually this is very understandable. Like Char *str, you can think like char a. Char A is a char-type variable, and char *str is a variable of char *. Don't get dizzy by its writing format (* with STR).
Eleven
void GetMemory (char **p, int num)
{
*p = (char *) malloc (num);
}
void Test (void)
{
char *str = NULL;
GetMemory (&STR);
strcpy (str, "Hello");
printf (str);
}
There is something wrong or inadequate about the code.
There are 2 problems:
malloc memory is not released in the test function in the subject.
The getmemory in this argument is a pointer to a string pointer, but executes the application memory and the assignment statement P = (char) malloc (num) in getmemory; After not Judging whether the memory is successful or not, you should add
if (*p = = NULL)
{
...//** to request memory failure processing * *
}
Twelve
void Test (void)
{
char *str = (char *) malloc (MB);
strcpy (str, "Hello");
Free (str);
...//omitted other statements
}
code has any deficiencies or errors.
There are 2 problems:
In the execution char str = (char) malloc (100); No memory after the application of the success of the judgment;
In addition, no str is empty after free (str), resulting in the possibility of becoming a "wild" pointer , plus:
str = NULL;
13,
The difference between a pointer and a reference:
Answer:
(1) Non-null difference. You cannot use a reference to a null value under any circumstances. So if you use a variable and let it point to an object, but the variable may not point to any object at some point, you should declare the variable as a pointer, because you can assign a null value to the variable. Conversely, if the variable definitely points to an object, for example, your design does not allow the variable to be empty, then you can declare the variable as an argument
Use. The fact that there is no reference to a null value means that the code that uses the reference is more efficient than using a pointer.
(2) the legality difference. You do not need to test the legality of a reference before using it. Instead, the pointer should always be tested to prevent it from being empty.
(3) The difference can be modified. Another important difference between pointers and references is that pointers can be assignable to point to another different object. However, references always point to objects that are specified when initialized and cannot be changed later, but the contents of the specified object can be changed.
Thinking: Encounter this type of topic, be sure to think about what their characteristics are, grasp the characteristics of the analysis; think about what their function is, compare.
14,
Analytic: This problem examines is the parameter passes, the value passes, the pointer passes (address passes) and the reference passes .
Swap1 Pass is a copy of the value, in the function only modified the formal parameter p, q (actually a copy of A, b), p, Q of the value is actually exchanged, but they are local variables, does not affect the main functions A and B. When the function Swap1 lifecycle ends, the stack of P and q is also deleted.
SWAP2 passes an address in which the formal parameter *p and *q in the function body are two pointers to the actual argument A and B addresses.
Here to note:
int *temp;
*temp=*p;
is illogical, int *temp a new pointer (but no memory allocated). *temp=*p is not pointing but copying. The value of the memory pointed to by the *p (that is, the value of a) is copied to the memory pointed to by the *temp. but does int *temp not allocate memory? It does not assign, so the system temporarily gives a random address at the time of the copy, allowing it to deposit a value. The assigned random address is an "unexpected" and is not recycled after the function ends, causing a memory leak.
SWAP3 Pass is an address, in the function body parameter *p, *q is to point to the actual parameter A, B address two pointers.
Here to note:
int *temp;
Temp=p;
int *temp a new pointer (but no memory allocated). Temp=p is a point, not a copy. Temp points to the address that *p is pointing to (that is, a). and the code:
int *temp;
Q=temp;
However, the function SWAP3 cannot achieve the exchange of two numbers, because the function body is only a pointer change, but the value in the address does not change.
SWAP4 can be exchanged in two numbers because it modifies the value in the address to which the pointer points.
The SWAP5 function is similar to the SWAP4, is a reference pass, and the result of the modification directly affects the argument.
Answer:
SWAP4 functions and SWAP5 functions.
Well, practice http://blog.csdn.net/hnust_xiehonghao/article/details/17638297 This blogger's big question, thanks.