読者です 読者をやめる 読者になる 読者になる

変数 = (式, 値); assert(値 == 変数);

C言語の文法にこんなのあったかなぁ..

#include <assert.h>
#include <string.h>
#define getVarint(p, v) (*(p+1) & 0xff)? (*v = *p, 1): get_varint(p, v)
unsigned get_varint(unsigned char* p, unsigned int* v) {
unsigned int a[4];
int i;
  assert(*p & 0xff);
  a[0] = *p++;
  a[1] = *p++;
  for(i = 2; *p; i++)
    a[i] = *p++;
int j;
  *v = 0;
  for(j=i-1;j>=0;j--) {
    *v = *v << 8;
    *v |= a[j];
  }
  return i;
}
int main(void) {
  unsigned int i = 0;
  unsigned int j;
  i = (j = 2, 3);
  assert(2 == j);
  assert(3 == i);
  {
  unsigned char buf[4];
  int i = 0x1234;
  int value;
  memset(buf, 0, 4 * sizeof(buf[0]));
  memcpy(buf, &i, sizeof(unsigned int));
  assert(2 == getVarint(buf, &value));
  assert(0x1234 == value);
  i = 0xab;
  memset(buf, 0, 4 * sizeof(buf[0]));
  memcpy(buf, &i, sizeof(unsigned int));  
  assert(1 == getVarint(buf, &value));
  assert(0xab == value);
 
  }
  return 0;
}