关于指针作为函数参数的有关问题
关于指针作为函数参数的问题
include <iostream>
using namespace std;
struct Student
{
long number;
float score;
Student *next;
};
Student* head;
Student* Create(void)
{
Student* PS;
Student* PEnd;
head = NULL;
while (1)
{
PS = new Student;
cin > > PS-> number > > PS-> score;
if (0 == PS-> number)
break;
if (NULL == head)
head = PS;
else
PEnd-> next = PS;
PEnd = PS;
}
PEnd-> next = NULL;
delete PS;
return head;
}
void Delete(Student* head, int number)
{
Student* p;
if (NULL == head)
{
cout < < "The list is empty! " < < endl;
return;
}
if (head-> number == number)
{
p = head;
head = head-> next;
delete p;
p = NULL;
cout < < "The head is deleted " < < endl;
return;
}
for (Student* pGuard=head; pGuard-> next; pGuard=pGuard-> next)
{
if (pGuard-> next-> number == number)
{
p = pGuard-> next;
pGuard-> next = p-> next;
delete p;
p = NULL;
cout < < "The number " < < number < < " is deleted! " < < endl;
return;
}
}
cout < < "Can not find " < < number < < "from list " < < endl;
return;
}
void ShowList(Student* head)
{
cout < < "The list is: " < < endl;
while (head)
{
cout < < head-> number < < " " < < head-> score < < endl;
head = head-> next;
}
}
void main(void)
{
Create();
ShowList(head);
Delete(head,3);
ShowList(head);
}
问题:
1.为什么void ShowList(Student* head)不会修改实参head而只修改形参head的地址?也就是说形参只是实参的一个拷贝?
2.为什么void Delete(Student* head, int number)里面修改了形参head所指向的地址实参head也跟着变化呢?
3.也就是说当指针做为函数的形参时,在什么情况下修改形参的地址或者所指向的地址实参也会变化?是不是修改*head的值,实参也会同步修改,但修改head的值,实参就不会同步变化?
谢谢!
------解决方案--------------------
1.为什么void ShowList(Student* head)不会修改实参head而只修改形参head的地址?也就是说形参只是实参的一个拷贝?
===========
形参只是实参的一个拷贝,
所以修改 head 不能修改实参,
但是修改 *head 可以实现 修改 *实参, 因为拷贝的内容是完全一样的,
即 *head = *实参
------解决方案--------------------
include <iostream>
using namespace std;
struct Student
{
long number;
float score;
Student *next;
};
Student* head;
Student* Create(void)
{
Student* PS;
Student* PEnd;
head = NULL;
while (1)
{
PS = new Student;
cin > > PS-> number > > PS-> score;
if (0 == PS-> number)
break;
if (NULL == head)
head = PS;
else
PEnd-> next = PS;
PEnd = PS;
}
PEnd-> next = NULL;
delete PS;
return head;
}
void Delete(Student* head, int number)
{
Student* p;
if (NULL == head)
{
cout < < "The list is empty! " < < endl;
return;
}
if (head-> number == number)
{
p = head;
head = head-> next;
delete p;
p = NULL;
cout < < "The head is deleted " < < endl;
return;
}
for (Student* pGuard=head; pGuard-> next; pGuard=pGuard-> next)
{
if (pGuard-> next-> number == number)
{
p = pGuard-> next;
pGuard-> next = p-> next;
delete p;
p = NULL;
cout < < "The number " < < number < < " is deleted! " < < endl;
return;
}
}
cout < < "Can not find " < < number < < "from list " < < endl;
return;
}
void ShowList(Student* head)
{
cout < < "The list is: " < < endl;
while (head)
{
cout < < head-> number < < " " < < head-> score < < endl;
head = head-> next;
}
}
void main(void)
{
Create();
ShowList(head);
Delete(head,3);
ShowList(head);
}
问题:
1.为什么void ShowList(Student* head)不会修改实参head而只修改形参head的地址?也就是说形参只是实参的一个拷贝?
2.为什么void Delete(Student* head, int number)里面修改了形参head所指向的地址实参head也跟着变化呢?
3.也就是说当指针做为函数的形参时,在什么情况下修改形参的地址或者所指向的地址实参也会变化?是不是修改*head的值,实参也会同步修改,但修改head的值,实参就不会同步变化?
谢谢!
------解决方案--------------------
1.为什么void ShowList(Student* head)不会修改实参head而只修改形参head的地址?也就是说形参只是实参的一个拷贝?
===========
形参只是实参的一个拷贝,
所以修改 head 不能修改实参,
但是修改 *head 可以实现 修改 *实参, 因为拷贝的内容是完全一样的,
即 *head = *实参
------解决方案--------------------