LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-30-2004, 06:36 AM   #1
suchi_s
Member
 
Registered: May 2004
Posts: 133

Rep: Reputation: 15
void pointers


Which one of the following is a true statement about non-generic (non-'void*') pointers?
Choice 1
A pointer to one type may not be cast to a pointer to any other type.
Choice 2
For efficiency, pointer values are always stored in machine registers.
Choice 3
Similarly typed pointers may be added to each other.
Choice 4
Similarly typed pointers may be subtracted from each other.
Choice 5
They are always 32-bit values.
 
Old 10-30-2004, 06:41 AM   #2
Anup Kumar
LQ Newbie
 
Registered: Feb 2004
Posts: 9

Rep: Reputation: 0
Choice 5 is true.
 
Old 10-30-2004, 07:12 AM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Please don't post homework questions. LQ rules clearly state:
Quote:
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
 
Old 10-30-2004, 08:32 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Moreover, don't always trust the answers.
Anup Kumar's one looks wrong to me.
 
Old 10-30-2004, 10:22 AM   #5
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
I thought pointers were system dependent.

I''ve got an 8 bit machine that would absolutely choke on a 32 bit pointer.
 
Old 10-30-2004, 10:30 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
That's what I said, actually 3 and 4 are both true, but 3's result is useless.
 
Old 10-30-2004, 01:41 PM   #7
cetialphav
Member
 
Registered: Sep 2003
Location: Raleigh, NC, USA
Distribution: Fedora
Posts: 88

Rep: Reputation: 16
Actually, 3 is not true. In fact it is specifically illegal.

From K&R2, page 103:

Quote:
The valid pointer operations are assignment of pointers of the same type, adding or subtracting a pointer and an integer, subtracting or comparing two pointers to members of the same array, and assigning or comparing to zero. All other pointer arithmetic is illegal. It is not legal to add two pointers
 
Old 10-30-2004, 02:48 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Oops, my mistake, you're right it's illegal, and compilers enforce it !
Code:
main()
{
        char *a=(char *)malloc(100);
        char *b=(char *)malloc(100);
        printf("%ld\n",b-a);
        printf("%ld\n",b+a);
}

a.c:6: invalid operands to binary +
I should have tested before ...
 
Old 10-30-2004, 06:13 PM   #9
cetialphav
Member
 
Registered: Sep 2003
Location: Raleigh, NC, USA
Distribution: Fedora
Posts: 88

Rep: Reputation: 16
Yeah, its an easy mistake to make because you can add and subtract integers to pointers, but you can only subtract two pointers. It makes sense when you think about it, but it isn't what you would intuitively think.

Not to be too nitpicky, but strictly speaking both of the pointer operations in your test are unsupported. The subtraction of pointers is only meaningful when the pointers are in the same array. Subtraction of pointers in different arrays (as in your test) is undefined. I.e., it won't necessarily be an error but you have no guarantees at all about what you will get.
 
Old 11-08-2004, 03:05 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You're right my example isn't respecting the same array prerequisite, and was just meant to test the compiler behaviour.
That said, pointers being adresses, substracting two of them gives an offset in the program (possibly virtual) memory addressing space which is not an undefined value, although not terribly useful ...
 
  


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
what are void pointers used for? aditya1 Linux - General 10 03-14-2005 03:06 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM
void? Patchorus Programming 9 10-25-2003 07:24 PM
void pointer help gonnaWorkItOut Programming 1 10-12-2003 11:52 AM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM

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

All times are GMT -5. The time now is 10:55 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