拾遗笔记

强制类型转换

long a =2;
int   intVar= (*((int*) (&(a))));
printf ("%d\n",intVar);
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct MS {
  int a;
  int b;
} my_union;

int main(int argc, char *argv[]){

  long    longvar;
  struct MS *m= malloc(sizeof(my_union));
  m->a=1;
  m->b=2;
  longvar= (*((long*) (&(m))));
  printf ("%ld\n",longvar);
  return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define CAST(new_type,old_object) (*((new_type *)(&(old_object))))

typedef struct MS {
  int a;
  int b;
} my_union;


int main(int argc, char *argv[]){

  long    longvar;
  struct MS *m= malloc(sizeof(my_union));
  m->a=1;
  m->b=2;
  longvar = CAST(long ,m);
  printf ("%ld\n",longvar);
  return 0;
}

Comments

comments powered by Disqus