473,395 Members | 1,670 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,395 software developers and data experts.

Void Pointer Question

#include <stdio.h>

int multi[2][4];

int main()
{
printf("\nmulti = %p",(void *)multi);
printf("\nmulti[0] = %p",(void *)&multi[0]);
printf("\nmulti[0] = %p",(void *)multi[0]);
printf("\n&multi[0][0] = %p\n",(void *)&multi[0][0]);
return 0;
}

as far as i know.. the above code is alright..

what i'd like to ask is why should (void *) put there .

what's the difference if i change 'pointer to array or pointer to
integer' to 'pointer to void' like above ?

TIA
Nov 13 '05 #1
2 11248
herrcho wrote:
printf("\nmulti = %p",(void *)multi); [snip]
what i'd like to ask is why should (void *) put there . what's the difference if i change 'pointer to array or pointer to
integer' to 'pointer to void' like above ?


Usually, if a function expects void * and you pass it something_else
*, the compiler will just cast to void *. It can't do that for
printf() and other variadic functions, because the prototype says
"..." and it doesn't know what the types should be. So, when using
%p and something_else *, always cast to void *.

--
Tom Zych
This is a fake email address to thwart spammers.
Real address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #2
On Thu, 25 Sep 2003 08:59:46 +0900 (KST), "herrcho"
<he*********@kornet.net> wrote:
#include <stdio.h>

int multi[2][4];

int main()
{
printf("\nmulti = %p",(void *)multi);
printf("\nmulti[0] = %p",(void *)&multi[0]);
printf("\nmulti[0] = %p",(void *)multi[0]);
printf("\n&multi[0][0] = %p\n",(void *)&multi[0][0]);
return 0;
}

as far as i know.. the above code is alright..

what i'd like to ask is why should (void *) put there .

what's the difference if i change 'pointer to array or pointer to
integer' to 'pointer to void' like above ?

For variadic functions, variable arguments cannot be coerced to the
"correct" type since that type is unknown. (There is an exception for
some integer and double promotions which don't apply here.)

%p tells printf that the corresponding argument will have type pointer
to void so it is you job to make sure that it does.

On the systems I'm familiar with, all pointers have the same size and
representation so the cast is essentially a nop. BUT, there is no
requirement for this consistency. It is legal/possible for a void* to
be eight bytes while a pointer to array is four bytes or for one to be
big endian and the other little endian. On such systems, omitting the
cast would cause undefined behavior.
<<Remove the del for email>>
Nov 13 '05 #3

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

Similar topics

52
by: Vladimir Grul | last post by:
Hello, I have a class member function declared as class some_class { .... virtual int call(void); }; Can I use this-> inside the function body?
7
by: Alfonso Morra | last post by:
Straight of the bat, I'll admit this is not a nice solution, it is dangerous (not type safe) etc,etc, I know what the perils are. I don't need a lecture on why what I'm doing is perilous - that is...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
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...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
11
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
4
by: Andreas Klimas | last post by:
hello, no many words, I will start with an example instead int foo(void *r) {...} void test(void) { int *x=0; foo(x); /* seems to me as valid */ }
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
28
by: junky_fellow | last post by:
Guys, Consider a function func(void **var) { /* In function I need to typecast the variable var as (int **) I mean to say, I need to access var as (int **) }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.