On a test in college an instructor asked if it was possible to multiply and divide pointers. In the book we are using it says nothing about it, and I was just wondering if you could.

Absolutely. A pointer is just an integer. Although it refers to a location in memory, you can perform operations on a pointer just like you can with any other integer.

You must, of course, be careful because when you use a pointer, you must be certain it points to a valid memory address (generally, this means to memory that your application owns).

Normally, C makes it easy to compute addresses without multiplying and dividing the actual pointer. For example, say you have a pointer to an integer array and you want to figure out the address of an element whose position is equal to x * y.

*p += (x * y)
Here, I’ve used standard integers (assuming x and y are integers) and simply added the result to a pointer. This is much more common, because we usually rely on the operating system to determine where our memory objects are located. By simply adding to an existing address, we are adding to an address we assume is safe.

It is also important to note that, while each integer uses 4 bytes (in 32-bit C), we didn’t need to multiply by 4. This is because C does this for us automatically. Generally, whenever C performs math on a pointer, it automatically multiplies numbers by the number of bytes used by the data type being pointed to.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may