帮助将整数poniter传递给函数 [英] Help with passing integer poniter to function
问题描述
我的程序中遇到了奇怪的问题。
我无法粘贴整个程序因为它很大所以只是粘贴
行我认为是必要的。
我传递一个整数数组指针指向一个函数。
**********
int套接字[MAX_TCPCLIENTS]; / *一组连接的插座* /
......
......
fprintf(stderr," ;值为%d",套接字);
accept_new_tcp_client(套接字,& readset,listenfd);
....... < br $> b $ b .......
void accept_new_tcp_client(int * sockets,fd_set * readset,int listenfd)
{
struct sockaddr * signaddr;
int addrlength;
int i;
int clientsockfd;
addrlength = sizeof(signaddr);
fprintf(stderr,\ n值为%d,套接字);
/ * ==首先接受新连接== * /
if((clientsockfd = accept(listenfd,(struct
sockaddr *)& signaddr,& ; addrlength))< 0)
{
printf(\ n接收连接时出现问题);
退出(EXIT_FAILURE );
}
fprintf(stderr,\ nAccepted一个新的客户端连接);
fpr intf(stderr,\ n值为%d,套接字);
********
打印输出为
.....
价值12643936
价值12643936
接受一个新的客户连接
值是0分段错误(核心转储)
任何人都可以探索发生什么事情???
干杯
Vishal
ma *********** @ gmail.com 写道:
我我的程序中有一个奇怪的问题。
我无法粘贴整个程序,因为它很大,只需粘贴我认为必要的
行。
我是将整数数组指针传递给函数。
int插槽[MAX_TCPCLIENTS]; / *一组连接的套接字* /
.....
.....
fprintf(stderr,值是%d,套接字);
accept_new_tcp_client(套接字,& readset,listenfd);
......
......
void accept_new_tcp_client(int * sockets,fd_set * readset ,int
listenfd){
struct sockaddr * signaddr;
int addrlength;
int i;
int clientsockfd;
addrlength = sizeof(signaddr);
fprintf(stderr,\ n值是%d,套接字);
这是您列表中的第二个''值是12643936'?
/ * ==首先接受新连接= = * /
if((clientsockfd = accept(listenfd,(struct
sockaddr *)& signaddr,& addrlength))< 0)
{
printf(接受连接时出现问题);
退出(EXIT_FAILURE);
}
fprintf(stderr,\ n接受新的客户端连接 );
fprintf(stderr,\ n值是%d,套接字);
这个产生错误?
********
打印输出是
....
值为12643936
值为12643936
接受新的客户端连接
值为0分段错误(核心转储) )
任何人都可以探索发生什么事情???
如果我的假设是正确的[上面],那么我会*猜测*某事'是
在你的''accept()'调用中搞砸了堆栈 - 如果你发表评论,
fprintf工作正常吗?
在对accept()的调用中,你将signaddr转换为''struct sockaddr *'' -
它已经是......
>
struct sockaddr * signaddr
但是,你通过使用& signaddr传递struct sockaddr。
- < br $>
==============
不是学生
========== ====
ma *********** @ gmail.com 写道:
我的程序中有一个奇怪的问题。
我无法粘贴整个程序,因为它很大,所以只需粘贴我认为必要的
行。
我将一个整数数组指针传递给一个函数。
不,你不是。您正在将int *或ptr-to-int传递给函数。
**********
int socket [MAX_TCPCLIENTS]; / *一组连接的套接字* /
.....
.....
fprintf(stderr,值为%d,套接字);
这是错误的。 fpritnf()期望%d的int你已经传递了一个
数组名称,该名称衰减了(自动转换)到
一个int *。您有未定义的行为(非常糟糕)您的程序'
行为无法预测。
您认为此声明有何作用?你的意思是
打印套接字[0]?
注意你在几个地方使用相同的破碎构造
< snip>
void accept_new_tcp_client(int * sockets,fd_set * readset,int listenfd)
正如我在上面提到的套接字是一个int *
< snip>
修复未定义的行为并重试。
-
Nick keighley
Nick Keighley写道:ma *********** @ gmail.com 写道:
我我的程序中有一个奇怪的问题。
我无法粘贴整个程序,因为它很大,只需粘贴我认为必要的
行。
我是将整数数组指针传递给函数。
不,你不是。您正在将int *或ptr-to-int传递给函数。
**********
int socket [MAX_TCPCLIENTS ]。 / *一组连接的套接字* /
.....
.....
fprintf(stderr,值是%d,套接字);
这是错误的。 fpritnf()期望%d的int你已经传递了一个衰变的数组名称。 (自动转换)到一个int *。你有未定义的行为(非常糟糕)你的程序的行为无法预测。
你认为这个陈述有什么作用?你的意思是打印插座[0]吗?
注意你在几个地方使用相同的破碎构造
< snip>
void accept_new_tcp_client(int * sockets,fd_set * readset,int
listenfd)
正如我在上面提到的那样,socket是一个int *
<修复未定义的行为并重试。
/看/它是有效的 - 可能是
输出数组地址的东西是作为一个
诊断辅助工具来跟踪原始问题?
根据我的经验,*大多数*人似乎使用%d与%p进行必要的
投票来检查地址,并且只要一个sizeof(int)== sizeof(?*)
那里不是很多可以出错的吗?
-
==============
不是学生
==============
Hi,
I am having strange problem in my Program.
I cannot paste the whole program as it is huge so just pasting the
lines i think are necessary.
I am passing a integer array pointer to a function.
**********
int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
......
......
fprintf(stderr,"The value is %d",sockets);
accept_new_tcp_client(sockets,&readset,listenfd);
.......
.......
void accept_new_tcp_client(int *sockets, fd_set *readset, int listenfd)
{
struct sockaddr *signaddr;
int addrlength;
int i;
int clientsockfd;
addrlength = sizeof(signaddr);
fprintf(stderr,"\nThe value is %d",sockets);
/*== First accept the new connection ==*/
if((clientsockfd = accept(listenfd,(struct
sockaddr*)&signaddr,&addrlength))<0)
{
printf("\nSome Problem with accepting connection");
exit(EXIT_FAILURE);
}
fprintf(stderr,"\nAccepted a new Client connection");
fprintf(stderr,"\nThe value is %d",sockets);
********
The print output is
.....
The value is 12643936
The value is 12643936
Accepted a new Client connection
The value is 0Segmentation fault (core dumped)
Can anyone explians whats happening???
Cheers
Vishal
ma***********@gmail.com wrote:Hi,
I am having strange problem in my Program.
I cannot paste the whole program as it is huge so just pasting the
lines i think are necessary.
I am passing a integer array pointer to a function.
**********
int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
.....
.....
fprintf(stderr,"The value is %d",sockets);
accept_new_tcp_client(sockets,&readset,listenfd);
......
......
void accept_new_tcp_client(int *sockets, fd_set *readset, int
listenfd) {
struct sockaddr *signaddr;
int addrlength;
int i;
int clientsockfd;
addrlength = sizeof(signaddr);
fprintf(stderr,"\nThe value is %d",sockets);
This is the second ''The value is 12643936'' in your list?
/*== First accept the new connection ==*/
if((clientsockfd = accept(listenfd,(struct
sockaddr*)&signaddr,&addrlength))<0)
{
printf("\nSome Problem with accepting connection");
exit(EXIT_FAILURE);
}
fprintf(stderr,"\nAccepted a new Client connection");
fprintf(stderr,"\nThe value is %d",sockets);
And this one produces the error?
********
The print output is
....
The value is 12643936
The value is 12643936
Accepted a new Client connection
The value is 0Segmentation fault (core dumped)
Can anyone explians whats happening???
If my assumptions are correct [above], then I would *guess* that something''s
screwing up the stack in your call to ''accept()'' - if you comment that out,
the fprintf works ok?
In the call to accept(), you''re casting signaddr to a ''struct sockaddr *'' -
which it already is ...
struct sockaddr *signaddr
But, you''re passing a struct sockaddr ** by using &signaddr.
--
==============
Not a pedant
==============
ma***********@gmail.com wrote:
I am having strange problem in my Program.
I cannot paste the whole program as it is huge so just pasting the
lines i think are necessary.
I am passing a integer array pointer to a function.
no you arn''t. You are passing an int* or ptr-to-int to a function.
**********
int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
.....
.....
fprintf(stderr,"The value is %d",sockets);
this is wrong. fpritnf() expects an int for %d you''ve passed an
array name which "decays" (is automatically converted) into
an int*. You''ve got undefined behaviour (Very Bad) your program''s
behaviour cannot be predicted.
What do you think this statement does? Did you mean to
print sockets[0]?
Note you use the same broken constuct in several places
<snip>
void accept_new_tcp_client(int *sockets, fd_set *readset, int listenfd)
as I noted above sockets is an int*
<snip>
fix the Undefined Behaviour and try again.
--
Nick keighley
Nick Keighley wrote:ma***********@gmail.com wrote:I am having strange problem in my Program.
I cannot paste the whole program as it is huge so just pasting the
lines i think are necessary.
I am passing a integer array pointer to a function.
no you arn''t. You are passing an int* or ptr-to-int to a function.**********
int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
.....
.....
fprintf(stderr,"The value is %d",sockets);
this is wrong. fpritnf() expects an int for %d you''ve passed an
array name which "decays" (is automatically converted) into
an int*. You''ve got undefined behaviour (Very Bad) your program''s
behaviour cannot be predicted.
What do you think this statement does? Did you mean to
print sockets[0]?
Note you use the same broken constuct in several places
<snip>void accept_new_tcp_client(int *sockets, fd_set *readset, int
listenfd)
as I noted above sockets is an int*
<snip>
fix the Undefined Behaviour and try again.
I *think* he just wanted the address - to /see/ that it''s valid - possibly
the stuff that outputs the address of the array was put in place as a
diagnostic aid to trace the original problem?
In my experience, *most* people seem to use %d vs. %p with the necessary
cast to check addresses, and as long as an sizeof(int) == sizeof(? *)
there''s not a lot that can go wrong with it is there?
--
==============
Not a pedant
==============
这篇关于帮助将整数poniter传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!