473,899 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why multiplication not allowed?

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 Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05
87 11275
Sean Kenwrick <sk*******@hotm ail.com> scribbled the following:
But I think the reason I didn't get the job was because one of the other
questions the interviewer asked me was to tell him what was wrong with the
following statement: i=+i; I said that there was nothing wrong with it, but the interviewer claimed
that it was an ambiguous statement because it could mean adding i to itselt
or setting i to +i. I got into an argument with him saying that he must
be confused with i+=i; but he was adament he was right and got annoyed that
I was disagreeing wih him. Afterwards I wondered whether this might have been some kind of syntax left
over from a very early incarnation of C which was subsequently dropped.
Perhaps the members of this group could enlighten me about this since I
think that this guy was just a complete idiot.


Yes, this is an obsolete feature of C. =+ and =- originally meant the
same as += and -=. Whoever designed them that way must have been
drinking something really strong. AFAIK they were dropped when ISO
standardised C.
That the interviewer still clung to the obsolete meanings of those
operators makes me feel that he wasn't the proper person to interview
you about C.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Parthenogeneti c procreation in humans will result in the founding of a new
religion."
- John Nordberg
Nov 14 '05 #21
Sean Kenwrick wrote:

<snip>
the interviewer asked me was to tell him what was wrong with the
following statement:

i=+i;

I said that there was nothing wrong with it, but the interviewer claimed
that it was an ambiguous statement because it could mean adding i to
itselt
or setting i to +i. I got into an argument with him saying that he must
be confused with i+=i; but he was adament he was right and got annoyed
that I was disagreeing wih him.
Getting into an argument is unwise. Personally, even if I hadn't known about
this particular gotcha, I'd possibly have weaseled my way through it,
because the first complaint I'd have made would have been about the absence
of whitespace. I then would have explained why whitespace is important.
Afterwards I wondered whether this might have been some kind of syntax
left over from a very early incarnation of C which was subsequently
dropped.
Precisely so. =+ is the old way of writing +=
The last question he asked was for me to declare a pointer to a funtion
that returned a pointer to a function that returned a pointer to a char.


Build up logically. We need a return value that is a pointer to a function
that returns a pointer to a char:

typedef char * (*pfrc)();

With that typedef, the solution becomes easy:

pfrc (*solution)();

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #22

"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:bt******** **@oravannahka. helsinki.fi...
Sean Kenwrick <sk*******@hotm ail.com> scribbled the following:
Your solution above is correct but can you figure out why the following code is invalid (which it is):

*k = *k/*j;

assuming k and j are pointers to ints that have been correctly
initialized....


Are you testing our knowledge? The C tokeniser thinks of the /* as the
start of a comment, so it ends up being:
*k = *k
which is a syntax error because the lack of a terminating semicolon.
Were you thinking of using it in some way like this?
*k = *k/*j; /* make sure the value of *k stays in bounds */
+0; /* this is just a dummy */


Yeah you got it. By dividing *k by *j (without spaces) you get a syntax
error.

