第15周 项目3-在oj上玩指针<3>

本文通过一个简单的C++程序示例介绍了如何使用指针作为函数参数来实现两个整数变量的值交换。程序首先定义了一个名为jiaohuan的函数,该函数接收两个指向整数的指针作为参数。在主函数中,用户输入两个整数值,通过调用jiaohuan函数实现了这两个变量的值交换,并最终输出交换后的结果。



/* 
 * Copyright (c) 2014, 烟台大学计算机学院 
 * All rights reserved. 
 * 文件名称:test.cpp 
 * 作    者:呼亚萍 
 * 完成日期:2014年 12 月 8日 
 * 版 本 号:v1.0 
 * 
 * 问题描述:指针当形参,将调用函数进行变量的交换
 * 输入描述:相应的程序
 * 程序输出:运算结果
 */ 

#include <iostream>
using namespace std;
void jiaohuan(int *p1, int *p2);
int main( )
{
   int a,b;
   cin>>a>>b;
   jiaohuan(&a,&b);
   cout<<a<<" "<<b<<endl;
   return 0;
}
void jiaohuan(int *p1,int *p2)
{
    int t;
    t=*p1;
    *p1=*p2;
    *p2=t;
    return;
}

运算结果:


知识点总结:

指针作形参,进行变量的交换,指针的应用

学习心得:

静下心来,认真学习,加油!

题目 3 用户 YyYy 提交时间 2025/11/16 21:51:44 编译器 C (gcc) 内存 0 / 65536 KB 用时 0 / 400 ms 状态 编译错误 分数 0 / 10 评测时间 2025/11/16 21:51:44 提交代码 复制内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 void pur_LinkList(LinkList L){ PNode p=L->next; PNode ppre=L; while(p!=NULL){ PNode x=p->next; PNode pre=p; while(x!=NULL){ if(x->data==p->data){ pre->next=x->next; free(x); x=x->next; } else{ pre=x; x=x->next; } } p=p->next; } } 编译器输出 a.c: In function ‘pur_LinkList’: a.c:69:5: error: unknown type name ‘PNode’; did you mean ‘LNode’? 69 | PNode p=L->next; | ^~~~~ | LNode a.c:69:13: warning: initialization of ‘int’ from ‘struct node *’ makes integer from pointer without a cast [-Wint-conversion] 69 | PNode p=L->next; | ^ a.c:70:5: error: unknown type name ‘PNode’; did you mean ‘LNode’? 70 | PNode ppre=L; | ^~~~~ | LNode a.c:70:16: warning: initialization of ‘int’ from ‘LinkList’ {aka ‘struct node *’} makes integer from pointer without a cast [-Wint-conversion] 70 | PNode ppre=L; | ^ a.c:71:12: warning: comparison between pointer and integer 71 | while(p!=NULL){ | ^~ a.c:72:9: error: unknown type name ‘PNode’; did you mean ‘LNode’? 72 | PNode x=p->next; | ^~~~~ | LNode a.c:72:18: error: invalid type argument of ‘->’ (have ‘int’) 72 | PNode x=p->next; | ^~ a.c:73:9: error: unknown type name ‘PNode’; did you mean ‘LNode’? 73 | PNode pre=p; | ^~~~~ | LNode a.c:74:16: warning: comparison between pointer and integer 74 | while(x!=NULL){ | ^~ a.c:75:17: error: invalid type argument of ‘->’ (have ‘int’) 75 | if(x->data==p->data){ | ^~ a.c:75:26: error: invalid type argument of ‘->’ (have ‘int’) 75 | if(x->data==p->data){ | ^~ a.c:76:20: error: invalid type argument of ‘->’ (have ‘int’) 76 | pre->next=x->next; | ^~ a.c:76:28: error: invalid type argument of ‘->’ (have ‘int’) 76 | pre->next=x->next; | ^~ a.c:77:22: warning: passing argument 1 of ‘free’ makes pointer from integer without a cast [-Wint-conversion] 77 | free(x); | ^ | | | int In file included from a.c:3: /usr/include/malloc.h:62:25: note: expected ‘void *’ but argument is of type ‘int’ 62 | extern void free (void *__ptr) __THROW; | ~~~~~~^~~~~ a.c:78:20: error: invalid type argument of ‘->’ (have ‘int’) 78 | x=x->next; | ^~ a.c:82:20: error: invalid type argument of ‘->’ (have ‘int’) 82 | x=x->next; | ^~ a.c:85:12: error: invalid type argument of ‘->’ (have ‘int’) 85 | p=p->next; | ^~ a.c:70:11: warning: unused variable ‘ppre’ [-Wunused-variable] 70 | PNode ppre=L; | ^~~~ a.c: In function ‘Creat_LinkList’: a.c:23:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 23 | scanf("%d",&x); | ^~~~~~~~~~~~~~ a.c:30:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 30 | scanf("%d",&x); | ^~~~~~~~~~~~~~
11-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值