LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2010, 08:49 PM   #1
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Rep: Reputation: 15
Typecasting void pointer


Hello Geeks,

I have 32-bit app,

short *app;
int *int_app;
void *p;

// Some code

app = (short *)p;
int_app = (int *)p;

This works fine on 32-bit platform. However it fails on 64-bit platform. It says " cast to pointer from integer of different size"

But, if I use long * pointer, then there won't be any error? I know that void * pointer on 64-bit is 64 bits size.

Any idea about this error?

Thanks,
Saurabh

Last edited by saurabhchokshi; 04-13-2010 at 08:53 PM. Reason: Missing Information
 
Old 04-13-2010, 09:10 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by saurabhchokshi View Post
Hello Geeks,

I have 32-bit app,

short *app;
int *int_app;
void *p;

// Some code

app = (short *)p;
int_app = (int *)p;

This works fine on 32-bit platform. However it fails on 64-bit platform. It says " cast to pointer from integer of different size"

But, if I use long * pointer, then there won't be any error? I know that void * pointer on 64-bit is 64 bits size.

Any idea about this error?

Thanks,
Saurabh
Don't use 'int', 'long', etc., just use 'size_t' - look it up using your favorite WEB search engine.

Solution with 'size_t' is portable (at least, I believe so based on what I read about it).
 
Old 04-14-2010, 10:45 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Is there a typo in your post? Seems to work fine here:
Code:
~/tmp$ uname -sm
Linux x86_64
~/tmp$ cat ptr.c
void func(void)
{
    short *app;
    int *int_app;
    void *p;

    app = (short *) p;
    int_app = (int *)p;
}

~/tmp$ make ptr.o
gcc -Wall -Wextra -g -ansi -pedantic   -c -o ptr.o ptr.c
~/tmp$
Quote:
just use 'size_t'
I think intptr_t is the type intended for this use.
 
Old 04-14-2010, 10:56 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ntubski View Post
...
I think intptr_t is the type intended for this use.
Probably.

However, because of 'size_t' definition it's equivalent. I.e. should be big enough to accommodate any array index, and array my consist of bytes (the smallest addressable unit in "C") and be as big as the whole address space (minus "epsilon" needed for stack, program itself, etc).

So, due to

Code:
a[i] == *(a + i)
identity/property of "C" 'size_t' is big enough for any pointer.
 
Old 04-14-2010, 12:41 PM   #5
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Original Poster
Rep: Reputation: 15
That means I can't use char * as a String in C to typecast any void pointer data. As we do in pthread_create().

Any idea how should I use char * for pthread_create thread?
 
Old 04-14-2010, 01:00 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by saurabhchokshi View Post
That means I can't use char * as a String in C to typecast any void pointer data. As we do in pthread_create().

Any idea how should I use char * for pthread_create thread?
This is the signature:

Code:
int pthread_create
  (
  pthread_t *thread,
  const pthread_attr_t *attr,
  void *(*start_routine) (void *),
  void *arg
  );
.

You probably mean the 'void *arg' part. In "C" 'void *' pointer is compatible with any other pointer, i.e. pointer of any other type can be used where 'void *' pointer is expected.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C - How do you malloc for a void pointer? golmschenk Programming 4 03-20-2010 05:21 PM
Help on void pointer yfaye Programming 7 04-18-2009 07:10 AM
void * pointer in function xailer Programming 23 01-16-2004 02:14 PM
void pointer help gonnaWorkItOut Programming 1 10-12-2003 11:52 AM
g++ delete [] void pointer? ugenn Programming 5 06-01-2002 03:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration