zoukankan      html  css  js  c++  java
  • C语言指针传参与C++引用传参,以及尾插法建立单链表使用到的引用

    先用传入数组做参

    #include <stdio.h>
    void sum(int *j){
        int i=0;
        while(i<4){
            j[0]+=j[i++];
        }
    }
    int main()
    {
        int j[]={1,2,3,4};
        sum(j);
        printf("j[0] in main is %d
    ",j[0]);
        return(0);
    }
    
    输出:j[0] in main is 11
    

    C中数组名就是指针,C中子函数用指针传参时,子函数会直接操作原值,而整形变量就不行,整形变量就不举例子了,看下面的如何用指针传参来改变原值

    变量的指针传参

    #include <stdio.h>
    void sum(int *a,int *b){
      *a=33,*b=65;
      printf("
    In sum,a= %d",*a);
      printf("
    In sum,b= %d",*b);
    }
    int main()
    {
      int a=32,b=64;
      int *pa,*pb;
      pa=&a;
      pb=&b;
      printf("
    In main,before sum,a= %d",a);
      printf("
    In main,before sum,b= %d",b);
      sum (pa,pb);
      printf("
    In main,after sum,a= %d",a);
      printf("
    In main,after sum,b= %d",b);
    }
    

    在sum函数中,需要用*a来操作原值(主函数的a),运行结果是

    In main,before sum,a= 32
    In main,before sum,b= 64
    In sum,a= 33
    In sum,b= 65
    In main,after sum,a= 33
    In main,after sum,b= 65
    

    在gcc编译时,也就是用C语言编写时,定义sum时,只能用指针,不能用引用。用引用会报错。网上好多讲c语言引用传参的,坑死了

    用g++编译时,才能用引用传参,也就是说C++才支持引用传递,C语言不支持!

    引用传参具体怎么用呢?请看下面的例子,add函数是引用传参,调用时,直接把变量名传进去。add函数会直接操作原值,不会在内部拷贝一份

    #include <stdio.h>
    int add(int &x,int &y);
    int main()
    {
        int a=3,b=5;
        int sum=add(a,b);
        printf("sum=%d
    ",sum);
        return 0;
    }
    int add(int &x,int &y)
    {
        x=x+1;
        y=y+1;
        return x+y;
    }
    
    sum=10
    

    用尾插法建立单链表,并输出。

    王道书的代码, 主要代码是C,但用到了C++的引用传参,需要用g++编译

    #include <stdio.h>
    #include <cstdlib>
    typedef struct LNode{
      int data;
      struct LNode *next;
    }LNode,*LinkList;     //LNode指示单个的节点,LinkList指示链表
    
    void List_TailInsert(LinkList &L){ 
      int x;
      L=(LinkList)malloc(sizeof(LNode));
      LNode *s,*r=L;    //r为表尾指针
      scanf("%d",&x);   //输入结点的值
      while(x!=9999){   //输入9999表示结束
        s=(LNode *)malloc(sizeof(LNode));
        s->data=x;
        r->next=s;
        r=s;        //r指向新的表尾结点
        scanf("%d",&x);
      }
      r->next=NULL;
      //return L;
    }
    
    void show(LinkList &L){
      LNode *p=L->next;
      int i=0;
      while(p!=NULL){
        printf("%d
    ",p->data);
        p=p->next;
      }
    }
    
    int main()
    {
      LinkList L1;
      List_TailInsert(L1);
      show(L1);
    }
    
    PS E:data-structure新建文件夹> g++ .3.尾插法使用引用.c
    PS E:data-structure新建文件夹> .a.exe
    1
    2
    3
    4
    9999
    1
    2
    3
    4
    
  • 相关阅读:
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
  • 原文地址:https://www.cnblogs.com/sq800/p/15516188.html
Copyright © 2011-2022 走看看