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

void *

what is wrong in following code
int a=4;
void *p;
p = &a;
printf("%s",(char *)p);
above program doesn't print any output?

Jul 22 '05 #1
6 1601

"gyan" <si*****@anz.com> schrieb im Newsbeitrag
news:c3******************************@localhost.ta lkaboutprogramming.com...
what is wrong in following code
int a=4;
void *p;
p = &a;
printf("%s",(char *)p);
above program doesn't print any output?


Yes, a = hex "04 00 00 00" (big endian)
If you print if, the first 'char' has the value 4 - which cannot be
seen, the next has 0, which means end of string.
If you do:
int a=-1; printf("%s", &a); you will get a terrible error, since
you're accessing all memory after 'a' until one byte is zero.

Jul 22 '05 #2
if you just want to print the address of p, do this:
int a=4;
void *p;
p = (void*)&a;
printf("%X",p);
or the value 4 in hex (in 32bit system):
int a=4;
void *p;
p = (void*)&a;
printf("%X",*((long*)p));

Jul 22 '05 #3

"nonamehkg" <no*******@hotmail.com> schrieb im Newsbeitrag
news:11**********************@c13g2000cwb.googlegr oups.com...
if you just want to print the address of p, do this:
int a=4;
void *p;
p = (void*)&a;
printf("%X",p);
or the value 4 in hex (in 32bit system):
int a=4;
void *p;
p = (void*)&a;
printf("%X",*((long*)p));


Address of is done by:
printf("%p", &somthething);
-Gernot
Jul 22 '05 #4
nonamehkg wrote:
if you just want to print the address of p, do this:
int a=4;
void *p;
p = (void*)&a;
printf("%X",p);
bzzt, UNDEFINED BEHAVIOR. %X expects a unsigned int argument
you gave it a pointer.

You must always give printf the right type argument. stdarg is
rather stupid that way.

"%p" by the way will print a pointer value when given a void*.
or the value 4 in hex (in 32bit system):
int a=4;
void *p;
p = (void*)&a;
printf("%X",*((long*)p));


Assuming int and long are the same effective type.
Jul 22 '05 #5
On Wed, 19 Jan 2005 09:15:42 +0100, Gernot Frisch wrote:


Yes, a = hex "04 00 00 00" (big endian)


Actually, that's little endian. :)

And big endian would terminate even sooner (since the first byte would be
a zero).

- Jay
Jul 22 '05 #6
Gernot Frisch wrote:
"nonamehkg" wrote:
if you just want to print the address of p, do this:
int a=4;
void *p;
p = (void*)&a;
printf("%X",p);


Address of is done by:
printf("%p", &somthething);


printf("%p", (void *)&something);

It's quite possible for (int *) to have different representation
to, or even to be be smaller than, (void *)
(eg. if int has alignment 4, then you do not need to bother to
store the bottom 2 bits which must always be 0).

or the value 4 in hex (in 32bit system):
int a=4;
void *p;
p = (void*)&a;
printf("%X",*((long*)p));


That's bizarre, I wonder how he thinks that was better than:
printf("%X", a);

(or, to be pedantic, (unsigned)a )

Jul 22 '05 #7

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...
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: ...
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...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
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...
18
by: Giannis Papadopoulos | last post by:
According to the standard (ISO C99 draft WG14/N1124), void is the incomplete type that cannot be completed and comprises the empty set of values. Since it declares the absense of a value can it be...
4
by: brianhray | last post by:
Hello: I am writing a C interface and was curious how/why a void* can be used as a reference parameter. //////////////////////////////////////////////////////////// // WORKS: // Client1.h...
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: 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:
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
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...
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
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
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.