CC++void*

    xiaoxiao2023-03-24  3

    一开始在福富实习,根据导师给的参考样例第一次接触到void*无类型参数;

    在做数据结构的题中偶然尝试void传递,遇到了指针的指针与预想不一样的结构,于是就研究了一天。

    终是知道问题所在。

    每个变量都会有一个地址,而指针变量也会有一个地址。

    所以对于指针变量

     int*me;//显然&me类型是int**,me类型是int*;*me类型是int;

    int m;//&m类型int*;m类型int;

    传递int**只能改变int*和int

    传递int*只能改变int

    传递int不能改变

    ===============================

    note:

    void没有参数

    ------------------

    void*无类型参数

    不检查类型,无论void*还是void**都一样作用,可互相赋值

    使用需要给定类型或者单位长度,详情memcpy源码样例

       void* memcpy(void* dest, void* source, size_t count)

          {

               void* ret = dest;

              //copy from lower address to higher address

              while (count--)

                      *dest++ = *source;

               return ret;

          }

    =========================================

    第一个例子符合想要的结果。每次new完,主函数中的p指向就被被改变了;(mypoint**)正好是强制转换获得存储mypoint地址的指针;

    而后前面价格星*,标识更改指针所存储的mypoint地址;

    /* 输入文件示例 3 3 1 3 2 3 5 4 6 2 8 7 输出文件示例 18 18 2 1 3 3 2 7 1 */ #include<iostream> using namespace std; struct mypoint { mypoint*next,*pre; int *array; int sf,so; int h,t; mypoint(int hh,int tt) { h=hh;//indexfront t=tt;//indextail sf=0;//half sun before so=0;//total sum } }; int deleteNode(void*nowp) { mypoint*p; p=(mypoint*)nowp; if(p->next==p)return -1; p->next->pre=p->pre; p->pre->next=p->next; delete p; return 1; } void newNode(void*before,void*intnode,int index) { mypoint*hd,*tmp; tmp=new mypoint(0,index-1); tmp->array=(int*)intnode; hd=*(mypoint**)before;//before=&p;hd=p; tmp->next=hd->next; tmp->pre=hd; hd->next->pre=tmp; hd->next=tmp; cout<<"IN_ hd=p"<<hd<<endl;//p; cout<<"IN_ before=&p"<<before<<endl; *(mypoint**)before=tmp; //p=tmp; cout<<"OUT_ (*(mypoint**)before)=p"<<hd<<endl; // return tmp; } int main(int argc,char*argv[]) { mypoint *head,*p; int **ck; head=new mypoint(0,0); p=head; head->next=head; head->pre=head; int n,i,j,temp; ///========read cin>>n; ck=new int*[n]; j=n; while(j--) { cin>>temp; ck[j]=new int[temp]; cout<<"&p"<<&p<<endl; cout<<"p"<<p<<endl; newNode(&p,&ck[j][0],temp); cout<<"out_Fun dest"<<p<<endl; int midindex=(temp-1)/2; for(i=0;i<temp;i++) { cin>>ck[j][i]; if(i<=midindex) p->sf+=ck[j][i]; p->so+=ck[j][i]; } cout<<p->sf<<" ||"<<p->so<<endl; } while(head!=head->next)deleteNode(head->next); delete head; for(i=0;i<n;i++) delete[] ck[i]; delete[] ck; return 0; }

    最初的代码,显然每次new子函数里面,形参before改变,但是主函数的p没有被改变

    #include<iostream> using namespace std; struct mypoint { mypoint*next,*pre; int *array; int sf,so; int h,t; mypoint(int hh,int tt) { h=hh;//indexfront t=tt;//indextail sf=0;//half sun before so=0;//total sum } }; int deleteNode(void*nowp) { mypoint*p; p=(mypoint*)nowp; if(p->next==p)return -1; if(p->h<=p->t)return 0; p->next->pre=p->pre; p->pre->next=p->next; delete p; return 1; } void newNode(void*before,void*intnode,int index) { mypoint*hd,*tmp; tmp=new mypoint(0,index-1); tmp->array=(int*)intnode; hd=(mypoint*)before; tmp->next=hd->next; tmp->pre=hd; hd->next->pre=tmp; hd->next=tmp; cout<<"IN_func&before"<<&before<<endl; cout<<"before"<<before<<endl; *(mypoint*)before=*tmp;//赋值 cout<<"before->t :"<<((mypoint*)before)->t<<endl; cout<<"*(mypoint*)before=tmp"<<before<<endl; tmp->t=1; cout<<"[]"<<((mypoint*)before)->t<<endl; before=tmp;//子函数参数改变 cout<<"before->t :"<<((mypoint*)before)->t<<endl; cout<<"before=tmp _func"<<before<<endl; // return tmp; } int main(int argc,char*argv[]) { mypoint *head,*p; int **ck; head=new mypoint(0,0); p=head; head->next=head; head->pre=head; int n,i,j,temp; ///========read cin>>n; ck=new int*[n]; j=n; while(j--) { cin>>temp; ck[j]=new int[temp]; cout<<"main &p"<<&p<<endl; cout<<"main p"<<p<<endl; newNode(p,&ck[j][0],temp); cout<<"out_Fun dest"<<p<<endl; int midindex=(temp-1)/2; for(i=0;i<temp;i++) { cin>>ck[j][i]; if(i<=midindex) p->sf+=ck[j][i]; p->so+=ck[j][i]; cout<<p->sf<<" || "<<p->so<<endl; } } while(head!=head->next)deleteNode(head->next); delete head; for(i=0;i<n;i++) delete[] ck[i]; delete[] ck; return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1202710.html
    最新回复(0)