473,385 Members | 1,370 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,385 software developers and data experts.

why c++ not allowed mutlipication and division operation with pointer aritmetic?

c++ only allow addition and subtraction operation with pointer .why multiplication and division is not allowed?
then how to perform multiplication and division with pointer
Jul 28 '13 #1
3 3385
weaknessforcats
9,208 Expert Mod 8TB
As far as I know, your statement is not true.

Can you show an example where you can't use multiplication and division in pointer arithmetic.

Remember, multiplication is just repetitive adding and division is repetitive subtracting.
Jul 28 '13 #2
Banfa
9,065 Expert Mod 8TB
It is true, pointer arithmetic consists of either add or subtracting an integer from a pointer. The pointer is moved along in units of the thing pointed to, i.e. for the type T

Expand|Select|Wrap|Line Numbers
  1.   T* pt = ...;
  2.  
  3.   pt += N;
  4.  
the absolute value of pt is changed by sizeof(T) * N.

Multiplication and division do not make sense for pointer arithmetic in the same way that addition and subtraction do. If the semantic meaning of pt += 2 move pt forward through the memory map by 2 times the size of the object it points to what would the semantic meaning of pt *= 2 be?

The answer is it has no meaning and therefore the operation is not allowed.
Jul 29 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
It makes sense now that you explain it.
Jul 29 '13 #4

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

Similar topics

3
by: Peter L. | last post by:
Hi! I would like to know if it's allowed to dereference a pointer to a class like im doing it. I'm building a tree with nodes. class node { private: int num;
26
by: HP | last post by:
Hi guys i wana remove this warning which i am getting during my codcomilation. the warning is - Possible use of null pointer 'PnoSytGenericTest::_response' in left argument to operator '->'
87
by: Vijay Kumar R Zanvar | last post by:
Hi, Why multiplication of pointers is not allowed? Till now I only know this, but not the reason why! PS: As a rule, I searched the FAQ, but could not find an answer. -- Vijay Kumar R...
4
by: Amjad | last post by:
How can I display the result of a division operation in 2 dicimal digits only? e.g. Dim MyValue As Double = 10 / 3 ' Returns 3.333333, I'd like to get 3.33 Thanks
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
13
by: Michal Nazarewicz | last post by:
The following code compiles fine (with warnings though) under GCC with -pedantic and -ansi arguments: #v+ int main(void) { const int a = 0; int *p = &a; return 0; } #v-
3
by: G | last post by:
Hi~ you guys. I come across the code below: ///////////////////////////////////////////////////////// #include <stdio.h> void foo(const char **pp) { *pp=NULL; //it works too //*pp="Hello...
51
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember...
10
by: Artemio | last post by:
Hello all! I just stumbled across a weird problem with precision of a division operation. I am on Mac OS X, GCC 4.0.1. Say I have two float or double numbers, and I want to divide one by...
9
by: haijin.biz | last post by:
I tried the following code and found that the form #include <iostream> using namespace std; int main(int argc, char** argv) { //int a; // error C2466: cannot allocate an array of constant...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.