-2

I am receiving a buffer inside this function , I would like to ignore the first character by incrementing the buffer address by one.

I increment the buffer but out side the function the buffer contains the received data but it's not incremented.

It's strange !! could any one help me please !!

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 );
}
4

2 回答 2

0

就像 Eugene Sh 所说,C 中的参数是按值传递的。

例子 :

void Test(int value)
{
  value++;
}

...

int foo = 3;
Test(foo);
// here, foo is still 3

如果你想foo在 C 中通过引用传递,你需要传递它的指针

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 );
}
于 2018-04-11T16:38:42.567 回答
0

我认为你应该像这样在将指针 buf 传递给函数 'mbedtls_net_recv' 之前增加它,

/* This call will not block */
    buf = buf + 1;
    recv = mbedtls_net_recv( ctx, buf, len );
    printf("Receiving\n");
    return( recv );
于 2018-04-11T17:28:52.653 回答