473,750 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is typecasting a pointer to the type (void *)p mean?

why do I see that in most C programs, pointers in functions are
accepted as:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing
to a void and why is it done?
Aso, what happens when we typecast a pointer to another type. say for
example int *i=(char *)p;
under different situations? I am kind of confused..can anybody clear
this confusion by clearly explaining to me what actually is happening
out here?
Bye

Jan 25 '06 #1
16 10392
On Wed, 25 Jan 2006 12:17:23 -0800, Abhishek wrote:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing to
a void and why is it done? A pointer to a void is a pointer to an unqualified place in memory. If you
want to work with threads this is unavoidable. One of the reasons is a
way of transfering data from one process to another. You cannot do this
and at the same time preserve the type information. This also means that
the receiving process must know how to dereference the pointer - cast it
to a known type.

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void test_p(void *t)
{
int i;
char *text = (char *) t;
for (i = 0; i < 10; ++i)
printf("%s\n", text);
pthread_exit(NU LL);
}

int main()
{
pthread_t t1 = 0;
char *text = "test";

printf("Howdy world!\n");
pthread_create( &t1, NULL, (void *)&test_p, (void *) text);
pthread_join(t1 , NULL);
return 0;
}
Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p;

This is dangerous since you could loose information an depending on your
compiler could course either a warning or an error.
--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917

Jan 25 '06 #2

Abhishek wrote:
why do I see that in most C programs, pointers in functions are
accepted as:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing
to a void and why is it done?
void pointer is a generic pointer, meaning that it can be used to store
address of any data type.

by (void*)p you are casting the address of p to void* type.
This is actually redundant. Typecasting is not necessary
while assigning value to a void* pointer.
Aso, what happens when we typecast a pointer to another type. say for
example int *i=(char *)p;
This should not even compile because you're trying to assign a wrong
type
of address to int *i.
under different situations? I am kind of confused..can anybody clear
this confusion by clearly explaining to me what actually is happening
out here?
Bye


Sharath A.V

Jan 25 '06 #3
Does this alsomean that I get the following flexibility?

like I can use the test_p function to accept different types of
pointers by typecasting them to type void * and later perform an action
depending on a particular flag variable I pass to the function?
like void test_p(void *t, int i)
{
int k;
if(i==1)
{
char *text = (char *) t;
for (k = 0; k < 10; ++k)
printf("%s\n", text);
}
else
{
int *j=(int *)t;
for(k=0;k<10;k+ +)
printf("%d\n",* j);
}

}

here...dependin g upon the value of 'i' I pass, I can take appropriate
action. However, I know that if I pass i=1, then I pass a pointer to a
string by typecasting to void * and if I pass i !=2 then I pass a
pointer to an integer by type casting the integer pointer to type void
*???
Pls clarify

Michael Rasmussen wrote:
On Wed, 25 Jan 2006 12:17:23 -0800, Abhishek wrote:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing to
a void and why is it done?

A pointer to a void is a pointer to an unqualified place in memory. If you
want to work with threads this is unavoidable. One of the reasons is a
way of transfering data from one process to another. You cannot do this
and at the same time preserve the type information. This also means that
the receiving process must know how to dereference the pointer - cast it
to a known type.

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void test_p(void *t)
{
int i;
char *text = (char *) t;
for (i = 0; i < 10; ++i)
printf("%s\n", text);
pthread_exit(NU LL);
}

int main()
{
pthread_t t1 = 0;
char *text = "test";

printf("Howdy world!\n");
pthread_create( &t1, NULL, (void *)&test_p, (void *) text);
pthread_join(t1 , NULL);
return 0;
}
Aso, what happens when we typecast a pointer to another type. say

for
example int *i=(char *)p;

This is dangerous since you could loose information an depending on your
compiler could course either a warning or an error.
--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917


Jan 25 '06 #4
Abhishek a écrit :
Does this alsomean that I get the following flexibility?
'flexibility' and 'void*' are often associated. Your intuition is correct.
like I can use the test_p function to accept different types of
pointers by typecasting them to type void * and later perform an action
depending on a particular flag variable I pass to the function?
Somehow, yes. Note that the conversion from/to void* doesn't need an
explicit typecast.
like void test_p(void *t, int i)
{
int k;
if(i==1)
{
char *text = (char *) t;
char *text = t;
if (text != NULL)
{ for (k = 0; k < 10; ++k)
printf("%s\n", text); } }
else
{
int *j=(int *)t;
int *j = t;
if (j != NULL)
{
for(k=0;k<10;k+ +)
printf("%d\n",* j); } }

}

here...dependin g upon the value of 'i' I pass, I can take appropriate
action.


Correct.

--
A+

Emmanuel Delahaye
Jan 25 '06 #5
On Wed, 25 Jan 2006 13:01:56 -0800, Abhishek wrote:
Does this alsomean that I get the following flexibility?

like I can use the test_p function to accept different types of pointers
by typecasting them to type void * and later perform an action depending
on a particular flag variable I pass to the function? like void
test_p(void *t, int i)

