#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/