user1665569
user1665569

Reputation: 191

Cast to pointer from integer of different size

client_t *client_create_no_window(char *in, char *out); 

// ...

char line[50];
while((c = getchar()) != EOF && c != '\n')
{
    line[pos] = c;
    pos++;
}
pos=0;

then I try to call the function client_create_no_window by first doing

char* first = (char*) line[1];
char* second= (char*) line[2];
client_create_no_window(first, second);

I keep getting the error casting to pointer from integer of different size. I've looked at some of the previous posts on these but still cant understand why this is going on

Upvotes: 1

Views: 6776

Answers (4)

user3416126
user3416126

Reputation: 158

Change your code as follows ( assume you are really trying to assign the pointer to the value of line[1] and line[2]). If you want to point the the address of line[1] and line[2] you would do the &line[] as describe above and you will not have to use reinterpret_cast to stop the warning messages about size:

line[1] = 2;
line[2] = 0x234234;

char* first = reinterpret_cast<char *>(line[1]);
char* second= reinterpret_cast<char *>(line[2]);
client_create_no_window(first, second);

The first will be pointing to 0x000002 The second will be pointing to 0x234234

The reinterpret_cast take the value and casts it to the type and size of pointer given. In the older compilers just doing the char *ptr = (char *)23 was good enough but the new compilers know a int (23 would be stored in a int) is not the size of a pointer (normally 64 bits) and it generates the warning message. reinterpret_cast fixes that.

Upvotes: 0

LihO
LihO

Reputation: 42085

char line[50];

defines an array of characters. Second character (line[1]) is stored in the memory at address that can be retrieved by using the "address of" operator (&): &line[1].

char* second = (char*) line [1];

takes the second character (line[1]) and casts it to the pointer to char, which is incorrect. Pointer to the second character should be initialized by using the address, where this character resides:

char* second = &line[1];

Upvotes: 2

Austin Mullins
Austin Mullins

Reputation: 7427

First, I'm sure it's just a typo, but you declared char lines[] instead of char line[]. Second, did you want to put the address of line[0] into first? That would be:

char *first = line;

Or

char *first = &line[0];

Similarly:

char *second = &line[1];

Notice the first and second characters are at indexes 0 and 1, respectively.

Upvotes: 1

unwind
unwind

Reputation: 399703

This:

char*first = (char*) lines[1];

takes the character at lines[1], converts it through a forced cast to char * (pointer to character, i.e. an address), and stores it in the character pointer first.

This is wrong, since characters are not addresses this assignment makes no sense.

What you want is probably the address of that character, which you can get by using the "address of" operator, &:

char *first = &lines[1];

which can also be written using pointer arithmetic as:

char *first = lines + 1;

Note, however, that C indexes from zero, so the first character is really at lines[0]:

char *first = &lines[0];

and the second is at lines[1]:

char *second = &lines[1];

Upvotes: 2

Related Questions