将指针的地址传递给func,该func不会接收指向指针的指针 [英] passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer

查看:63
本文介绍了将指针的地址传递给func,该func不会接收指向指针的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

#include< stdlib.h>


//警告:函数的签名应该是be:f(int **)

void f(int * ip){

static int dummy = 5;

* ip =& ;假;

}


int main(){

int * ip;

f( & ip);

printf("%d",* ip);

返回0;

}


函数的签名应为:f(int **)。但是,f(int *)似乎

产生相同的结果,即使我是由编译器警告的
(Borland_bcc_5.5):

警告W8069 solution.c 5:函数中的非便携式指针转换f

警告W8075 solution.c 12:函数main中的可疑指针转换


什么可以当你将指针的地址传递给一个函数时,会出错?

没有收到指针的指针?


TIA

jimjim

#include <stdio.h>
#include <stdlib.h>

//WARNING: The function''s signature should be: f( int ** )
void f(int *ip) {
static int dummy = 5;
*ip = &dummy;
}

int main(){
int *ip;
f(&ip);
printf("%d",*ip);
return 0;
}

The function''s signature should be: f( int ** ). However, f( int *) seems to
produce the same results, even though I am
warned by the compiler (Borland_bcc_5.5):
Warning W8069 solution.c 5: Nonportable pointer conversion in function f
Warning W8075 solution.c 12: Suspicious pointer conversion in function main

What can go wrong when you pass the address of a pointer to a function that
does not recieve a pointer to a pointer?

TIA
jimjim

推荐答案

jimjim写道:
jimjim wrote:
当你通过时会出现什么问题
指向
没有接收指针指针的函数的指针地址?
What can go wrong when you pass
the address of a pointer to a function that
does not recieve a pointer to a pointer?




函数转换可能导致什么?

没有考虑到你可能有错误的类型传递了




新闻组的哲学,

是值得了解的如何编写已定义的代码,

并且不值得确切地知道

未定义的代码可能会发生什么。


-

pete



Whatever might be caused by the function translation
not taking into consideration that you may have
passed in the wrong type.

The philosophy of the newsgroup,
is that it is worth knowing how to write defined code,
and not worth knowing just exactly
what might happen with undefined code.

--
pete


jimjim schrieb:
jimjim schrieb:
#include< stdio.h>
#include< stdlib.h>

//警告:函数的签名应为:f(int **)
void f(int * ip){
static int dummy = 5;
* ip =& dummy;
}
int main(){
int * ip;
f(& ip) ;
printf("%d",* ip);
返回0;
}

函数的签名应为:f(int * *)。但是,f(int *)似乎会产生相同的结果,即使我被编译器警告(Borland_bcc_5.5):
警告W8069 solution.c 5:不可移植的指针转换在函数中f
警告W8075 solution.c 12:函数main中的可疑指针转换

当你将指针的地址传递给
没有的函数时会出现什么问题收到一个指针指针?
#include <stdio.h>
#include <stdlib.h>

//WARNING: The function''s signature should be: f( int ** )
void f(int *ip) {
static int dummy = 5;
*ip = &dummy;
}

int main(){
int *ip;
f(&ip);
printf("%d",*ip);
return 0;
}

The function''s signature should be: f( int ** ). However, f( int *) seems to
produce the same results, even though I am
warned by the compiler (Borland_bcc_5.5):
Warning W8069 solution.c 5: Nonportable pointer conversion in function f
Warning W8075 solution.c 12: Suspicious pointer conversion in function main

What can go wrong when you pass the address of a pointer to a function that
does not recieve a pointer to a pointer?




以上似乎工作通过纯粹,愚蠢的运气或运气不好。

它有效,如果

1)int **的表示与int *的表示相同

2)转换地址 - > int->地址产生原始的
地址

