473,484 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Querry on pointers

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
Jun 27 '08 #1
11 1365
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.)

No pointer-type-games needed.

--
"Where the shadows run from themselves." /White Room/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 27 '08 #2
abhaybhat wrote:
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>

Jun 27 '08 #3
On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@hp.comwrote:
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)
Jun 27 '08 #4
On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat
<ab*****@gmail.comwrote:
>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
Jun 27 '08 #5
On Jun 12, 4:07 pm, Barry Schwarz <schwa...@dqel.comwrote:
On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat

<abha...@gmail.comwrote:
Hello all,
char a[2]
char* u8
int u32
u8 = &u32
Cant we referance a char pointer to int ?

Not implicitly.
char *p = 0;
Jun 27 '08 #6
On 12 Jun, 13:09, abhaybhat <abha...@gmail.comwrote:
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.
--
Nick Keighley

why isn't there an obfuscated C++ contest?

Jun 27 '08 #7
vi******@gmail.com wrote:
On Jun 12, 4:07 pm, Barry Schwarz <schwa...@dqel.comwrote:
>On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat

<abha...@gmail.comwrote:
>Hello all,
>char a[2]
char* u8
int u32
>u8 = &u32
>Cant we referance a char pointer to int ?

Not implicitly.
char *p = 0;
That's not an example of "referance a char pointer to int"
(by which the OP seems to mean "assign an int pointer value
to a char pointer variable").

--
"The letter was not unproductive." /Mansfield Park/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 27 '08 #8
vi******@gmail.com wrote:
On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@hp.comwrote:
>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.

--
"Giving my opinion / to whoever's there." /Wonderland/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #9
On Jun 12, 4:27 pm, Chris Dollin <chris.dol...@hp.comwrote:
vipps...@gmail.com wrote:
On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@hp.comwrote:
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.hand 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.
Jun 27 '08 #10
vi******@gmail.com wrote:
On Jun 12, 4:27 pm, Chris Dollin <chris.dol...@hp.comwrote:
>vipps...@gmail.com wrote:
On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@hp.comwrote:
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.hand 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.

--
"I am trying to say that you have no choice." /The Courts of Chaos/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 27 '08 #11
santosh <sa*********@gmail.comwrites:
abhaybhat wrote:
>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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3181
by: Eric Kincl | last post by:
Hello, I have an array of data in PHP. I would like to insert each member of the array into it's own row in SQL. The array is of variable length, so it would have to be dynamic code. How would...
0
1585
by: Costa Lino | last post by:
Hi All, I have a DataSet with xml file and I want to make a querry like this DataView dv = new DataView(mytable); dv.RowFilter = " Impression < ( MaxImpressions) "; Impression et...
5
1560
by: ranjeet.gupta | last post by:
Dear All As I was going through the Recent replies on the realloc(), I got some question and my annalysis on that, so regarding on these please guide me where I fail on the theoritical and...
7
1611
by: Lalatendu Das | last post by:
hi Please someone give a real time implimentation of a function pointer. where exactly we need a function pointer very much . if it is in terms of code i will be happy enough . thanks in advance
5
1834
by: Clownfish | last post by:
OK, I'm having a brain freeze. I have a table like this: Office Name Phone ---------------------------------- SG Larry 555-1212 SG Moe 553-4444 SG Curly ...
1
1734
Steve Kiss
by: Steve Kiss | last post by:
Hi. I am developping a site for which one of the pages uses querry strings to pass some parameters. I can use the querry strings if I call the page from a plain html anchor. However, when I add the...
1
1480
by: nj2md | last post by:
Can some one assist with a querry. I need to know the code to querry a database to find the number of female and males that make over 50K a year and how to get capital gains and loses from the same...
0
1250
by: getmeidea | last post by:
I have the following tables, 1> employee_master(emp_id int primary key, emp_name varchar(100)); 2> employee_salary_payment(salary_rid int primary key, emp_id int, sal_date date, paid_amt int); ...
2
1439
by: dipalichavan82 | last post by:
i came across a article, where it was mentioned if we want a dynamic querry to fire then use parameterized querry e.g. string inputcity=textbox.text; SqlCommand cmd = new SqlCommand("select * from...
19
2010
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
6950
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7103
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7140
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4841
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3038
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
588
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.