3

If i have a char pointer, say char *ptr, that holds the address of a single ascii character, and want to use the modulo operator on that address, how do i do so? Whenever I try and do this with a basic operation such as int modulo = ptr % 16, i get the error "invalid operands to binary %" I have a weak grasp on binary arithmetic, so I know I need to keep developing this skill, but if someone could tell me what concept I'm missing here that would be a great help, thanks.

Edit: Sorry I wasn't as clear with what I was trying to accomplish, but basically I just need to display a block of memory with 16 slots. Its a program where a ascii character is found in an array, which i've already done, and then the block of memory that this ascii character is stored in is then displayed. When I run it now, it displays the found ascii character and the 15 characters that come after the character found and their memory locations. Instead of doing this, if the found ascii character is in the middle of block of memory, I need it to display what is stored around it, not only what comes after it. So like if the found character is in the 23 slot of the array, the program will then display the character and memory locations of what is in slots 16-31 of an array.

5
  • 2
    What are you trying to achieve by doing a % operation on an address ? Commented Mar 26, 2014 at 20:28
  • 1
    why would you need modulo on address? Commented Mar 26, 2014 at 20:28
  • Are you sure you don't intend to do (*ptr)%16? Commented Mar 26, 2014 at 20:40
  • What I'm having to do is after finding a certain ascii character, I need to display the block of 16 memory spaces around this ascii character, so if the character is in the 23 spot of an array, i need to display the block of memory holding spots 16-31 of that array Commented Mar 26, 2014 at 20:43
  • Then do the arithmetic on an integer which is an offset into the array. Commented Mar 26, 2014 at 22:05

2 Answers 2

6

You need to cast to a suitably wide integer temporarily, e.g.

#include <stdint.h>

ptr = (char*)((uintptr_t)ptr % 16);

Whether the resulting pointer is of any practical use is another matter of course.

Sign up to request clarification or add additional context in comments.

1 Comment

The integer result of (uintptr_t)ptr % 16 may be meaningful on some systems. The result of converting that integer to char* is almost certainly meaningless. I suspect (while waiting for feedback from the OP) that the desire is for an integer result.
4

You can't apply % to a pointer, any more than you can take the square root of a string. Pointers are not numbers.

You can, as Paul's answer mentions, convert the pointer to an integer (uintptr_t, defined in <stdint.h>, is the best choice) and then apply % to the result, but there is no guarantee that it means anything. In fact I've worked on systems where the result doesn't mean what you might expect it to mean, because pointers have an unusual (but perfectly valid) representation.

You need to explain just what you're trying to accomplish. (I can guess, but I'd rather not try.) We can probably help you with that (though there may not be a portable solution).

UPDATE :

Since you're dealing with elements of an array, there's no need to play games with pointer arithmetic (at least not directly).

In your example, given an index value of 23, you want to display elements 16 through 31. You can easily do this by performing integer arithmetic on index values. For example (untested code follows):

char arr[256];
int index = 23;
int start = index - (index % 16);
int end = start + 15;
for (int i = start; i <= end; i ++) {
    /* whatever */
}

(The relationship between arrays and pointers in C can be confusing. Section 6 of the comp.lang.c FAQ does an excellent job of explaining it.)

7 Comments

Sorry for not being clear with my goal, what I need to be able to do is basically display a block of 16 memory spaces. So for example in an array, if i search for and find the entered ascii character in slot 38 of the array, i need to display the block of memory holding spots 32-47 of that array
@user3071432: Please update your question to clarify what you want to do. But if you have an array, you can use % on an index value rather than playing games with pointers.
Thank you. Classic example of me just overthinking what I needed to do, shouldnt have made it more complicated than I needed to in the first place. Simplest answer worked without flaw.
I just got a second downvote on this answer. Would the downvoter care to comment? If there's something wrong with my answer, I'd like to learn from it.
"Pointers are not numbers." If they're not numbers, then why can you perform other arithmetic operations on them? You even mention casting it to an integer, that wouldn't make sense if they weren't numbers. The only reason it doesn't make sense to take the square root of a string is because it's a collection of numbers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.