anish
anish

Reputation: 1033

how to cast from one type to another in c

I have following code

#include <stdio.h>
#include<ctype.h>

typedef struct {
  int Type;
  int Type2;
}foo;

typedef struct {
  char cData[40];
}bar;

int main()
{
  bar b1;
  strcpy(b1.cData,"11");
  foo *f=(struct foo *)&b1;
  printf("Type is  %d \n",f->Type);
  return 0;
}

But i am not getting the value of type 1 in f's pointer , instead i am getting size of that particuler struct.

Upvotes: 0

Views: 286

Answers (5)

Roy Samuel
Roy Samuel

Reputation: 790

You are trying to make a pointer to foo (*f) point to another structure 'bar'. foo will not be able to point to this type. Hence you would not be able to cast one structure to the other.

A pointer can point to only data that is used, for the declaration of the pointer.

Try this :

typedef struct {
  int Type;
  int Type2;
}foo;

typedef struct {
  char s[8];
}bar;

int main()
{
  bar b1;
  strcpy(b1.s,"0x01020304");
  foo *f=(struct foo *)&b1;
  printf("Type is  %d \n",f->Type);
  return 0;
}

Upvotes: 0

Secure
Secure

Reputation: 4378

Your cast foo *f=(struct foo *)&b1; is wrong, because you have a typedef foo, not a struct foo. Use foo *f=(foo *)&b1; instead. I.e. don't use it at all, because it violates the strict aliasing rule and is undefined behaviour.

You write the data as a string and read it as int, that's the next undefined behaviour.

The strcpy only writes 3 bytes to the struct (two characters and the terminating \0). Assuming 32 bit ints, the fourth byte of the int remains indeterminate. Reading indeterminate values is again undefined behaviour.

Whatever you get as output comes with no surprise, because everything is allowed to happen on undefined behaviour.

Upvotes: 0

pejotr
pejotr

Reputation: 120

After executing your code struct bar looks like this

| 0x31 | 0x31 | 38 * Rubish.... |

casting it to foo makes interpreting this as int. When you print it in hex you will see output similar to this:

printf("Type is  %x \n",f->Type); 
Type is  b7003131 

Upvotes: 0

The Archetypal Paul
The Archetypal Paul

Reputation: 41759

When I run the code (after correcting for errors), it prints 12593. Which is 49*256 + 49 - in other words, "11" as an integer (ascii 1 being 49). So nothing wrong with the code as far as I can see (apart from the memory layout assumptions pointed out by Benoit), so we do need to know what you expected to happen

#include <stdio.h>
#include<ctype.h>

typedef struct {
  int Type;
  int Type2;
}foo;

typedef struct {
  char cData[40];
}bar;

int main()
{
  bar b1;
  foo *f=(foo *)&b1;
  strcpy(b1.cData,"11");
  printf("Type is  %d \n",f->Type);
  return 0;
}

Upvotes: 4

AndersK
AndersK

Reputation: 36082

after the cast

foo* f = (foo*)&b1

you are interpreting the string "11" (which ASCII-wise is represented as 0x31 0x31 binary) and not as the value 11

     +-------------+
f -> | 0x31 | 0x31 |
     +-------------+

not as

     +-------------+
f -> | 0x01 | 0x01 |
     +-------------+

if you wanted to see 11 in type after the cast you would have to do something like

strcpy(b1.cData,"\x001\x001");

Upvotes: 1

Related Questions