Pointer value modification behaviour
I am C programmer beginner and I have encountered something I cannot fully understand to.
Here is my whole code:
#include <curses.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winuser.h>
#define DELAY 300000
int x = 3, y = 20;
int max_y = 0, max_x = 0;
int next_x = 0;
int directionX = -1;
int directionY = 0;
int main(int argc, char *argv)
{
initscr();
noecho();
curs_set(FALSE);
getmaxyx(stdscr, max_y, max_x);
while(1)
{
// drawing on the map
clear();
mvprintw(y, x, "o");
refresh();
usleep(DELAY);
// make next step
next_x = x + directionX;
//set next step directionX
if (next_x >= max_x || next_x < 0)
{
directionX*= -1;
}
else
{
x+= directionX;
}
}
endwin();
}
The issue I cannot fully understand is described on this picture from my debugging:
Please, could somebody explain to me, how is it possible to assign pointer value to -1 and receive +1?
c
add a comment |
I am C programmer beginner and I have encountered something I cannot fully understand to.
Here is my whole code:
#include <curses.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winuser.h>
#define DELAY 300000
int x = 3, y = 20;
int max_y = 0, max_x = 0;
int next_x = 0;
int directionX = -1;
int directionY = 0;
int main(int argc, char *argv)
{
initscr();
noecho();
curs_set(FALSE);
getmaxyx(stdscr, max_y, max_x);
while(1)
{
// drawing on the map
clear();
mvprintw(y, x, "o");
refresh();
usleep(DELAY);
// make next step
next_x = x + directionX;
//set next step directionX
if (next_x >= max_x || next_x < 0)
{
directionX*= -1;
}
else
{
x+= directionX;
}
}
endwin();
}
The issue I cannot fully understand is described on this picture from my debugging:
Please, could somebody explain to me, how is it possible to assign pointer value to -1 and receive +1?
c
6
It not the dereference operator,*
, it's the multiplication operator,*
. Unfortunately, and probably incorrectly, C (the writers) chose to overload it.directionX*= -1;
is the same asdirectionX = directionX * -1
.
– Fiddling Bits
Nov 24 '18 at 23:23
Oh, thank you, this may be the key!
– user8620575
Nov 24 '18 at 23:24
1
@FiddlingBits What's wrong with the*=
operator? It's consistent with all other operators, and unambiguous. In all pointer, contexts the*
goes on the left of the name.
– Jonathon Reinhart
Nov 24 '18 at 23:30
@JonathonReinhart Nothing is wrong with it. I'm only saying it's unfortunate they overloaded the*
operator.
– Fiddling Bits
Nov 24 '18 at 23:54
4
@FiddlingBits: “Overloading” operators or functions usually refers to the operator or function being different based on operand or argument types. The operation here is distinguished by syntax, and that is not usually called “overloading.” If it were, many operators in many languages are overloaded: parentheses (function call, grouping, and casts), commas (argument list and expression sequence),-
(negation and subtraction),.
(structure members and floating-point constants), and more.
– Eric Postpischil
Nov 25 '18 at 0:05
add a comment |
I am C programmer beginner and I have encountered something I cannot fully understand to.
Here is my whole code:
#include <curses.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winuser.h>
#define DELAY 300000
int x = 3, y = 20;
int max_y = 0, max_x = 0;
int next_x = 0;
int directionX = -1;
int directionY = 0;
int main(int argc, char *argv)
{
initscr();
noecho();
curs_set(FALSE);
getmaxyx(stdscr, max_y, max_x);
while(1)
{
// drawing on the map
clear();
mvprintw(y, x, "o");
refresh();
usleep(DELAY);
// make next step
next_x = x + directionX;
//set next step directionX
if (next_x >= max_x || next_x < 0)
{
directionX*= -1;
}
else
{
x+= directionX;
}
}
endwin();
}
The issue I cannot fully understand is described on this picture from my debugging:
Please, could somebody explain to me, how is it possible to assign pointer value to -1 and receive +1?
c
I am C programmer beginner and I have encountered something I cannot fully understand to.
Here is my whole code:
#include <curses.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winuser.h>
#define DELAY 300000
int x = 3, y = 20;
int max_y = 0, max_x = 0;
int next_x = 0;
int directionX = -1;
int directionY = 0;
int main(int argc, char *argv)
{
initscr();
noecho();
curs_set(FALSE);
getmaxyx(stdscr, max_y, max_x);
while(1)
{
// drawing on the map
clear();
mvprintw(y, x, "o");
refresh();
usleep(DELAY);
// make next step
next_x = x + directionX;
//set next step directionX
if (next_x >= max_x || next_x < 0)
{
directionX*= -1;
}
else
{
x+= directionX;
}
}
endwin();
}
The issue I cannot fully understand is described on this picture from my debugging:
Please, could somebody explain to me, how is it possible to assign pointer value to -1 and receive +1?
c
c
edited Nov 24 '18 at 23:36
Thomas Dickey
32.4k62861
32.4k62861
asked Nov 24 '18 at 23:21
user8620575user8620575
486
486
6
It not the dereference operator,*
, it's the multiplication operator,*
. Unfortunately, and probably incorrectly, C (the writers) chose to overload it.directionX*= -1;
is the same asdirectionX = directionX * -1
.
– Fiddling Bits
Nov 24 '18 at 23:23
Oh, thank you, this may be the key!
– user8620575
Nov 24 '18 at 23:24
1
@FiddlingBits What's wrong with the*=
operator? It's consistent with all other operators, and unambiguous. In all pointer, contexts the*
goes on the left of the name.
– Jonathon Reinhart
Nov 24 '18 at 23:30
@JonathonReinhart Nothing is wrong with it. I'm only saying it's unfortunate they overloaded the*
operator.
– Fiddling Bits
Nov 24 '18 at 23:54
4
@FiddlingBits: “Overloading” operators or functions usually refers to the operator or function being different based on operand or argument types. The operation here is distinguished by syntax, and that is not usually called “overloading.” If it were, many operators in many languages are overloaded: parentheses (function call, grouping, and casts), commas (argument list and expression sequence),-
(negation and subtraction),.
(structure members and floating-point constants), and more.
– Eric Postpischil
Nov 25 '18 at 0:05
add a comment |
6
It not the dereference operator,*
, it's the multiplication operator,*
. Unfortunately, and probably incorrectly, C (the writers) chose to overload it.directionX*= -1;
is the same asdirectionX = directionX * -1
.
– Fiddling Bits
Nov 24 '18 at 23:23
Oh, thank you, this may be the key!
– user8620575
Nov 24 '18 at 23:24
1
@FiddlingBits What's wrong with the*=
operator? It's consistent with all other operators, and unambiguous. In all pointer, contexts the*
goes on the left of the name.
– Jonathon Reinhart
Nov 24 '18 at 23:30
@JonathonReinhart Nothing is wrong with it. I'm only saying it's unfortunate they overloaded the*
operator.
– Fiddling Bits
Nov 24 '18 at 23:54
4
@FiddlingBits: “Overloading” operators or functions usually refers to the operator or function being different based on operand or argument types. The operation here is distinguished by syntax, and that is not usually called “overloading.” If it were, many operators in many languages are overloaded: parentheses (function call, grouping, and casts), commas (argument list and expression sequence),-
(negation and subtraction),.
(structure members and floating-point constants), and more.
– Eric Postpischil
Nov 25 '18 at 0:05
6
6
It not the dereference operator,
*
, it's the multiplication operator, *
. Unfortunately, and probably incorrectly, C (the writers) chose to overload it. directionX*= -1;
is the same as directionX = directionX * -1
.– Fiddling Bits
Nov 24 '18 at 23:23
It not the dereference operator,
*
, it's the multiplication operator, *
. Unfortunately, and probably incorrectly, C (the writers) chose to overload it. directionX*= -1;
is the same as directionX = directionX * -1
.– Fiddling Bits
Nov 24 '18 at 23:23
Oh, thank you, this may be the key!
– user8620575
Nov 24 '18 at 23:24
Oh, thank you, this may be the key!
– user8620575
Nov 24 '18 at 23:24
1
1
@FiddlingBits What's wrong with the
*=
operator? It's consistent with all other operators, and unambiguous. In all pointer, contexts the *
goes on the left of the name.– Jonathon Reinhart
Nov 24 '18 at 23:30
@FiddlingBits What's wrong with the
*=
operator? It's consistent with all other operators, and unambiguous. In all pointer, contexts the *
goes on the left of the name.– Jonathon Reinhart
Nov 24 '18 at 23:30
@JonathonReinhart Nothing is wrong with it. I'm only saying it's unfortunate they overloaded the
*
operator.– Fiddling Bits
Nov 24 '18 at 23:54
@JonathonReinhart Nothing is wrong with it. I'm only saying it's unfortunate they overloaded the
*
operator.– Fiddling Bits
Nov 24 '18 at 23:54
4
4
@FiddlingBits: “Overloading” operators or functions usually refers to the operator or function being different based on operand or argument types. The operation here is distinguished by syntax, and that is not usually called “overloading.” If it were, many operators in many languages are overloaded: parentheses (function call, grouping, and casts), commas (argument list and expression sequence),
-
(negation and subtraction), .
(structure members and floating-point constants), and more.– Eric Postpischil
Nov 25 '18 at 0:05
@FiddlingBits: “Overloading” operators or functions usually refers to the operator or function being different based on operand or argument types. The operation here is distinguished by syntax, and that is not usually called “overloading.” If it were, many operators in many languages are overloaded: parentheses (function call, grouping, and casts), commas (argument list and expression sequence),
-
(negation and subtraction), .
(structure members and floating-point constants), and more.– Eric Postpischil
Nov 25 '18 at 0:05
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463245%2fpointer-value-modification-behaviour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463245%2fpointer-value-modification-behaviour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
It not the dereference operator,
*
, it's the multiplication operator,*
. Unfortunately, and probably incorrectly, C (the writers) chose to overload it.directionX*= -1;
is the same asdirectionX = directionX * -1
.– Fiddling Bits
Nov 24 '18 at 23:23
Oh, thank you, this may be the key!
– user8620575
Nov 24 '18 at 23:24
1
@FiddlingBits What's wrong with the
*=
operator? It's consistent with all other operators, and unambiguous. In all pointer, contexts the*
goes on the left of the name.– Jonathon Reinhart
Nov 24 '18 at 23:30
@JonathonReinhart Nothing is wrong with it. I'm only saying it's unfortunate they overloaded the
*
operator.– Fiddling Bits
Nov 24 '18 at 23:54
4
@FiddlingBits: “Overloading” operators or functions usually refers to the operator or function being different based on operand or argument types. The operation here is distinguished by syntax, and that is not usually called “overloading.” If it were, many operators in many languages are overloaded: parentheses (function call, grouping, and casts), commas (argument list and expression sequence),
-
(negation and subtraction),.
(structure members and floating-point constants), and more.– Eric Postpischil
Nov 25 '18 at 0:05