The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Help with pointers
Discuss Help with pointers in the C Programming forum on Dev Shed. Help with pointers C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 31st, 2012, 12:42 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 7
Time spent in forums: 2 h 57 m 24 sec
Reputation Power: 0
|
|
|
Help with pointers
Hi, is there any way to divide two numbers and module two numbers without using arithmetic operators ('%' and '/') ?
I need to use just pointers..
TNX 
|

October 31st, 2012, 02:02 PM
|
|
|
Quote: | Originally Posted by Jan4185 Hi, is there any way to divide two numbers and module two numbers without using arithmetic operators ('%' and '/') ?
I need to use just pointers..
TNX  |
No.
|

October 31st, 2012, 02:10 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
Yes. Actually, there are still processors out there that don't implement division in hardware. To divide two arbitrary numbers on such a processor, one has to implement division using the other operations (meaning without '%' and '/', and sometimes even without multiplication). (Compilers should do this for you, should you encounter such a chip in real life.)
I have no idea what "I need to use just pointers" means. Can you provide a more accurate description of your problem?
|

October 31st, 2012, 02:11 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 7
Time spent in forums: 2 h 57 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by bdb No. |
Are you sure ? this is my HW..
tnx anyway.. 
|

October 31st, 2012, 02:16 PM
|
|
|
Quote: | Originally Posted by Jan4185 Are you sure ? this is my HW..
tnx anyway..  |
I think you miscopied your HW.
It is impossible to calculate division and/or modulus with just pointers.
Maybe you want to calculate without the operators / and % but with all the other operators available in C (including + and - and * ...)? ???
|

October 31st, 2012, 02:18 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 7
Time spent in forums: 2 h 57 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by bdb I think you miscopied your HW.
It is impossible to calculate division and/or modulus with just pointers.
Maybe you want to calculate without the operators / and % but with all the other operators available in C (including + and - and * ...)? ??? |
Yes, I'm sorry. I need to divide and module 2 numbers without using '/' or '%'. But I must use pointers.
|

October 31st, 2012, 02:30 PM
|
|
|
Quote: | Originally Posted by Jan4185 Yes, I'm sorry. I need to divide and module 2 numbers without using '/' or '%'. But I must use pointers. |
Division (and modulus) aren't but "glorified" subtraction.
So just divide using subtraction and use a pointer:
Code:
int main(void) {
printf("42 / 13 is %d\n", divide(42, 13)); /* functions divide() and modulus() */
printf("42 % 13 is %d\n", modulus(42, 13)); /* left as an exercise for the reader */
int *pointer; /* <== look: a pointer! */
return 0;
}
|

October 31st, 2012, 03:02 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 62
  
Time spent in forums: 1 Day 1 h 9 m 54 sec
Reputation Power: 2
|
|
You could also try using bitwise operators, for instance, this code here. I guess that you could send a memory pointer that points to an integer and do some low-level stuff with it, depending on the compiler, you can do something like this - remembering that the C convention means that the top of the stack will be y and x respectively. You may also need to consider word boundaries depending on the host processor.
Code:
void multiply(int &x, int &y)
{
#asm
PUSH DE
PUSH HL
// Do low-level operations here
POP HL
POP DE
RET
#endasm
}
That's Z80, but x86 assembly should be similar. I think all processors have the equivalent of push and pop anyway, and modern processors will have multiply instructions, though it's usually quicker to use bitwise operators. Remember, most processors use the first in, last out thingy when pushing and pulling from the stack.
Oh, and the equivalent of the #asm tag on some comilers is __asm( /** code here */ ); or something, but don't quote me on that.
Good luck!
Shaun.
Last edited by Shaun_B : October 31st, 2012 at 03:06 PM.
Reason: Forgot the closing tag... and forgot with registers I used too :-\
|

October 31st, 2012, 07:34 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | Originally Posted by Jan4185 Yes, I'm sorry. I need to divide and module 2 numbers without using '/' or '%'. But I must use pointers. | No, thank you. My opinion on this type of task is that asking students to implement something without using X, or only using X, Y, and Z, make for good problem-solving exercises. (Heck, they even sort of prepare you for real life, where you might have to work under restrictions.) However, saying that you must use X is idiotic. I'd be willing to help you understand division algorithms, but I have no desire to play the game where we're forced to use pointers somewhere in our implementation.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|