简体   繁体   English

重新类型转换一个变量,可能吗?

[英]Re-typecasting a variable, possible?

Is it possible to recast the a variable permanently, or have a wrapper function such that the variable would behave like another type?是否可以永久重铸 a 变量,或者有一个包装器 function 使得该变量的行为类似于另一种类型?

I would want to achieve something I posted in the other question: Typecasting variable with another typedef我想实现我在另一个问题中发布的内容: Typecasting variable with another typedef

Update: Added GCC as compiler.更新:添加 GCC 作为编译器。 May have a extension that would help?可能有一个可以帮助的扩展?

Yes, you can cast a variable from one type to another:是的,您可以将变量从一种类型转换为另一种类型:

 int x = 5;
 double y = (double) x; // <== this is what a cast looks like

However, you cannot modify the type of the identifier 'x' in-place, if that is what you are asking.但是,如果您要求的是,您不能就地修改标识符“x”的类型。 Close to that, though, you can introduce another scope with that identifier redeclared with some new type:不过,接近于此,您可以引入另一个 scope ,该标识符用一些新类型重新声明:

  int x = 5;
  double y = (double) x;
  {
      double x = y; // NOTE: this isn't the same as the 'x' identifier above
      // ...
  }
  // NOTE: the symbol 'x' reverts to its previous meaning here.

Another thing you could do, though it is really a horrible, horrible idea is:你可以做的另一件事是:

  int x = 5;
  double new_version_of_x = (double) x;  // Let's make 'x' mean this
  #define x new_version_of_x
  // The line above is pure evil, don't actually do it, but yes,
  // all lines after this one will think 'x' has type double instead
  // of int, because the text 'x' has been rewritten to refer to
  // 'new_version_of_x'. This will likely lead to all sorts of havoc

You accomplish that by casting then assigning.你通过转换然后分配来实现这一点。

int f(void * p) {
  int * i;

  i = (int *)p;

  //lots of code here with the i pointer, and every line
  //really thinks that it is an int pointer and will treat it as such
} 

EDIT From the other question you linked:编辑从您链接的另一个问题:

typedef struct {
  unsigned char a;
  unsigned char b; 
  unsigned char c;
} type_a;

typedef struct {
 unsigned char e;
 unsigned char f[2];
} type_b;

//initialize type a
type_a sample;
sample.a = 1;
sample.b = 2;
sample.c = 3;

Now sample is initialized, but you want to access it differently, you want to pretend that in fact that variable has another type, so you declare a pointer to the type you want to "disguise" sample as:现在sample已初始化,但是您想以不同的方式访问它,您想假装该变量实际上具有另一种类型,因此您声明了一个指向要“伪装” sample的类型的指针:

type_b * not_really_b;
not_really_b = (type_b*)&sample;

See, that is the whole magic.看,这就是全部的魔法。

not_really_b->e is equal 1 not_really_b->e等于 1

not_really_b->f[0] is equal 2 not_really_b->f[0]等于 2

not_really_b->f[1] is equal 3 not_really_b->f[1]等于 3

Does this answer your question?这回答了你的问题了吗?

The other answers are better (declare a variable of the type you want, and do an assignment).其他答案更好(声明您想要的类型的变量,并进行赋值)。 If that's not what you're asking for, you could use a macro:如果这不是您所要求的,您可以使用宏:

long i;
#define i_as_int ((int)i)

printf( "i = %ld\n", i);
printf( "i = %d\n", i_as_int);

But wouldn't it be clearer to just say (int) i if that's what you mean?但是如果这就是你的意思,那么说(int) i不是更清楚吗?

As long as you realize in C pointers are nothing but addresses of memory 
locations of certain types, you should have your answer. For example the
following program will print the name of the file

int main(int argc, char *argv[]) {
    int *i;
    i = (int *) argv[0];
    printf("%s\n", argv[0]);
    printf("%s\n", ((char *) i));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM