473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointer's subtraction or arithmetic

1
Can we subtract a variable value from a pointer
Means is this possible *P-t where t is a variable
Apr 25 '16 #1
1 1128
weaknessforcats
9,208 Expert Mod 8TB
Yes.

But a pointer has a type so ptr + 1 will take the address in the pointer and the sizeof one int to it.

ptr + 5 where the type of ptr is double will take the address in ptr and the sizeof 5 doubles to it.

That is, doing arithmetic with a pointer is not just adding a number of bytes.

This leads to the rule that a pointer changes by the sizeof the thing it points at.

Your example, however, *P -t does not change the pointer. It changes the thing the pointer points at.
Apr 25 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Marc Schellens | last post by:
I have a: vector<A> V; an A* a; which might point to one element of V.
73
by: David Scarlett | last post by:
Is the following code safe? SomeType *a, *b; /* Some code.... */ free(a);
48
by: Daniel Rudy | last post by:
Hello, On a x86 machine, what is the format of a pointer in C? I know for a fact that the x86 p-mode uses a /selector:offset/ notation where the selector is defined in either the GDT or LDT. ...
2
by: Xiangliang Meng | last post by:
Hi, all. As far as I know, the speical arithmetic operator on void pointer is an externsion in GNU CC. However, I could not find the relative topics in C99. Would someone like to help me find...
42
by: Abhishek Jha | last post by:
We cann't add two pointers, We cann't multiply two pointers, We cann't divide two pointers, But We can subtract two pointers. Why ?
1
by: andy_dufresne | last post by:
hi, i'd a question abt taking the difference between two pointers. for eg: if you have 2 char pointers pointing to members of an array, you advance one till you encounter a space then take the...
24
by: Frederick Gotham | last post by:
There is a thread currently active on this newsgroup entitled: "how to calculate the difference between 2 addresses ?" The thread deals with calculating the distance, in bytes, between two...
96
by: subramanian | last post by:
I have been thinking that all pointers(to any obejct) have the same size. The size of a pointer is the size of an int. This is beause a memory location is addressed by an int. Is that right/wrong?
66
by: Praveen | last post by:
Hi, I came across a program as follows main() { int x, y ; int *p1 = &x; int *p2 = &y; printf("%d\n", p1-p2); }
27
by: Jess | last post by:
Hello, the following code failed with "segmentation fault". Could someone tell me why it failed please? Thanks! #include<iostream> #include<string> #include<cstring> #include<cstddef> using...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.