473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

void * in ANSC



void printhex(const void *data,size_t len)
{
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
printf("%.2X ",*tmp++);
printf("\n");
}
Feb 3 '08 #1
19 1552
On Feb 3, 5:13 am, "Mark Jiao" <jzg1...@126.comwrote:
void printhex(const void *data,size_t len)
{
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
You are performing arithmetic operations with a void * pointer which
is not allowed.
&data[len] means &*(data + len)
Try this
--
while(tmp < (char *)data + len)
Feb 3 '08 #2
On 2ÔÂ3ÈÕ, ÉÏÎç11ʱ13·Ö, "Mark Jiao" <jzg1...@126.comwrote:
void printhex(const void *data,size_t len)
{
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
printf("%.2X ",*tmp++);
printf("\n");

}- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
??
Feb 3 '08 #3
ÔÚ Sun, 03 Feb 2008 11:22:09 +0800£¬<vi******@gmail.comдµÀ:
On Feb 3, 5:13 am, "Mark Jiao" <jzg1...@126.comwrote:
>void printhex(const void *data,size_t len)
{
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
You are performing arithmetic operations with a void * pointer which
is not allowed.
&data[len] means &*(data + len)
Sorry!
I am very poor English!
How many the memery address would be add?? 1? or No Standards ??

Thanks!
Try this
--
while(tmp < (char *)data + len)


--
Feb 3 '08 #4
"Mark Jiao" <jz*****@126.comwrites:
void printhex(const void *data,size_t len)
{
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
printf("%.2X ",*tmp++);
printf("\n");
}
I think what you're asking is why the compiler prints just a warning
rather than an error message.

Attempting to dereference a void*, as you've done here, is a
constraint violation. The compiler is required to issue some sort of
diagnostic message, but it doesn't have to be a fatal error (the
standard doesn't distinguish between warnings and other diagnostics).

As it happens, at least one major compiler allows, as an extension,
certain operations on void* that are not supported by the language.
If you're using gcc, that explains it.

Most warnings should be treated as errors anyway.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 3 '08 #5
Mark Jiao wrote:
>
ÔÚ Sun, 03 Feb 2008 11:22:09 +0800£¬<vi******@gmail.comдµÀ:
On Feb 3, 5:13 am, "Mark Jiao" <jzg1...@126.comwrote:
void printhex(const void *data,size_t len)
{
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
You are performing arithmetic operations with a void * pointer which
is not allowed.
&data[len] means &*(data + len)
Sorry!
I am very poor English!
How many the memery address would be add?? 1? or No Standards ??
No Standards for ((void *)data + 1).
Try this
--
while(tmp < (char *)data + len)
1 memery address would be added for ((char *)data + 1).

--
pete
Feb 3 '08 #6
On Feb 3, 9:32 am, Keith Thompson <ks...@mib.orgwrote:
"Mark Jiao" <jzg1...@126.comwrites:
void printhex(const void *data,size_t len)
{
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
printf("%.2X ",*tmp++);
printf("\n");
}

I think what you're asking is why the compiler prints just a warning
rather than an error message.

Attempting to dereference a void*, as you've done here, is a
constraint violation. The compiler is required to issue some sort of
diagnostic message, but it doesn't have to be a fatal error (the
standard doesn't distinguish between warnings and other diagnostics).
I believe, since x[y] expands to (or, is equal to) *(x + y) what the
compiler should diagnose is about arithmetic on a 'void *' pointer.
Feb 3 '08 #7
On Sun, 03 Feb 2008 03:31:07 -0800, vippstar wrote:
On Feb 3, 9:32 am, Keith Thompson <ks...@mib.orgwrote:
>"Mark Jiao" <jzg1...@126.comwrites:
void printhex(const void *data,size_t len) {
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
printf("%.2X ",*tmp++);
printf("\n");
}

I think what you're asking is why the compiler prints just a warning
rather than an error message.

Attempting to dereference a void*, as you've done here, is a constraint
violation. The compiler is required to issue some sort of diagnostic
message, but it doesn't have to be a fatal error (the standard doesn't
distinguish between warnings and other diagnostics).
I believe, since x[y] expands to (or, is equal to) *(x + y) what the
compiler should diagnose is about arithmetic on a 'void *' pointer.
You're correct. Dereferencing a void * is allowed, though not
particularly useful, since you can't do anything with the result but
discard it or take its address again. Performing arithmetic on a void *
is not allowed, because void is an incomplete type. It's the same as how
performing arithmetic on int (*)[] is not allowed, because int [] is an
incomplete type.
Feb 3 '08 #8
On Sun, 03 Feb 2008 12:47:45 +0100, Harald van D?k <tr*****@gmail.com>
wrote in comp.lang.c:
On Sun, 03 Feb 2008 03:31:07 -0800, vippstar wrote:
On Feb 3, 9:32 am, Keith Thompson <ks...@mib.orgwrote:
"Mark Jiao" <jzg1...@126.comwrites:
void printhex(const void *data,size_t len) {
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
printf("%.2X ",*tmp++);
printf("\n");
}

I think what you're asking is why the compiler prints just a warning
rather than an error message.

Attempting to dereference a void*, as you've done here, is a constraint
violation. The compiler is required to issue some sort of diagnostic
message, but it doesn't have to be a fatal error (the standard doesn't
distinguish between warnings and other diagnostics).
I believe, since x[y] expands to (or, is equal to) *(x + y) what the
compiler should diagnose is about arithmetic on a 'void *' pointer.

You're correct. Dereferencing a void * is allowed, though not
Allowed by whom? Certainly not by the C language standard. Can you
cite C&V to the contrary?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Feb 3 '08 #9
Jack Klein <ja*******@spamcop.netwrites:
On Sun, 03 Feb 2008 12:47:45 +0100, Harald van D?k <tr*****@gmail.com>
wrote in comp.lang.c:
[...]
>You're correct. Dereferencing a void * is allowed, though not

Allowed by whom? Certainly not by the C language standard. Can you
cite C&V to the contrary?
Can you cite C&V that says it's *not* allowed? I checked earlier, and
I was surprised to discover that it does appear to be allowed.

In C99 6.5.3.2, the only constraint for the unary "*" operator is:

The operand of the unary * operator shall have pointer type.

and void* is a pointer type. Obviously there's very little you can do
with the result, but this:

void *ptr = /* some non-null value */
*ptr;

appears to be legal (though not particularly useful).

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 3 '08 #10
Keith Thompson wrote:
In C99 6.5.3.2, the only constraint for the unary "*" operator is:

The operand of the unary * operator shall have pointer type.

and void* is a pointer type. Obviously there's very little you can do
with the result, but this:

void *ptr = /* some non-null value */
*ptr;

appears to be legal (though not particularly useful).
This has a curious consequence:
volatile void *ptr = SOMEWHERE;
*ptr;

How many bytes does that access?

--
Army1987 (Replace "NOSPAM" with "email")
Feb 4 '08 #11
Army1987 <army1...@NOSPAM.itwrote:
Keith Thompson wrote:
In C99 6.5.3.2, the only constraint for the unary "*"
operator is:

* * The operand of the unary * operator shall have pointer
type. and void* is a pointer type. *Obviously there's very
little you can do with the result, but this:

* * void *ptr = /* some non-null value */
* * *ptr;

appears to be legal (though not particularly useful).

This has a curious consequence:
* * *volatile void *ptr = SOMEWHERE;
* * **ptr;

How many bytes does that access?
None. [How many bytes does any other void expression access?]

--
Peter
Feb 4 '08 #12
Peter Nilsson wrote:
Army1987 <army1...@NOSPAM.itwrote:
>Â* Â* Â*volatile void *ptr = SOMEWHERE;
Â* Â* Â**ptr;

How many bytes does that access?

None. [How many bytes does any other void expression access?]
(void)memcpy(foo, bar, 42) accesses 42 bytes.

--
Army1987 (Replace "NOSPAM" with "email")
Feb 4 '08 #13
Army1987 wrote:
(void)memcpy(foo, bar, 42) accesses 42 bytes.
84 bytes, I meant.

--
Army1987 (Replace "NOSPAM" with "email")
Feb 4 '08 #14
Army1987 <army1...@NOSPAM.itwrote:
Peter Nilsson wrote:
Army1987 <army1...@NOSPAM.itwrote:
* * *volatile void *ptr = SOMEWHERE;
* * **ptr;
>
How many bytes does that access?
None. [How many bytes does any other void expression
access?]

(void)memcpy(foo, bar, 42) accesses [84] bytes.
How many bytes does the following access?

memcpy;

--
Peter
Feb 4 '08 #15
In article <fo**********@tdi.cu.mi.it>, Army1987 <ar******@NOSPAM.itwrote:
>Army1987 wrote:
>(void)memcpy(foo, bar, 42) accesses 42 bytes.
>84 bytes, I meant.
How do you arrive at that number? memcpy() does not define
the behaviour if the fields overlap. memmove() is the function
that defines the copying "as if" the data were copied into a
temporary buffer, and if such temporary copies were to take
place then Yes that would increase the byte count, but as you
have not defined the extent to which foo overlaps with bar,
you cannot say with any degree of certainty -exactly- how many
bytes would be accessed or how many times those bytes would be
accessed.
--
"Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson
Feb 4 '08 #16
Army1987 <army1...@NOSPAM.itwrote:
Peter Nilsson wrote:
Army1987 <army1...@NOSPAM.itwrote:
* * *volatile void *ptr = SOMEWHERE;
* * **ptr;

How many bytes does that access?
>
None. [How many bytes does any other void expression
access?]
(void)memcpy(foo, bar, 42) accesses [84] bytes.
<snip>

[In light of Harald van Dijk's point on my followup...]
How many bytes does the following access?

memcpy(foo, bar, 0);

If it's none, then why should *memcpy(foo, bar, 0) be
any different? If it isn't different, then why should
dereferencing a volatile void pointer be any different?

--
Peter
Feb 4 '08 #17
Walter Roberson wrote:
>Army1987 wrote:
>>(void)memcpy(foo, bar, 42) accesses 42 bytes.
>84 bytes, I meant.

How do you arrive at that number? memcpy() does not define
the behaviour if the fields overlap. memmove() is the function
.... snip ...

There is a from and a to parameter. They have restrict applied, so
they are different. Each points to a 42 byte area, which is either
examined or modified. 42 + 42 is roughly equal to 84. In base 10.

7.21.2.1 The memcpy function

Synopsis
[#1]
#include <string.h>
void *memcpy(void * restrict s1,
const void * restrict s2,
size_t n);
Description
[#2] The memcpy function copies n characters from the object
pointed to by s2 into the object pointed to by s1. If
copying takes place between objects that overlap, the
behavior is undefined.

Returns
[#3] The memcpy function returns the value of s1.

Please don't strip attributions for any material you quote.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 5 '08 #18
On Mon, 04 Feb 2008 18:46:13 -0500, CBFalconer wrote:
Walter Roberson wrote:
[stripped attribution re-inserted]
>>In article <fo**********@tdi.cu.mi.it>, Army1987 <ar******@NOSPAM.it>
wrote:
>>Army1987 wrote:
>>>(void)memcpy(foo, bar, 42) accesses 42 bytes.
>>84 bytes, I meant.

How do you arrive at that number? memcpy() does not define the
behaviour if the fields overlap. memmove() is the function

Please don't strip attributions for any material you quote.
Please don't strip attributions for any material you quote. You stripped
attributions. Walter Roberson didn't, or at least not in the message you
replied to.
Feb 5 '08 #19
Peter Nilsson wrote:
How many bytes does the following access?

memcpy(foo, bar, 0);

If it's none, then why should *memcpy(foo, bar, 0) be
any different? If it isn't different, then why should
dereferencing a volatile void pointer be any different?
Ok, another example:

volatile struct incomplete *foo = a_function_returning_a_struct_pointer();
*foo;
How many bytes does the statement access?
--
Army1987 (Replace "NOSPAM" with "email")
Feb 5 '08 #20

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

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
6
by: dam_fool_2003 | last post by:
Hai, I thank those who helped me to create a single linked list with int type. Now I wanted to try out for a void* type. Below is the code: #include<stdlib.h> #include<stdio.h>...
15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
3
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
7
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.