无法更改char*c指向的地址
我在这个函数中接收到一个缓冲区,我想通过将缓冲区地址增加1来忽略第一个字符 我增加了缓冲区,但是在函数的外面缓冲区包含了接收到的数据,但是没有增加 真奇怪!!谁能帮帮我吗无法更改char*c指向的地址,c,char,mbedtls,C,Char,Mbedtls,我在这个函数中接收到一个缓冲区,我想通过将缓冲区地址增加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; in
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 );
}
正如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.
请注意,我还在测试函数内递增指针,但由于指针本身是通过值传递的,所以在测试函数外不会递增
为了实现所需,需要通过引用将指针作为指针传递:
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 );
}
我认为您应该在将指针buf传递给函数“mbedtls\u net\u recv”之前增加它,如下所示
/* This call will not block */
buf = buf + 1;
recv = mbedtls_net_recv( ctx, buf, len );
printf("Receiving\n");
return( recv );
c中的参数是按值传递的。不能在函数中更改它们。