Pointers

Pointers

Question:
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.

Answer:
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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes