Querry on pointers

A

abhaybhat

Hello all,

char a[2]
char* u8
int u32

u8 = &u32

Cant we referance a char pointer to int ?
Why is this not possible?

I want to transfer all the data from array a to u32.


Thanks
 
C

Chris Dollin

abhaybhat said:
Hello all,

char a[2]
char* u8
int u32

u8 = &u32

You want to assign an int* to a char*? That sounds
like a recipe for confusion.
Cant we referance a char pointer to int ?
Why is this not possible?

It's /possible/. It's likely not /wise/, depending.
I want to transfer all the data from array a to u32.

u32 = (a[1] << 8) + a[0];

(where `8` should likely be spelled `CHAR_BIT` and `1`
and `0` may need interchanging depending on what
endianness you've picked for the bytes.)

No pointer-type-games needed.
 
S

santosh

abhaybhat said:
Hello all,

char a[2]
char* u8
int u32

u8 = &u32

Cant we referance a char pointer to int ?

Not portably. A pointer of type T* may hold either the value NULL or the
address of an object of type T. The special pointer void* may hold NULL
or the address of any object type, but may not be deferenced. Nor can
you do pointer arithmetic on void*.
Why is this not possible?

That's how the language has been designed. The main reason is
implementability on systems where different pointer types may be
incompatible with each other, i.e., their size may be different, their
representation may be different, etc.

<snip>
 
V

vippstar

abhaybhat said:
Hello all,
char a[2]
char* u8
int u32
u8 = &u32

You want to assign an int* to a char*? That sounds
like a recipe for confusion.
Cant we referance a char pointer to int ?
Why is this not possible?

It's /possible/. It's likely not /wise/, depending.
I want to transfer all the data from array a to u32.

u32 = (a[1] << 8) + a[0];

(where `8` should likely be spelled `CHAR_BIT` and `1`
and `0` may need interchanging depending on what
endianness you've picked for the bytes.)
AFAIK that won't work if int's size is char's size as well. (*if* 8 is
replaced with CHAR_BIT)
 
B

Barry Schwarz

Hello all,

char a[2]
char* u8
int u32

u8 = &u32

Cant we referance a char pointer to int ?

Not implicitly.
Why is this not possible?

It is possible; use a cast.
I want to transfer all the data from array a to u32.

Use memcpy. But why? Is there any guarantee that the result will be
in any way meaningful? Based on you names, it looks like a will
occupy only half of u32. What will you do with the other half?

You could put a and u32 in a union and not need to transfer anything.


Remove del for email
 
N

Nick Keighley

char a[2]
char* u8
int u32

I'm never too fond of things like u8 and u32,
but if you must use them then most people would expect
the "u" to stand for unsigned. char may or may not be unsigned
int is never unsigned.

If u8 is 8-bits and u32 is 32-bits then a u32 will
consist of 4 u8s not 2 as you imply.

u8 = &u32

Cant we referance a char pointer to int ?
Why is this not possible?

I want to transfer all the data from array a to u32.

I bet you don't.
 
C

Chris Dollin

abhaybhat said:
Hello all,
char a[2]
char* u8
int u32
u8 = &u32

You want to assign an int* to a char*? That sounds
like a recipe for confusion.
Cant we referance a char pointer to int ?
Why is this not possible?

It's /possible/. It's likely not /wise/, depending.
I want to transfer all the data from array a to u32.

u32 = (a[1] << 8) + a[0];

(where `8` should likely be spelled `CHAR_BIT` and `1`
and `0` may need interchanging depending on what
endianness you've picked for the bytes.)
AFAIK that won't work if int's size is char's size as well. (*if* 8 is
replaced with CHAR_BIT)

If `sizeof(int)` equals `sizeof(char)`, what the OP wants to do
cannot be done at all (and their chosen names would be misleading).
Quarts & pint pots.
 
V

vippstar

abhaybhat wrote:
Hello all,
char a[2]
char* u8
int u32
u8 = &u32
You want to assign an int* to a char*? That sounds
like a recipe for confusion.
Cant we referance a char pointer to int ?
Why is this not possible?
It's /possible/. It's likely not /wise/, depending.
I want to transfer all the data from array a to u32.
u32 = (a[1] << 8) + a[0];
(where `8` should likely be spelled `CHAR_BIT` and `1`
and `0` may need interchanging depending on what
endianness you've picked for the bytes.)
AFAIK that won't work if int's size is char's size as well. (*if* 8 is
replaced with CHAR_BIT)

If `sizeof(int)` equals `sizeof(char)`, what the OP wants to do
cannot be done at all (and their chosen names would be misleading).
Quarts & pint pots.
Their names are misleading nonetheless.
Using <stdint.h> and uintN_t it can be done:

uint8_t arrayu8[2];
/*...*/
uint32_t u32 = arrayu8[1] << 8 | arrrayu8[0];

It is not however portable to use << CHAR_BIT or >> CHAR_BIT with any
integer expression.
 
C

Chris Dollin

abhaybhat wrote:
Hello all,
char a[2]
char* u8
int u32
u8 = &u32
You want to assign an int* to a char*? That sounds
like a recipe for confusion.
Cant we referance a char pointer to int ?
Why is this not possible?
It's /possible/. It's likely not /wise/, depending.
I want to transfer all the data from array a to u32.
u32 = (a[1] << 8) + a[0];
(where `8` should likely be spelled `CHAR_BIT` and `1`
and `0` may need interchanging depending on what
endianness you've picked for the bytes.)
AFAIK that won't work if int's size is char's size as well. (*if* 8 is
replaced with CHAR_BIT)

If `sizeof(int)` equals `sizeof(char)`, what the OP wants to do
cannot be done at all (and their chosen names would be misleading).
Quarts & pint pots.
Their names are misleading nonetheless.
Using <stdint.h> and uintN_t it can be done:

uint8_t arrayu8[2];
/*...*/
uint32_t u32 = arrayu8[1] << 8 | arrrayu8[0];

It is not however portable to use << CHAR_BIT or >> CHAR_BIT with any
integer expression.

When those expressions are not defined, the OP can't do what they
want anyway. When they can do what they want (or at least my
reading of it), the shifts are well-defined.

I agree that the code must be guarded somehow against undefinedness.
 
K

Keith Thompson

santosh said:
abhaybhat said:
Hello all,

char a[2]
char* u8
int u32

u8 = &u32

Cant we referance a char pointer to int ?

Not portably. A pointer of type T* may hold either the value NULL or the
address of an object of type T. The special pointer void* may hold NULL
or the address of any object type, but may not be deferenced. Nor can
you do pointer arithmetic on void*.
Why is this not possible?

That's how the language has been designed. The main reason is
implementability on systems where different pointer types may be
incompatible with each other, i.e., their size may be different, their
representation may be different, etc.

I think the main reason is simple type safety, so programmers don't go
around assigning float values to int objects and so forth. (You can
still do such things, but it's a bit more complicated, and the
gyrations you have to go through make it a bit clearer that it's
non-portable.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top