C language learning - has shown signs pointer

A long time ago I heard this thing pointer, and the rumor is very difficult to understand, no its type, to ask his name. However, it seems to have no special place.

  • pointer

  A pointer is a variable to store variable addresses.  

Thus, we can know, the pointer is stored in the variable address and can only be applied to objects in memory, the pointer itself is a variable. But not only can store a pointer variable address, it may be another example of function pointers.

C language program is running, the memory is divided into these parts

  1. --- read-only storage area to store the program itself and the constant machine code
  2. Static storage area ---- global variables and static variables
  3. Dynamic memory heap and stack ----

Like the relationship pointer and say not great.

 

  • Pointer declaration

  Pointers [Keyword Type] [variable name] is defined in such a manner. eg: int * a; definition of a pointer, a pointer called a, any address may be stored int type variables. Keyword pointer type is the type of memory to store data.

  Now two appreciated * & unary operator, & address operator is acting the return address of the variable. * Is the indirect addressing operator returns the value of the address of the specified operand. In which I use the c programming language textbooks and did not detail these two operators.

  Understanding these two operators, now we can do something, the following code;

    

int A, C;      // definition of variables A, C 
int * B;       // define a pointer B 
A = . 1 ;         // now. 1 = A 
B = A &;     // B to point A 
C * = B;     // C is now equal to . 1 
* B = . 5 ;     // A is now equal. 5 
B & C =;     // B now points C 
* B = 0 ; // 0 C is now equal to

 

#include <stdio.h>
#include <stdlib.h>


main()
{
    int *p=NULL,q=1;
    p = &q;
     printf("Address:%p\n",&q);
    printf("Address:%d\n",q);
    printf("Address:%p\n",p);
    printf("Address:%d\n",p);
    printf("Address:%p\n",*p);
    printf("Address:%d\n",*p);

}
Results are as follows:

Address:0060FEF8
Address:1
Address:0060FEF8
Address:6356728
Address:00000001
Address:1

Since pointers for direct memory operation, therefore, to know where each pointer pointing to, what action. Null pointer is dangerous, so when the pointer does not point to any place, you need to use the standard library provides NULL initialized.

 

 

指针可以用void 声明,我之前想用void的指针printf一个变量,但是编译器报错了。网上说void 不指定类型的指针可以指向任何累心的地址,我在coudblocks测试是可以,但是输出报错,如果用另一个指定类型的指针再指向这个void的指针,就可以不报错输出。我对于这个void有点迷,看不懂。

找了半天,这个类型转换我还有点懵,啥也不写了,先写这么多吧。

 

 

Guess you like

Origin www.cnblogs.com/dosu/p/11931175.html
Recommended