Ha593 2022-05-21 16:51 采纳率: 95.2%
浏览 26
已结题

C++函数之间的参数传递

int List(int *a,int *b){…
a=1,b=2
}

int Initlist(int *c,int *d){…}

如果希望让*a=c,b=*d,应该怎么做

  • 写回答

2条回答 默认 最新

  • fortunely2 2022-05-22 01:53
    关注

    "如果希望让a=c,b=d,应该怎么做" 要求可能是错误的,因为c是int类型,一个指针,而a 是int类型。

    另外,你写的代码也有很大问题,很多地方写得不正确。
    下面是我根据你的意思,写的一个C风格的table。

    #include <iostream>
    using namespace std;
    
    static const int kTableInitSize = 100;
    struct table {
        int size; //顺序表的容量大小(形参) 
        int length;  //顺序表的有效长度,及数组中的元素个数 
        int* head;//动态数组 
    };
    
    table InitList(table& t) { // 这里必须引用传值
        int n;
        cout << "输入n的值:"; 
        cin >> n;
    
        // 确保数组有足够空间
        if (n < kTableInitSize)
            t.head = new int[kTableInitSize];
        else
            t.head = new int[n + kTableInitSize];
    
        // 手动输入table的n个值
        for (int i = 0; i < n; ++i) {
            cout << "输入第" << i + 1 << "个数据:";
            cin >> t.head[i]; 
            t.length++;
        }
    }
    
    void DestroyList(table& t) { // 销毁table
        delete[] t.head;
    }
    
    void PrintList(table& t) { // 打印table
        cout << "{";
        for (int i = 0; i < t.length; ++i) {
            cout << t.head[i];
            if (i < t.length - 1)
                cout << ", ";
        }
        cout << "}" << endl;
    }
    int main()
    {
        table t;
        InitList(t);
        PrintList(t); 
        DestroyList(t);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月30日
  • 已采纳回答 5月22日
  • 创建了问题 5月21日

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)