Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > generic pointer question

Reply
Thread Tools

generic pointer question

 
 
steve.pagliarulo@gmail.com
Guest
Posts: n/a
 
      12-28-2007
On Dec 28, 2:50*am, Logan Lee <(E-Mail Removed)> wrote:
> int
> main()
> {
> * int i;
> * char c;
> * void *the_data;
>
> * i = 6;
> * c = 'a';
>
> * the_data = &i;
> * printf("the_data points to the integer value %d\n", *(int*) the_data);
> * * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^
> * the_data = &c;
> * printf("the_data now points to the character %c\n", *(char*) the_data);
> * * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^^
> * return 0;
>
> }
>
> On ^^^^ why can't them be typecasted with int and char respectively like
> this:
> (int) the_data
> (char) the_data


It sounds like you are looking for a typeless language. C is not a
typeless language. C++ templates can help but not straight C.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      12-28-2007
Logan Lee wrote:
>
> int main() {
> int i;
> char c;
> void *the_data;
>
> i = 6;
> c = 'a';
> the_data = &i;
> printf("the_data points to the integer value %d\n", *(int*) the_data);
> the_data = &c;
> printf("the_data now points to the character %c\n", *(char*) the_data);
> return 0;
> }
>
> On why can't them be typecasted with int and char respectively
> like this:
> (int) the_data
> (char) the_data


Works fine, with the appropriate #include:

[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main(void) {
int i;
char c;
void *the_data;

i = 6;
c = 'a';

the_data = &i;
printf("*the_data is integer value %d\n", *(int*)the_data);
the_data = &c;
printf("*the_data now is the char %c\n", *(char*)the_data);
return 0;
}

[1] c:\c\junk>cc junk.c

[1] c:\c\junk>.\a
*the_data is integer value 6
*the_data now is the char a

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



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

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      12-28-2007
Richard Heathfield wrote:
> Logan Lee said:
>
> <snip>
>
>> On ^^^^ why can't them be typecasted with int and char
>> respectively like this:
>> (int) the_data
>> (char) the_data

>
> It's a pointer. Interpreting it as something else would be like
> trying to interpret the Suez Crisis as a small currant bun topped
> with icing.


I'm getting hungry.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-29-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> On Dec 28, 2:50*am, Logan Lee <(E-Mail Removed)> wrote:
>> int
>> main()
>> {
>> * int i;
>> * char c;
>> * void *the_data;
>>
>> * i = 6;
>> * c = 'a';
>>
>> * the_data = &i;
>> * printf("the_data points to the integer value %d\n", *(int*) the_data);
>> * * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^
>> * the_data = &c;
>> * printf("the_data now points to the character %c\n", *(char*) the_data);
>> * * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^^
>> * return 0;
>>
>> }
>>
>> On ^^^^ why can't them be typecasted with int and char respectively like
>> this:
>> (int) the_data
>> (char) the_data

>
> It sounds like you are looking for a typeless language. C is not a
> typeless language. C++ templates can help but not straight C.


What makes you think the OP is looking for a typeless language? I
have no idea *why* he wants to cast the_data to int or to char. (In a
typeless language, casting would be meaningless.)

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
spaglia
Guest
Posts: n/a
 
      12-29-2007
On Dec 28, 8:38*pm, Keith Thompson <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > On Dec 28, 2:50*am, Logan Lee <(E-Mail Removed)> wrote:
> >> int
> >> main()
> >> {
> >> * int i;
> >> * char c;
> >> * void *the_data;

>
> >> * i = 6;
> >> * c = 'a';

>
> >> * the_data = &i;
> >> * printf("the_data points to the integer value %d\n", *(int*) the_data);
> >> * * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^
> >> * the_data = &c;
> >> * printf("the_data now points to the character %c\n", *(char*) the_data);
> >> * * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^^
> >> * return 0;

>
> >> }

>
> >> On ^^^^ why can't them be typecasted with int and char respectively like
> >> this:
> >> (int) the_data
> >> (char) the_data

>
> > It sounds like you are looking for a typeless language. C is not a
> > typeless language. C++ templates can help but not straight C.

>
> What makes you think the OP is looking for a typeless language? *I
> have no idea *why* he wants to cast the_data to int or to char. *(In a
> typeless language, casting would be meaningless.)
>
> --
> Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
> [...]
> "We must do something. *This is something. *Therefore, we must do this.."
> * * -- Antony Jay and Jonathan Lynn, "Yes Minister"- Hide quoted text -
>
> - Show quoted text -


As you say I was focusing on the why and pointing out that C does not
intrinsically handle polymorphic types. In his example he wants to
treat the_data as a polymorphic type. Perhaps I'm reading too much
into his quetion and it is in fact about casts.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments