c语言二维数组指针出错两个数指针

关于C语言交换两个数(有关指针)_百度知道
关于C语言交换两个数(有关指针)
在C语言中,通过函数参数传递,进行两个数的交换,必须采用传递指针的方式,同时,在函数中,一定要对两个指针中的数据内容进行交换才能达到交换实参地址中数据的目的。如://正确的交换函数void&swap1(int&*p1,int&*p2){&&&&int&p;&&&&p=*p1;&//通过*引用地址中的数据,进行交换&&&&*p1=*p2;&&&&*p2=p;}//错误的交换函数void&swap2(int&*p1,int&*p2){&&&&int&*p;&&&&&p=p1;&//这里进行改变的是形参的值,而形参只是实参的一个副本,形参本身的变化,是不能改变实参的值的!!&&&&&p1=p2;&&&&p2=p;}PS:要正确认识C语言中的实参与形参,形参在使用中只是实参的副本,若形参是指针,我们可以通过形参改变实参地址中的内容,但无法改变实参本身的值!
其他类似问题
为您推荐:
实际上指针指向的数据还是没有被交换对吧,上面的swap交换的是指针指向的数据。而交换指针又有什么用呢区别在于。下面的swap你交换的是指针
如果在下面的swap函数中输出*p1和*p2也就是改为void swap(int *p1,int *p2){int *p;p=p1;p1=p2;p2=p;printf(&\n swap: *p1=%d,*p2=%d\n&,*p1,*p2);}输入结果是swap:*p1=22;*p2=12确实又交换了,如何理解?
因为你交换了指针。地方A有屋子A,地方B有屋子B。我(也即指针)手中有屋子A的地址,你手中有屋子B的地址。我们交换之后,我手中当然就有了屋子B的地址。但不代表 屋子A和屋子B的地方挪动了吧?
输出的*p1和*p2应该不是地址啊,是数据吧,去掉&*&后的p1和p2才是表示地址吧?难道我理解错了吗
其他2条回答
指针变量是指向数据的地址.不能向你下面这直接赋值.  
你要定义一个整型或者别的变量指针变量赋值的三种方法   int i1,i2上面的P你定义的是int的整型变量,*p2=i2,i3;
给指针变量赋予变量的地址; 用整形变量的地址给基类型为整形的指针变量赋值(2)p1=&i3;
通过指针变量给指针变量赋值,而你下面定义的是一个指针变量;(3)
p1=p2;(1)int *p1=&i1
指针p有自己的地址,该地址存指向的数据的地址,*p就是通过地址找到变量,第二个函数你只是交换了指针变量的地址,地址指向的数据是不会有变化的
c语言的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁共有 2454 人关注过本帖
标题:用指针完成对输入三个数从大到小顺序排列请大神看看我的程序为什么运行不了 ...
等 级:新手上路
帖 子:30
结帖率:90.91%
&&已结贴√
&&问题点数:7&&回复次数:9&&&
用指针完成对输入三个数从大到小顺序排列请大神看看我的程序为什么运行不了?
#include&stdio.h&
void main()
&&& int x,y,z;
&&& int *px,*py,*
&&& printf(&Input:\n&);
&&& scanf(&%d%d%d&,&x,&y,&z);
&&& px=&x;
&&& py=&y;
&&& pz=&z;
&&& if(px&py&pz)
&&&&&&&&printf(&*px=%d&,*px);
&&&&&&&&printf(&*py=%d&,*py);
&&&&&&&&printf(&*pz=%d&,*pz);
&&& if(py&px&pz)
&&&&&&&&printf(&*py=%d&,*py);
&&&&&&&&printf(&*px=%d&,*px);
&&&&&&&&printf(&*pz=%d&,*pz);
&&& if(pz&py&px)
&&&&&&&&printf(&*pz=%d&,*pz);
&&&&&&&&printf(&*py=%d&,*py);
&&&&&&&&printf(&*px=%d&,*px);
&&& if(px&pz&py)
&&&&&&&&printf(&*px=%d&,*px);
&&&&&&&&printf(&*pz=%d&,*pz);
&&&&&&&&printf(&*py=%d&,*py);
&&& if(py&pz&px)
&&&&&&&&printf(&*py=%d&,*py);
&&&&&&&&printf(&*pz=%d&,*pz);
&&&&&&&&printf(&*px=%d&,*px);
&&& if(pz&px&py)
&&&&&&&&printf(&*pz=%d&,*pz);
&&&&&&&&printf(&*px=%d&,*px);
&&&&&&&&printf(&*py=%d&,*py);
搜索更多相关主题的帖子:
等 级:论坛游民
专家分:35
if(px&py&pz)
修改为:if(*px&*py&*pz)
下同。。。。
另外。。。。这什么算法?还让计算机活不?
*才是取值.
Yesterday is history,tomorrow is mistery,but today is a gift.
来 自:神界
等 级:友情版主
威 望:338
帖 子:10962
专家分:43123
以下是引用Bccn_Billy在 09:39:30的发言:
if(px&py&pz)
修改为:if(*px&*py&*pz)
下同。。。。
另外。。。。这什么算法?还让计算机活不?
*才是取值.
不要误人子弟&&&有X&Y&Z这种语法吗&&&这貌似是纯数学语法
DO IT YOURSELF !
等 级:新手上路
帖 子:30
回复 2 楼 Bccn_Billy
可以运行了但是没有达到目的,输出三个值但是输不出来也没从大到小的排序,我这程序是哪里出错了。太感谢了
等 级:新手上路
帖 子:30
回复 3 楼 wp231957
那有什么更好的方法去做呢?用数组我会做但是用指针就毫无头绪
来 自:神界
等 级:友情版主
威 望:338
帖 子:10962
专家分:43123
以下是引用f在 09:52:03的发言:
那有什么更好的方法去做呢?用数组我会做但是用指针就毫无头绪
既然你会用数组做&&那就照搬好了&&我只是怀疑你是否真的会用数组做
DO IT YOURSELF !
等 级:新手上路
帖 子:30
回复 6 楼 wp231957
&&& 我试试吧。
等 级:论坛游民
专家分:35
回复 3 楼 wp231957
脑子一下子短路了,不好意思。
if (*px&*py && *py&*pz)
Yesterday is history,tomorrow is mistery,but today is a gift.
等 级:职业侠客
帖 子:74
专家分:335
用指针完成对输入三个数从大到小顺序排列,思路上跟两个数的大小比较一样。
#include&stdio.h&
void swap(int *px,int *py) //用指针实现数据交换
&temp=*&&*px=*&&&*py=
void main()
{ int x,&&y,
&&scanf(&%d%d%d&,&x,&y,&z);
&&if(z&x)&&&&&&&&&&&//第一个数和第三个数按大小排列
&&&&&&swap(&x,&z);
&&if(y&x)&&&&&&&&&&&//拿第二个数分别和第一个、第三个数比较
&&&&&swap(&x,&y);&&&&&&&&&&&&
&&else if(y&z)
&&&&&& swap(&y,&z);
&&printf(&after swap :x=%d,y=%d,z=%d\n&,x,y,z);
等 级:职业侠客
帖 子:74
专家分:335
看到这个题我想笑。因为以前我们学C语言的时候第九章指针里面有个例题,就是用指针实现两个数的交换。估计这个老师希望学生好好看看例题就会做了,结果学生完全没领会老师的意图,根本没想到用指针实现交换哈哈……
[ 本帖最后由 comewest 于
10:29 编辑 ]
版权所有,并保留所有权利。
Powered by , Processed in 0.027273 second(s), 8 queries.
Copyright&, BCCN.NET, All Rights Reservedc语言-用指针互换两个数_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
c语言-用指针互换两个数
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
你可能喜欢c语言怎样比较两个数的大小 指针 函数调用_百度知道
c语言怎样比较两个数的大小 指针 函数调用
我有更好的答案
b = 2,&b); *b = return 0;n&quot,a;,b),int *b){
printf(&quot,a;a=%d; temp = *a;}int main(){ int a = 1;n a=%d,b=%d&#92.h&gt.&#92#include &lt.,b=%d\;void swap(int *a; printf(& swap(&a.;n& *a = *b;stdio,b)
其他类似问题
为您推荐:
函数调用的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁1449人阅读
C语言(3)
在C语言中,我们常常用到的一个运算是让某个变量的值+1.
例如 M = M + 1。
而在实际运用中,我们发现
对于指针进行+1运算,算出来的结果是+4。
图中我们定义的&变量M 和指针Matrix如下:
int M = 3;
int* Matrix = {1,2,3};
可以看到,对于M和 Matrix ,+1运算的效果是不同的。
这个差异是因为C语言的标准中规定了 加法与减法运算对于地址的操作和对于值的操作是不同的,如下文中粗体所示:
3.3.6 Additive operators
& & additive-expression:
& & & & multiplicative-expression
& & & & additive-expression + multiplicative-expression
& & & & additive-expression - multiplicative-expression
Constraints
For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to an object type and the other shall have integral type. (Incrementing is equivalent to adding 1.)
For subtraction, one of the following shall hold:
& & * both operands
& & * both operands are pointers to qualified or unqualified versions of com or
& & * the left operand is a pointer to an object type and the right operand has integral type. (Decrementing is equivalent to subtracting 1.)
If both operands have arithmetic type, the usual arithmetic conversions are performed on them.
The result of the binary + operator is the sum of the operands.
The result of the binary - operator is the difference resulting from the subtraction of the second operand from the first.
When an expression that has integral type is added to or subtracted from a pointer, the integral value is first multiplied by the size of the object pointed to. The result has the type of the pointer operand.
If the pointer operand points to a member of an array object, and the array object is large enough, the result points to a member of the same array object, appropriately offset from the original member. Thus if P points to a member of an array
object, the expression P+1 points to the next member of the array object. Unless both the pointer operand and the result point to a member of the same array object, or one past the last member of the array object, the behavior is undefined. Unless both the
pointer operand and the result point to a member of the same array object, or the pointer operand points one past the last member of an array object and the result points to a member of the same array object, the behavior is undefined if the result is used
as the operand of a unary * operator.
当一个加法运算,加号左边的操作数是一个指针,而右边的操作数是一个整数时,这个整数值先乘以指针类型的大小(sizeof(int)),然后再加到左边的数上。
这就解答了标题所述的第一个问题。
而标准的描述中另外一个值得注意的点是,两个地址相减的值会是什么?
问题呈现如下:
同样答案在C标准当中,见下文粗体。
When two pointers to members of the same array object are subtracted, the difference is divided by the size of a member. The result represents the difference of the subscripts of the two array members.
The size of the result is implementation-defined, and its type (a signed integral type) is ptrdiff_t defined in the &stddef.h& header. As with any other arithmetic overflow, if the result does not fit in the space provided, the behavior is undefined.
If two pointers that do not point to members of the same array object are subtracted, the behavior is undefined. However, if P points either to a member of an array object or one past the last member of an array object, and Q points to the last member of the
same array object, the expression (Q+1) - P has the same value as (Q-P) + 1, even though Q+1 does not point to a member of the array object.
当同一个数组的两个成员的指针相减时,其差值为:地址值的差,再除以一个数组成员的size。这个结果代表了两个指针对应元素的下标之差。
所以大家才遇到了上图中所遇到的问题。这是C语言标准所规定的。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:84637次
积分:1009
积分:1009
排名:千里之外
原创:22篇
(1)(3)(9)(9)(6)

我要回帖

更多关于 c语言指针数组初始化 的文章

 

随机推荐