关于C/C++指针(转)

 #include<stdio.h>

char ga[]="abrtgertrht4rjhrt";

void my_array_func(char ca[10])
{
  printf(" addr of array param = %#x/n",&ca);    //the address of local variable
  printf(" addr of (ca[0]) = %#x/n",&(ca[0]));      //the address of global array
  printf(" addr of (ca[1]) = %#x/n",&(ca[1]));
  printf(" ++ca = %#x/n",++ca);                         // The value of the local variable
}

void my_pointer_func(char *pa)
{
  printf(" addr of ptr param = %#x/n",&pa);
  printf(" addr of (pa[0]) = %#x/n",&(pa[0]));
  printf(" addr of (pa[1]) = %#x/n",&(pa[1]));
  printf(" ++pa = %#x/n",++pa);
}


int main()
{
  printf(" addr of gobal array = %#x/n",&ga);
  printf(" addr of (ga[0]) = %#x/n",&(ga[0]));
  printf(" addr of (ga[1]) = %#x/n",&(ga[1]));
  my_array_func(ga);
  my_pointer_func(ga);
  
   return 0;
}
------------------------------------------------------output--------------------------------------------------
 addr of gobal array = 0x402000
 addr of (ga[0]) = 0x402000
 addr of (ga[1]) = 0x402001
 addr of array param = 0x22ff50
 addr of (ca[0]) = 0x402000
 addr of (ca[1]) = 0x402001
 ++ca = 0x402001
 addr of ptr param = 0x22ff50
 addr of (pa[0]) = 0x402000
 addr of (pa[1]) = 0x402001
 ++pa = 0x402001


Terminated with return code 0
Press any key to continue ...

-------------------------------------------------------------------------------------------------------------------------
The above example is from Expert C Programming Deep C secrets. Just comprehend the remarks I wrote above.

-----------------------------------------The following is an question from 优快云 forum------------------------------
#include<iostream>
using namespace std;

struct Node
{
    int value;
    Node* next;
};
   
void creatlist(Node **head)//
{   
    int i;
    //Node *tmp=*head;   // If  this line  is here , but not the following one,  it will be an error becase the
    cin>>i;                       // pointer tmp using the the following code doesn't not point to the list, but NULL
    *head=new Node;     // It's the author' fault.
    (*head)->value=i;
    (*head)->next=NULL;
    Node *tmp=*head;     //update and keep the same
    Node* p;
   
    while(cin>>i)
    {
        p=new Node;
        p->value=i;
        p->next=NULL;
        tmp->next=p;
        //cout<<i<<endl;
        tmp=tmp->next;
    }  
    cin.clear();   // clear buffer

}

int print(Node *head)
{
    Node *p=head;       
    while(p)
    {  
        cout<<p->value<<endl;
        p=p->next;
    }
    return 0;
}

void del(Node **head)
{
   
    cout<<"delete one node, please input the value of the node:";
    Node* q=*head;
    Node* p=*head;   
    int i;
    cin>>i;
   
    if(p && p->value == i)
    {
        *head=(*head)->next;
        delete p;
    }
    else                 // Here without else wil also be an error, because if the actual value is the first node
    {                      // it will delete twice.
       while(p && p->value!=i)
      {
         q=p;
         p=p->next;
      }
      q->next=p->next;
      delete p;
    }   
}

int main()
{  
    Node *head=NULL;
    creatlist(&head);
    print(head);
    del(&head);
    print(head);
    return 0;
}
// This case the most important point is to master the pointer to pointer.


http://sysuor.blog.163.com/blog/static/862013120083203214690/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小雄哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值