*k=*k/*j;

you get a syntax error because the /* starts a comment. I was given this
as a question in an interview for a job when I was a student (and I didn't
get the question right (or the job!)).

But I think the reason I didn't get the job was because one of the other
questions the interviewer asked me was to tell him what was wrong with the
following statement:

i=+i;

I said that there was nothing wrong with it, but the interviewer claimed
that it was an ambiguous statement because it could mean adding i to itselt
or setting i to +i. I got into an argument with him saying that he must
be confused with i+=i; but he was adament he was right and got annoyed that
I was disagreeing wih him.

Afterwards I wondered whether this might have been some kind of syntax left
over from a very early incarnation of C which was subsequently dropped.
Perhaps the members of this group could enlighten me about this since I
think that this guy was just a complete idiot.

The last question he asked was for me to declare a pointer to a funtion that
returned a pointer to a function that returned a pointer to a char.
Needless to say, without my K&R book to hand I messed that up as well (but
by now I got the feeling that this guy was just being mean).

In the end they gave the job to a female friend of mine who by her own
admission was not a great programmer (she got me to do all her programming
assignments in return for her doing all my essay assignments).

Sean
Nov 14 '05 #23
Sean Kenwrick wrote:
[snip]
But I think the reason I didn't get the job was because one of the other
questions the interviewer asked me was to tell him what was wrong with the
following statement:

i=+i;

I said that there was nothing wrong with it, but the interviewer claimed
that it was an ambiguous statement because it could mean adding i to itselt
or setting i to +i. I got into an argument with him saying that he must
be confused with i+=i; but he was adament he was right and got annoyed that
I was disagreeing wih him.

Afterwards I wondered whether this might have been some kind of syntax left
over from a very early incarnation of C which was subsequently dropped.
Perhaps the members of this group could enlighten me about this since I
think that this guy was just a complete idiot.


ISTR an article by one of the original C developers (Dennis Ritchie or
perhap PJ Plauger) commenting on the /very/ early syntax of C. Specifically,
the article said that the syntax

i =+ j;

had been considered as a way to express

i = i + j;

but was ultimately rejected because it was ambigious, and could be confused with

i = j;

If I read http://cm.bell-labs.com/cm/cs/who/dmr/chist.html correctly, it
looks like this ambigious syntax was adopted from B into the earliest of C
compilers, but later dropped (in 1976) before the adoption of "K&R C".

[snip]

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 14 '05 #24
Joona I Palaste wrote:
Sean Kenwrick <sk*******@hotm ail.com> scribbled the following:
But I think the reason I didn't get the job was because one of the other
questions the interviewer asked me was to tell him what was wrong with the
following statement:
i=+i;


I said that there was nothing wrong with it, but the interviewer claimed
that it was an ambiguous statement because it could mean adding i to itselt
or setting i to +i. I got into an argument with him saying that he must
be confused with i+=i; but he was adament he was right and got annoyed that
I was disagreeing wih him.


Afterwards I wondered whether this might have been some kind of syntax left
over from a very early incarnation of C which was subsequently dropped.
Perhaps the members of this group could enlighten me about this since I
think that this guy was just a complete idiot.

Yes, this is an obsolete feature of C. =+ and =- originally meant the
same as += and -=.


And =* meant the same as *=, which made expressions like
x=*p;
needlessly ambiguous. splint, the lint-like program I use for laughs,
still complains about things stemming from that wart.
Whoever designed them that way must have been
drinking something really strong.


Indeed.
Nov 14 '05 #25
On Wed, 07 Jan 2004 05:41:07 -0700, August Derleth <em***@for.addr ess>
wrote:
The question Ben is asking is, what would you propose pointer
multiplication do? How would you define the operation? I can't think of
any way in which multiplying a pointer by any other value would be
useful or even meaningful.


The way I reason is like this: If I take i and assign an address to it
(that is, I make it a pointer), i is the name of a block of memory that
holds a certain string of bits or trits or decimal digits that compose
that address. At this layer of abstraction, it's no different from an
int or a float. Since I can multiply two ints and stand a good chance at
getting a meaningful result, why not two pointers? Or an int and a pointer?


You didn't answer the question. What meaning would you ascribe to the
result of the operation?

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #26
In article <26************ **************@ posting.google. com>,
jd*********@yah oo.com (James Dow Allen) wrote:
Why multiplication of pointers is not allowed?
While I've never felt an urge to multiply pointers, there
is a situation where adding them is quite legitimate:
char *rcopy, *q, *r;
. . .
strcpy(rcopy, r);
q = index(r, '*');
. . .
/* Now point to the '*' in the copy */
foobar(rcopy + q - r);
(Please don't write in to tell me I forgot to allocate space for rcopy, etc.
This is just a minimal illustration of the point.)

Although the intermediate expression (rcopy + q) has the illegal form
of (ptr + ptr) the net expression is OK.


If the intermediate expression is illegal, then the whole expression is
illegal. Period.

I was disappointed when gcc rejected my construction like this, depending
on how I parenthesized it. It seems like it shouldn't be hard to support.

Yes, yes; the better parenthesizatio n would also make the program more
readable. Well nevermind ....


Adding pointers is still meaningless. What you're trying to do in
this situation is get the difference of two pointers, and add that
difference to another pointer. What you want is:

rcopy + (q - r)

Which is not ptr + ptr, it is ptr + ptrdiff. The better parenthesized
version is not just "more readable", it is legal.

Nov 14 '05 #27
Sean Kenwrick wrote:
.... snip ...
But I think the reason I didn't get the job was because one of the
other questions the interviewer asked me was to tell him what was
wrong with the following statement:

i=+i;

I said that there was nothing wrong with it, but the interviewer
claimed that it was an ambiguous statement because it could mean
adding i to itselt or setting i to +i. I got into an argument
with him saying that he must be confused with i+=i; but he was
adament he was right and got annoyed that I was disagreeing wih him.

Afterwards I wondered whether this might have been some kind of
syntax left over from a very early incarnation of C which was
subsequently dropped. Perhaps the members of this group could
enlighten me about this since I think that this guy was just a
complete idiot.


You've got it. He had just been around from pre-standardization
days. Note that proper civilized use of blanks untangles
everything.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #28
Richard Heathfield <in*****@addres s.co.uk.invalid > writes:
The last question he asked was for me to declare a pointer to a funtion
that returned a pointer to a function that returned a pointer to a char.


Build up logically. We need a return value that is a pointer to a function
that returns a pointer to a char:

typedef char * (*pfrc)();

With that typedef, the solution becomes easy:

pfrc (*solution)();


It's also easy without a helper type if you read (or build up) the
declaration "from inside to outside" instead of from left to right:

p The identifier `p'...
*p ...is a pointer...
(*p) () ...to a function...
*(*p) () ...which returns a pointer...
(*(*p) ()) () ...to a function...
*(*(*p) ()) () ...which returns a pointer...
char *(*(*p) ()) (); ...to `char'.

Martin
Nov 14 '05 #29
August Derleth wrote:
Kevin Goodsell wrote:

The question Ben is asking is, what would you propose pointer
multiplication do? How would you define the operation? I can't think
of any way in which multiplying a pointer by any other value would be
useful or even meaningful.

The way I reason is like this: If I take i and assign an address to it
(that is, I make it a pointer), i is the name of a block of memory that
holds a certain string of bits or trits or decimal digits that compose
that address. At this layer of abstraction, it's no different from an
int or a float. Since I can multiply two ints and stand a good chance at
getting a meaningful result, why not two pointers? Or an int and a pointer?


Because the result is *not* meaningful for pointers.

Your question is somewhat like asking "why can't I assign to a
function?" or "why can't I take the square root of a struct?" There's
just no logical reason why you *should* be able to.

My response to this is that the compiler shouldn't hold my hand that
much. As Kernighan said, "If you want PL/I, you know where to get it."
In Forth, for example, a pointer is simply a bunch of bits on the stack,
and you can do unto a pointer the same as you can do unto an int or
anything else. The only extra feature is that, if you dereference it,
you stand a good chance of getting something meaningful.

I suppose if I want Forth, I know where to find it.


All I can say to this is that I'm glad you didn't design C.

You might also be interested in BCPL. I know almost nothing about it,
but I believe I read that it only had 1 type: the machine word. I
imagine this would give you the kind of complete freedom from
hand-holding that you seem to want. (It's also possible that the
language I'm thinking of is B, not BCPL.)

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2296
by: Soeren Sonnenburg | last post by:
Hi all, Just having started with python, I feel that simple array operations '*' and '+' don't do multiplication/addition but instead extend/join an array: a= >>> b= >>> a+b
9
8008
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
11
4890
by: mjdeesh_hi | last post by:
How can we perfom multiplication programatically without using + or * operator. Can any one help out in this one. Jagadeesh.
11
5246
by: Skybuck Flying | last post by:
Hello, type Tbig = array of byte; vAddTable : array of array of byte; // = value // = transport/carry vMulTable : array of array of byte;
1
9181
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void recMultiply(int i, int j, float a, int k, int l, float b, int x, int y, float c, int s); int i, j, k, s, matrixsize, blocksize, jj, kk, power, bsize; float sum, maxr, total=0.0, startmult, finishmult, multtime; float* A = NULL; float* B = NULL;
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11276
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9671
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8043
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3320
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.