3)int *可以存储在int(这个如果int *的大小与int的大小相同,则可以保持。

这只是一个粗略的描述,而不是通过

标准。这意味着,如果你想到的话,如果sizeof(int *)!= sizeof(int),那么它很容易就会破坏或者有时会破坏

这是不太可能的

64位架构。


就是不要这样做。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



The above seems to "work" by pure, dumb luck or bad luck.
It works, if
1) the representation of an int** is the same as for an int*
2) the conversion address->int->address yields the original
address
3) an int* can be stored in an int (this can hold if the size
of an int* is the same as the size of an int).
This is just a rough description, not by the very word of the
standard. This means, it can easily break or sometimes break
if sizeof (int*) != sizeof (int) which is not that unlikely
in the future if you think of 64 bit architectures.

Just do not do that.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


2006年3月25日星期六11:37:格林尼治标准时间40,jimjim < ne ***** @ blueyonder.co.uk>

写道:
On Sat, 25 Mar 2006 11:37:40 GMT, "jimjim" <ne*****@blueyonder.co.uk>
wrote:
#include< stdio.h>
# include< stdlib.h>

//警告:函数的签名应为:f(int **)
void f(int * ip){
static int dummy = 5;
* ip =& dummy;


由于错误的调用语句,此语句调用

未定义的行为。

未定义行为的一个更阴险的表现是似乎按预期工作。运气不好。

不是最糟糕的运气(它可能已经格式化了你的硬盘)或者很好

运气(一个容易诊断的软件故障)。

}

int main(){
int * ip;
f(& ip);
printf("%d",* ip) ;
返回0;
}

函数的签名应为:f(int **)。但是,f(int *)似乎会产生相同的结果,即使我被编译器警告(Borland_bcc_5.5):
警告W8069 solution.c 5:不可移植的指针转换在函数中f
警告W8075 solution.c 12:函数main中的可疑指针转换


如果你想要忽略警告,但你真的需要了解所有

的影响。

当你将指针的地址传递给
没有收到指针指针的函数时,会出现什么问题?
#include <stdio.h>
#include <stdlib.h>

//WARNING: The function''s signature should be: f( int ** )
void f(int *ip) {
static int dummy = 5;
*ip = &dummy;
As a result of the faulty calling statement, this statement invokes
undefined behavior. One of the more insidious manifestations of
undefined behavior is to "appear to work as intended." Just bad luck.
Not the worst luck (it could have formatted your hard drive) or good
luck (an easily diagnosed software failure).
}

int main(){
int *ip;
f(&ip);
printf("%d",*ip);
return 0;
}

The function''s signature should be: f( int ** ). However, f( int *) seems to
produce the same results, even though I am
warned by the compiler (Borland_bcc_5.5):
Warning W8069 solution.c 5: Nonportable pointer conversion in function f
Warning W8075 solution.c 12: Suspicious pointer conversion in function main
Ignore the warnings if you want but you really need to understand ALL
the repercussions.

What can go wrong when you pass the address of a pointer to a function that
does not recieve a pointer to a pointer?


让我们只考虑你的代码和一个需要在四字节边界上对齐的int
的系统。当你从f返回时,ip包含

地址5.你的printf语句说转到地址5并提取

那里的int值。什么时候硬件会做什么?
试图提取一个未正确对齐的int。


如果int和地址不共享的系统怎么办?相同的

表示。如果您编写类似

int * ptr;

ptr =(int *)dummy;

编译器知道您正在将int转换为一个指针,可以生成代码来正确执行转换。在你的
代码中,编译器认为你为int分配了一个int值,并且

认为不需要任何转换。 ip中的值可能是陷阱

表示(这将是好运)。

删除电子邮件的del


Let''s just consider your code and a system that requires an int to be
aligned on a four-byte boundary. When you return from f, ip contains
the address 5. Your printf statement says go to address 5 and extract
the value of the int that is there. What will the hardware do when it
attempts to extract an int that is not aligned properly.

What about systems where an int and an address do not share the same
representation. If you code something like
int *ptr;
ptr = (int*)dummy;
the compiler knows you are converting an int to a pointer and can
generate the code to to perform the conversion properly. In your
code, the compiler believes you assigning an int value to an int and
sees no need for any conversion. The value in ip might be a trap
representation (that would be good luck).
Remove del for email


这篇关于将指针的地址传递给func,该func不会接收指向指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