我在这个函数中收到一个缓冲区,我想通过将缓冲区地址递增1来忽略第一个字符。
我增加了缓冲区但是在缓冲区包含接收数据的函数外面,但它没有递增。
很奇怪!!任何人都可以帮助我!!
int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
uint32_t timeout )
{
int ret,recv;
struct timeval tv;
fd_set read_fds;
int fd = ((mbedtls_net_context *) ctx)->fd;
if( fd < 0 )
return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
FD_ZERO( &read_fds );
FD_SET( fd, &read_fds );
tv.tv_sec = timeout / 1000;
tv.tv_usec = ( timeout % 1000 ) * 1000;
ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
/* Zero fds ready means we timed out */
if( ret == 0 )
return( MBEDTLS_ERR_SSL_TIMEOUT );
if( ret < 0 )
{
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32)
if( WSAGetLastError() == WSAEINTR )
return( MBEDTLS_ERR_SSL_WANT_READ );
#else
if( errno == EINTR )
return( MBEDTLS_ERR_SSL_WANT_READ );
#endif
return( MBEDTLS_ERR_NET_RECV_FAILED );
}
/* This call will not block */
recv = mbedtls_net_recv( ctx, buf, len );
buf = buf + 1;
printf("Receiving\n");
return( recv );
}
答案 0 :(得分:0)
就像Eugene Sh所说的那样,C中的参数是按值传递的。
示例:
void Test(int value)
{
value++;
}
...
int foo = 3;
Test(foo);
// here, foo is still 3
如果你想在C中通过引用传递foo
,你需要传递它的指针
void Test(int* value)
{
*value++;
value++;
}
...
int foo = 3;
int *fooPtr = &foo;
Test(fooPtr);
// Now, foo is 4, but fooPtr still is &foo.
请注意,我还增加了Test()
函数内的指针,但由于指针本身是按值传递的,因此它不会在Test()
函数之外递增。
为了达到你想要的效果,你需要通过引用传递指针(作为指针):
void Test(int** value)
{
**value++;
*value++;
}
...
int foo = 3;
int *fooPtr = &foo;
Test(&fooPtr);
// Now, foo is 4, and fooPtr was incremented.
// This is just an example ; don't use fooPtr beyond this point, its value is wrong.
您需要将buf指针作为参考传递,以便能够更改指针值:
int mbedtls_net_recv_timeout( void *ctx, unsigned char **buf, size_t len,
uint32_t timeout )
{
[... snip ...]
/* This call will not block */
recv = mbedtls_net_recv( ctx, *buf, len );
*buf = *buf + 1;
printf("Receiving\n");
return( recv );
}
答案 1 :(得分:0)
我认为你应该在将指针buf传递给函数'mbedtls_net_recv'之前增加指针buf,
/* This call will not block */
buf = buf + 1;
recv = mbedtls_net_recv( ctx, buf, len );
printf("Receiving\n");
return( recv );