473,395 Members | 1,422 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.

Passing void pointer to p_thread that is a Char

I am trying to create a p_thread
pthread_create(&threads[k], &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 from integer of different size.

Now I thought that when it was a void I could pass anything? Thing is it
works when I use an int, but in this case I wanted to use a char. It wouldnt
be hard to work around it, but it annoys me because I've heard anything can
be passed as long as its a void.

I have remembered to declare the function:
void *Teste(void *var)

and in the main method that created the thread I have declared the var
variable :
char var;
var = 'A'

and the function itself: void *Teste(void *var)

Now If I were to change the char declaration of var to f.example:
int var;
var = 1;

It works.

Now as I said I get this message:
test.c:58: warning: cast to pointer from integer of different size.

Why? And where does the pointer from integer come from? Theres no integer
involved, Im trying to pass a void thats a char.

If anyone can help me I would highly apriciate it.


Nov 14 '05 #1
9 5266

Juggernaut wrote:
I am trying to create a p_thread
p_threads are a bit OT...
pthread_create(&threads[k], &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 from integer of different size.
<snip>
and in the main method that created the thread I have declared the var variable :
char var;
var = 'A'
Is it really your intention to convert the (probably) 8 bit char value
contained within var into a (probably) 32 bit memory address, and then
to save this address as a void* ?

<snip>
Now If I were to change the char declaration of var to f.example:
int var;
var = 1;

It works.
This works beause the int is (probably) a 32 bit value.
Now as I said I get this message:
test.c:58: warning: cast to pointer from integer of different size.

Why? And where does the pointer from integer come from? Theres no integer involved, Im trying to pass a void thats a char.

If anyone can help me I would highly apriciate it.


What you really want to do is (void *)&var. This will achieve, I
believe, what you are trying to do.

-Jason

Nov 14 '05 #2
>What you really want to do is (void *)&var. This will achieve, I believe,
what you are trying to do.

In the function or the p_thread_create?

The reason for the void thing is because, if I've understood correctly,
p_thread creates demands a void variable.

And I need the passed value to be a char.

">Jason" <ma******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Juggernaut wrote:
I am trying to create a p_thread


p_threads are a bit OT...
pthread_create(&threads[k], &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 from integer of different size.


<snip>
and in the main method that created the thread I have declared the

var
variable :
char var;
var = 'A'


Is it really your intention to convert the (probably) 8 bit char value
contained within var into a (probably) 32 bit memory address, and then
to save this address as a void* ?

<snip>
Now If I were to change the char declaration of var to f.example:
int var;
var = 1;

It works.


This works beause the int is (probably) a 32 bit value.
Now as I said I get this message:
test.c:58: warning: cast to pointer from integer of different size.

Why? And where does the pointer from integer come from? Theres no

integer
involved, Im trying to pass a void thats a char.

If anyone can help me I would highly apriciate it.


What you really want to do is (void *)&var. This will achieve, I
believe, what you are trying to do.

-Jason

Nov 14 '05 #3

Juggernaut wrote:
What you really want to do is (void *)&var. This will achieve, I
believe, what you are trying to do.

In the function or the p_thread_create?
Yep

The reason for the void thing is because, if I've understood correctly, p_thread creates demands a void variable.
void * you mean...

And I need the passed value to be a char.


The function expects a void*, but you want to give it a char. The only
choice you have is to give it the _address_ of your char variable. This
_address_ gets cast into a void*.

For example:

char var;
var = 'A';

Internally var could be an 8 bit section of RAM at address 0x123ABC
(I'm making up the address, but you get the point).

If you pass (void *)&var then you are saying (void *)0x123ABC. But, if
you simply type (void *)var then what you are really doing is taking
the char value of 'A' (which is 0x41, or 65) and casting _that_ into a
void *.

Luckily, the compiler catches the error and warns you that you are
using a small integer value (char) as the source for conversion.

Read up on memory addresses, pointers, and such. It sounds like you are
still a bit shaky.

-Jason

Nov 14 '05 #4
Thanks
Read up on memory addresses, pointers, and such. It sounds like you are
still a bit shaky.
Yes, I'm new to C and in the past I've programmed in Java so pointers and
such is new to me.

"Jason" <ma******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Juggernaut wrote:
What you really want to do is (void *)&var. This will achieve, I

believe,
what you are trying to do.

In the function or the p_thread_create?


Yep

The reason for the void thing is because, if I've understood

correctly,
p_thread creates demands a void variable.


void * you mean...

And I need the passed value to be a char.


The function expects a void*, but you want to give it a char. The only
choice you have is to give it the _address_ of your char variable. This
_address_ gets cast into a void*.

For example:

char var;
var = 'A';

Internally var could be an 8 bit section of RAM at address 0x123ABC
(I'm making up the address, but you get the point).

If you pass (void *)&var then you are saying (void *)0x123ABC. But, if
you simply type (void *)var then what you are really doing is taking
the char value of 'A' (which is 0x41, or 65) and casting _that_ into a
void *.

Luckily, the compiler catches the error and warns you that you are
using a small integer value (char) as the source for conversion.

Read up on memory addresses, pointers, and such. It sounds like you are
still a bit shaky.

-Jason

Nov 14 '05 #5
"Juggernaut" <ju************@hotmail.com> writes:
I am trying to create a p_thread
pthread_create(&threads[k], &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 from integer of different size.


Right, because you're casting to a pointer type (void*) from an
integer of a different size (char). (Remember that char is an integer
type.)

pthread_create is strictly off-topic here, but the issues you're
running are really language issues.

Suppose you have a function called foo_create declared like this:

int foo_create(foo_t *foo,
something_t *something,
void *(*start_routine)(void *),
void *arg);

(You may notice a resemblance to pthread_create, which I won't discuss
further since it's off-topic.)

The foo_create() function attempts to create a "foo" (whatever that
may be). Part of the process of creating a "foo" involves calling a
user-defined start routine. The third argument to foo_create is a
pointer to this start routine (a function that takes a single void*
argument), and the fourth argument is the void* value to be passed to
the start routine.

Why void*? Because void* is a generic pointer type. Any object
pointer type can be converted to void*, and vice versa; a void*
pointer can point to any object of any type. foo_create() doesn't
have to know what kind of data you want to pass to your start
function; only the function itself has to know. This is similar to
the method used by the standard qsort() function, which takes a
pointer to a comparison function that in turn takes two void*
arguments, pointers to the values being compared.

You can't call your start routine directly; you call foo_create(),
which calls your start routine. So any information you want to pass
to your start routine has to go through a parameter of type void*.

Converting between integer types and pointer types, as you've
attempted to do, is seldom a good idea. If you really want your start
routine to accept a char argument, you can probably get away with
converting the char value to void*, and letting the start routine
convert the void* back to char, but that's ugly. C has little enough
type checking as it is; subverting it is likely to get you into
trouble. (Note that your start routine can't be declared to take a
char argument; for compatibility with the definition of foo_create(),
it has to accept a void* argument.)

The idea of the void* argument isn't to squeeze information *into* the
pointer argument, it's to use it to *point to* the information you
want. If you need your start routine to accept a larger amount of
information, you can wrap it all in a structure and pass a pointer to
the structure. You'd convert the structure pointer to void* so you
can pass it to foo_create(), foo_create would pass the void* to your
start routine, and your start routine would then convert it from void*
to a structure pointer, which can be used to access the members. (The
conversions can be done implicitly; converting between void* and
another object pointer type doesn't require an explicit cast.)

But if you just want to pass a single char value to your start
routine, the cleanest way to do it is to pass a pointer to a char
object. Your start routine can then convert its void* argument to
char* and dereference it to obtain the original char value.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Thanks for the thourough and absolutely great explanation.

Btw, I'll try to read the FAQ for this group before next time I post. :)
Btw, is there a FAQ I can read for this group?
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Juggernaut" <ju************@hotmail.com> writes:
I am trying to create a p_thread
pthread_create(&threads[k], &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 from integer of different size.
Right, because you're casting to a pointer type (void*) from an
integer of a different size (char). (Remember that char is an integer
type.)

pthread_create is strictly off-topic here, but the issues you're
running are really language issues.

Suppose you have a function called foo_create declared like this:

int foo_create(foo_t *foo,
something_t *something,
void *(*start_routine)(void *),
void *arg);

(You may notice a resemblance to pthread_create, which I won't discuss
further since it's off-topic.)

The foo_create() function attempts to create a "foo" (whatever that
may be). Part of the process of creating a "foo" involves calling a
user-defined start routine. The third argument to foo_create is a
pointer to this start routine (a function that takes a single void*
argument), and the fourth argument is the void* value to be passed to
the start routine.

Why void*? Because void* is a generic pointer type. Any object
pointer type can be converted to void*, and vice versa; a void*
pointer can point to any object of any type. foo_create() doesn't
have to know what kind of data you want to pass to your start
function; only the function itself has to know. This is similar to
the method used by the standard qsort() function, which takes a
pointer to a comparison function that in turn takes two void*
arguments, pointers to the values being compared.

You can't call your start routine directly; you call foo_create(),
which calls your start routine. So any information you want to pass
to your start routine has to go through a parameter of type void*.

Converting between integer types and pointer types, as you've
attempted to do, is seldom a good idea. If you really want your start
routine to accept a char argument, you can probably get away with
converting the char value to void*, and letting the start routine
convert the void* back to char, but that's ugly. C has little enough
type checking as it is; subverting it is likely to get you into
trouble. (Note that your start routine can't be declared to take a
char argument; for compatibility with the definition of foo_create(),
it has to accept a void* argument.)

The idea of the void* argument isn't to squeeze information *into* the
pointer argument, it's to use it to *point to* the information you
want. If you need your start routine to accept a larger amount of
information, you can wrap it all in a structure and pass a pointer to
the structure. You'd convert the structure pointer to void* so you
can pass it to foo_create(), foo_create would pass the void* to your
start routine, and your start routine would then convert it from void*
to a structure pointer, which can be used to access the members. (The
conversions can be done implicitly; converting between void* and
another object pointer type doesn't require an explicit cast.)

But if you just want to pass a single char value to your start
routine, the cleanest way to do it is to pass a pointer to a char
object. Your start routine can then convert its void* argument to
char* and dereference it to obtain the original char value.

--
Keith Thompson (The_Other_Keith) ks***@mib.org

<http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.

Nov 14 '05 #7
"Juggernaut" <ju************@hotmail.com> writes:
Thanks for the thourough and absolutely great explanation.

Btw, I'll try to read the FAQ for this group before next time I post. :)
Btw, is there a FAQ I can read for this group?


Yes, it's at <http://www.eskimo.com/~scs/C-faq/top.html>. The
compressed text version at <ftp://ftp.eskimo.com/u/s/scs/C-faq/faq.gz>
is more up-to-date, but less convenient (don't ask me why the HTML
version hasn't been updated).

There's also a periodically posted welcome message that deals with the
newsgroup (the C FAQ deals mostly with the language). There are
several URLs with posting advice; I don't have them handy, but I'm
sure someone will post a followup.

Incidentally, one of the things emphasized in the welcome message is
that top-posting is considered poor style around here. Any new text
should follow (or be interspersed with) any quoted text, and quoted
text should be trimmed down to what's necessary for context.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Juggernaut wrote:
I am trying to create a p_thread
pthread_create(&threads[k], &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 from integer of different size.

The fact that it works with an integer is coincidence and is undefined
behaviour (probably the 'int' is physically the same size as a 'void *',
but it doesn't necessarily have to be). Actually, an 'int *' might be of
different representation/size to a 'char *'.

Typically (on say x86), sizeof(char)==1; sizeof(void *)==4. This is what
I think the compiler is complaining about.
Now I thought that when it was a void I could pass anything? Thing is it
works when I use an int, but in this case I wanted to use a char. It wouldnt
be hard to work around it, but it annoys me because I've heard anything can
be passed as long as its a void.

I have remembered to declare the function:
void *Teste(void *var)

and in the main method that created the thread I have declared the var
variable :
char var;
var = 'A'

and the function itself: void *Teste(void *var)

Now If I were to change the char declaration of var to f.example:
int var;
var = 1;

It works.

Now as I said I get this message:
test.c:58: warning: cast to pointer from integer of different size.

Why? And where does the pointer from integer come from? Theres no integer
involved, Im trying to pass a void thats a char.

If anyone can help me I would highly apriciate it.


I did a quick "google" and you might be interested in the page:

http://lists.svlug.org/pipermail/svl...ne/027551.html

This is already some answers to your questions (and maybe some more)
Nov 14 '05 #9
Keith Thompson <ks***@mib.org> spoke thus:
There's also a periodically posted welcome message that deals with the
newsgroup (the C FAQ deals mostly with the language). There are
several URLs with posting advice; I don't have them handy, but I'm
sure someone will post a followup.


http://www.ungerhu.com/jxh/clc.welcome.txt
http://benpfaff.org/writings/clc/off-topic.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #10

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

Similar topics

3
by: sam pal | last post by:
I have the following programs and please clear my doubts. Passing the value by reference is same as pointer by reference. Is this true? What is the difference between display and display2? How...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: Steven Taylor | last post by:
Hope someone can assist. I'm trying to answer a book question. I'm going around in circles in relation to 'pointer-to-char'. Object : A short program is to be created, which involves a...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
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...
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
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
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
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
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
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.