---
title: "链表，形参，实参"
author: "gauss2008"
date: "2006-11-10T10:35:00Z"
tags: ["C", "C#", "C++", "FP"]
slug: "untitled"
draft: false
lastmod: "2006-11-10T10:35:00Z"
id: 74711
url: "https://gauss2008.iteye.com/blog/74711"
---

在学习C语言的时候，关于形参如何改变实参，链表中节点的值何时被改变，何时不被改变，一直没有搞明白，今天终于搞懂，记录下来备份。 

1、形参改变实参

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;stdio.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;malloc.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)typedef struct LNode  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    FILE \*fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    char \*fname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    struct LNode \*next;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}LNode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)LNode \*LinkList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)LNode \*ListInit(LNode \*linklist)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if((linklist=(LNode \*)malloc(sizeof(LNode)))==NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("Init Link Error");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    linklist-&gt;next=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)ListInsert(LNode \*linklist, char \*fname,FILE \*fp)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    LNode \*lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  LNode \*p;     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  p=linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if((lnode=(LNode\*)malloc(sizeof(LNode)))==NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    printf("Insert Error");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                while(p-&gt;next!=NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    p=p-&gt;next;                      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    if(!strcmp(p-&gt;fname,fname))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            p-&gt;fp=fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            return ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p-&gt;next=lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;fp=fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;fname=fname;              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;next=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p=lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //注意这一段代码，这里只是改变了test指针指向的所指变量的值，即fname的值，  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //故，在main函数中的fname的值会发生变化  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                while(\*fname!='

2、形参不改变实参

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;stdio.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;malloc.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)typedef struct LNode  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    FILE \*fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    char \*fname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    struct LNode \*next;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}LNode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)LNode \*LinkList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)LNode \*ListInit(LNode \*linklist)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if((linklist=(LNode \*)malloc(sizeof(LNode)))==NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("Init Link Error");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    linklist-&gt;next=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)ListInsert(LNode \*linklist, char \*fname,FILE \*fp)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    LNode \*lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  LNode \*p;     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  p=linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if((lnode=(LNode\*)malloc(sizeof(LNode)))==NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    printf("Insert Error");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                while(p-&gt;next!=NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    p=p-&gt;next;                      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    if(!strcmp(p-&gt;fname,fname))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            p-&gt;fp=fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            return ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p-&gt;next=lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;fp=fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;fname=fname;              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;next=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p=lnode;                  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //注意这一段代码，与前一个TestOne.c不一样，  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //这里将fname的值变为"TTT"的地址，就是所fname的地址改变了，  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //但是调用函数不能改变实参指针变量的值，也就是地址，故main函数中的fname的值不会变  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            /\*\*//*    while(\*fname!='

3、通过改变main函数中的字符串数组的值，来改变链表中对应的值

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;stdio.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;malloc.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)typedef struct LNode  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    FILE \*fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    char \*fname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    struct LNode \*next;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}LNode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)LNode \*LinkList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)LNode \*ListInit(LNode \*linklist)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if((linklist=(LNode \*)malloc(sizeof(LNode)))==NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("Init Link Error");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    linklist-&gt;next=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)ListInsert(LNode \*linklist, char \*fname,FILE \*fp)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    LNode \*lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  LNode \*p;     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  p=linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if((lnode=(LNode\*)malloc(sizeof(LNode)))==NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    printf("Insert Error");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                while(p-&gt;next!=NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    p=p-&gt;next;                      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    if(!strcmp(p-&gt;fname,fname))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            p-&gt;fp=fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            return ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p-&gt;next=lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;fp=fp;          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;fname=fname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                printf("...........after lnode-&gt;fname=%s ",lnode-&gt;fname);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;next=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p=lnode;          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    LinkList = linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)FILE \*openFile(char \*fname)