For this kind of problem I would recommend using a struct.

#include <stdio.h>

enum Kind {integer, string};

struct container {
enum Kind kind;
void *ptr;
};

void test_p(void *data)
{
struct container *c = (struct container *) data;
switch (c->kind) {
case integer:
printf("Receive d integer:\t%d\n" , (int) c->ptr);
break;
case string:
printf("Receive d string:\t%s\n", (char *) c->ptr);
break;
default:
printf("Undefin ed data type\n");
}
}

int main()
{
struct container c;
char *text = "test";
int i = 1;

c.kind = string;
c.ptr = (void *)text;
test_p((void *) &c);
c.kind = integer;
c.ptr = (void *)i;
test_p((void *) &c);
return 0;
}

--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917

Jan 25 '06 #6
On Wed, 25 Jan 2006 22:56:57 +0100, Emmanuel Delahaye wrote:

Somehow, yes. Note that the conversion from/to void* doesn't need an
explicit typecast.

Well, not always but in this situation it is mandatory:
void foo(void *t) {
printf("%s", (char *) t);
}

int main() {
char *t = "test";
foo(t);
return 0;
}

So might as well use explicit casting since it is a lot more readable for
an outsider which later has to maintain the code. IMHO

--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917

Jan 25 '06 #7
On Wed, 25 Jan 2006 23:07:54 +0100, Michael Rasmussen <mi*@miras.or g>
wrote:
On Wed, 25 Jan 2006 22:56:57 +0100, Emmanuel Delahaye wrote:

Somehow, yes. Note that the conversion from/to void* doesn't need an
explicit typecast.

Well, not always but in this situation it is mandatory:
void foo(void *t) {
printf("%s", (char *) t);
}

int main() {
char *t = "test";
foo(t);
return 0;
}

So might as well use explicit casting since it is a lot more readable for
an outsider which later has to maintain the code. IMHO


void foo(void *t) {
char *rt = t;
printf("%s", rt);
}

--
Al Balmer
Sun City, AZ
Jan 25 '06 #8
Thank you all for that wonderful explanation.
So, whats the difference between a null pointer and a pointer which is
pointing to void?
Are these same?

Michael Rasmussen wrote:
On Wed, 25 Jan 2006 22:56:57 +0100, Emmanuel Delahaye wrote:

Somehow, yes. Note that the conversion from/to void* doesn't need an
explicit typecast.

Well, not always but in this situation it is mandatory:
void foo(void *t) {
printf("%s", (char *) t);
}

int main() {
char *t = "test";
foo(t);
return 0;
}

So might as well use explicit casting since it is a lot more readable for
an outsider which later has to maintain the code. IMHO

--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917


Jan 25 '06 #9
On Wed, 25 Jan 2006 14:18:42 -0800, Abhishek wrote:
Thank you all for that wonderful explanation. So, whats the difference
between a null pointer and a pointer which is pointing to void?
Are these same?

A null pointer points to nothing - eg. you have a pointer of some type
which has not jet been assigned any value. A pointer to void is a pointer
of some unknown type assigned an address in memory.

--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917

Jan 25 '06 #10

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

Similar topics

7
4544
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator returns a 'mytype'. In main() I use the operator on 'Test'-class objects. If 'mytype' is a pointer the operator works fine. But if I instead try to call an overloaded operator, the typecast operator is not invoked, and I get a compiler error...
2
4177
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin code*/
1
2834
by: b83503104 | last post by:
When are they not consistent?
11
4421
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple assignment. int b = some_value;
13
2192
by: ranjeet.gupta | last post by:
Dear All What does exactly below code means struct File { void* data; }; typedef struct File File; typedef File* fl;
3
3994
by: bnoordhuis | last post by:
Consider this: int foo(int *a, int *b); int (*bar)(void *, void *) = (void *)foo; How legal - or illegal - is the typecast and are there real-world situations where such code will cause trouble? I don't mean trouble like in 'not compiling' but trouble like in 'crashing hard'.
1
1546
by: ramakanta.sinha | last post by:
Hi, While typecasting an integer to (void *) the following warning is found. warning: cast to pointer from integer of different size. This is the line where typecasting is done. fld->base = (void *)(((fld->f90_table_index-1) * F90_MAX_FLD_SIZE_IN_BYT
5
11245
by: WittyGuy | last post by:
How to typecast a "function pointer" to "const void*" type in C++ way? int MyFunction (double money); // Function prototype const void* arg = (const void*)MyFunction; // type casting function pointer to const void* in C-style void(*pFunc)() = (void(*)())(arg); // type casting const void* to function pointer in C-style (*pFunc)(); // Calling the function after type casting is done
3
13125
by: sritejv | last post by:
Hello everyone, I am having a problem with typecasting void pointers.I have read the pointer basics but still cant understand why the following test code doesnt work. void *xyz; struct abcd { int a; };
0
9577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9339
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.