Cleaning a static char array after use
I want my program to clean the static chars array for a new input I've tried with memset but it doesn't work or i'm doing it wrong. please some advice. Thanks in advance.
const char * password() {
static int i = 0;
static char pwd[STRING_LEN], c = '';
static char return_buffer[255];
memset(pwd, 0, sizeof pwd); //<---
memset(return_buffer, 0, sizeof return_buffer); //<--
printf("Password: ");
while (i < STRING_LEN){
pwd[i] = getch();
c = pwd[i];
if(c == 13) { break; }
i++;
}
pwd[i] = '';
snprintf(return_buffer, sizeof(return_buffer), "%s", pwd);
printf("n");
return return_buffer;
}
c arrays clear memset
|
show 1 more comment
I want my program to clean the static chars array for a new input I've tried with memset but it doesn't work or i'm doing it wrong. please some advice. Thanks in advance.
const char * password() {
static int i = 0;
static char pwd[STRING_LEN], c = '';
static char return_buffer[255];
memset(pwd, 0, sizeof pwd); //<---
memset(return_buffer, 0, sizeof return_buffer); //<--
printf("Password: ");
while (i < STRING_LEN){
pwd[i] = getch();
c = pwd[i];
if(c == 13) { break; }
i++;
}
pwd[i] = '';
snprintf(return_buffer, sizeof(return_buffer), "%s", pwd);
printf("n");
return return_buffer;
}
c arrays clear memset
1
In what way is it not working?
– Christian Gibbons
Nov 12 '18 at 18:02
You'll write the''
outside the array if the user entersSTRING_LEN
characters. You needwhile (i < STRING_LEN - 1)
to leave room for the null.
– Barmar
Nov 12 '18 at 18:05
1
if(c == 13)
looks suspicious.
– Christian Gibbons
Nov 12 '18 at 18:07
That depends on the platform.
– Swordfish
Nov 12 '18 at 18:09
1
13 is not'n'
but'r'
(carriage return). Since you usegetch()
i suspect you are on windows where a "newline" consists of'r'
and'n'
, so checking for'r'
(= 13) is the right(tm) thing to do. Neither,'r'
nor'n'
should result in a crash, though.
– Swordfish
Nov 12 '18 at 19:08
|
show 1 more comment
I want my program to clean the static chars array for a new input I've tried with memset but it doesn't work or i'm doing it wrong. please some advice. Thanks in advance.
const char * password() {
static int i = 0;
static char pwd[STRING_LEN], c = '';
static char return_buffer[255];
memset(pwd, 0, sizeof pwd); //<---
memset(return_buffer, 0, sizeof return_buffer); //<--
printf("Password: ");
while (i < STRING_LEN){
pwd[i] = getch();
c = pwd[i];
if(c == 13) { break; }
i++;
}
pwd[i] = '';
snprintf(return_buffer, sizeof(return_buffer), "%s", pwd);
printf("n");
return return_buffer;
}
c arrays clear memset
I want my program to clean the static chars array for a new input I've tried with memset but it doesn't work or i'm doing it wrong. please some advice. Thanks in advance.
const char * password() {
static int i = 0;
static char pwd[STRING_LEN], c = '';
static char return_buffer[255];
memset(pwd, 0, sizeof pwd); //<---
memset(return_buffer, 0, sizeof return_buffer); //<--
printf("Password: ");
while (i < STRING_LEN){
pwd[i] = getch();
c = pwd[i];
if(c == 13) { break; }
i++;
}
pwd[i] = '';
snprintf(return_buffer, sizeof(return_buffer), "%s", pwd);
printf("n");
return return_buffer;
}
c arrays clear memset
c arrays clear memset
asked Nov 12 '18 at 18:01
Gabriel VieiraGabriel Vieira
2216
2216
1
In what way is it not working?
– Christian Gibbons
Nov 12 '18 at 18:02
You'll write the''
outside the array if the user entersSTRING_LEN
characters. You needwhile (i < STRING_LEN - 1)
to leave room for the null.
– Barmar
Nov 12 '18 at 18:05
1
if(c == 13)
looks suspicious.
– Christian Gibbons
Nov 12 '18 at 18:07
That depends on the platform.
– Swordfish
Nov 12 '18 at 18:09
1
13 is not'n'
but'r'
(carriage return). Since you usegetch()
i suspect you are on windows where a "newline" consists of'r'
and'n'
, so checking for'r'
(= 13) is the right(tm) thing to do. Neither,'r'
nor'n'
should result in a crash, though.
– Swordfish
Nov 12 '18 at 19:08
|
show 1 more comment
1
In what way is it not working?
– Christian Gibbons
Nov 12 '18 at 18:02
You'll write the''
outside the array if the user entersSTRING_LEN
characters. You needwhile (i < STRING_LEN - 1)
to leave room for the null.
– Barmar
Nov 12 '18 at 18:05
1
if(c == 13)
looks suspicious.
– Christian Gibbons
Nov 12 '18 at 18:07
That depends on the platform.
– Swordfish
Nov 12 '18 at 18:09
1
13 is not'n'
but'r'
(carriage return). Since you usegetch()
i suspect you are on windows where a "newline" consists of'r'
and'n'
, so checking for'r'
(= 13) is the right(tm) thing to do. Neither,'r'
nor'n'
should result in a crash, though.
– Swordfish
Nov 12 '18 at 19:08
1
1
In what way is it not working?
– Christian Gibbons
Nov 12 '18 at 18:02
In what way is it not working?
– Christian Gibbons
Nov 12 '18 at 18:02
You'll write the
''
outside the array if the user enters STRING_LEN
characters. You need while (i < STRING_LEN - 1)
to leave room for the null.– Barmar
Nov 12 '18 at 18:05
You'll write the
''
outside the array if the user enters STRING_LEN
characters. You need while (i < STRING_LEN - 1)
to leave room for the null.– Barmar
Nov 12 '18 at 18:05
1
1
if(c == 13)
looks suspicious.– Christian Gibbons
Nov 12 '18 at 18:07
if(c == 13)
looks suspicious.– Christian Gibbons
Nov 12 '18 at 18:07
That depends on the platform.
– Swordfish
Nov 12 '18 at 18:09
That depends on the platform.
– Swordfish
Nov 12 '18 at 18:09
1
1
13 is not
'n'
but 'r'
(carriage return). Since you use getch()
i suspect you are on windows where a "newline" consists of 'r'
and 'n'
, so checking for 'r'
(= 13) is the right(tm) thing to do. Neither, 'r'
nor 'n'
should result in a crash, though.– Swordfish
Nov 12 '18 at 19:08
13 is not
'n'
but 'r'
(carriage return). Since you use getch()
i suspect you are on windows where a "newline" consists of 'r'
and 'n'
, so checking for 'r'
(= 13) is the right(tm) thing to do. Neither, 'r'
nor 'n'
should result in a crash, though.– Swordfish
Nov 12 '18 at 19:08
|
show 1 more comment
1 Answer
1
active
oldest
votes
Besides that there is no need to "clean" the arrays since you are overwriting their content with the result of getch()
and a ''
... you never reset i
.
i
is a static int
that only will get initialized the first time, execution passes its point of definition. Also, there is no need for i
and pwd
to be static variables.
Why does he need to reseti
?
– Barmar
Nov 12 '18 at 18:07
Because he surely doesn't want input being written behind former input at a 2nd, 3rd, ... call to the function.
– Swordfish
Nov 12 '18 at 18:08
3
i
is a static int. Subsequent calls to the function will keep incrementing without resetting it back to 0.
– Christian Gibbons
Nov 12 '18 at 18:08
Yeah, just noticed thestatic
declaration.
– Barmar
Nov 12 '18 at 18:08
The answer should go into more detail about how static variable initialization works. There's also no need fori
andpwd
to be static at all. The only variable that needs to be static isreturn_buffer
, since it's being returned.
– Barmar
Nov 12 '18 at 18:10
|
show 1 more comment
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%2f53267683%2fcleaning-a-static-char-array-after-use%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Besides that there is no need to "clean" the arrays since you are overwriting their content with the result of getch()
and a ''
... you never reset i
.
i
is a static int
that only will get initialized the first time, execution passes its point of definition. Also, there is no need for i
and pwd
to be static variables.
Why does he need to reseti
?
– Barmar
Nov 12 '18 at 18:07
Because he surely doesn't want input being written behind former input at a 2nd, 3rd, ... call to the function.
– Swordfish
Nov 12 '18 at 18:08
3
i
is a static int. Subsequent calls to the function will keep incrementing without resetting it back to 0.
– Christian Gibbons
Nov 12 '18 at 18:08
Yeah, just noticed thestatic
declaration.
– Barmar
Nov 12 '18 at 18:08
The answer should go into more detail about how static variable initialization works. There's also no need fori
andpwd
to be static at all. The only variable that needs to be static isreturn_buffer
, since it's being returned.
– Barmar
Nov 12 '18 at 18:10
|
show 1 more comment
Besides that there is no need to "clean" the arrays since you are overwriting their content with the result of getch()
and a ''
... you never reset i
.
i
is a static int
that only will get initialized the first time, execution passes its point of definition. Also, there is no need for i
and pwd
to be static variables.
Why does he need to reseti
?
– Barmar
Nov 12 '18 at 18:07
Because he surely doesn't want input being written behind former input at a 2nd, 3rd, ... call to the function.
– Swordfish
Nov 12 '18 at 18:08
3
i
is a static int. Subsequent calls to the function will keep incrementing without resetting it back to 0.
– Christian Gibbons
Nov 12 '18 at 18:08
Yeah, just noticed thestatic
declaration.
– Barmar
Nov 12 '18 at 18:08
The answer should go into more detail about how static variable initialization works. There's also no need fori
andpwd
to be static at all. The only variable that needs to be static isreturn_buffer
, since it's being returned.
– Barmar
Nov 12 '18 at 18:10
|
show 1 more comment
Besides that there is no need to "clean" the arrays since you are overwriting their content with the result of getch()
and a ''
... you never reset i
.
i
is a static int
that only will get initialized the first time, execution passes its point of definition. Also, there is no need for i
and pwd
to be static variables.
Besides that there is no need to "clean" the arrays since you are overwriting their content with the result of getch()
and a ''
... you never reset i
.
i
is a static int
that only will get initialized the first time, execution passes its point of definition. Also, there is no need for i
and pwd
to be static variables.
edited Nov 12 '18 at 18:13
answered Nov 12 '18 at 18:05
SwordfishSwordfish
9,06811335
9,06811335
Why does he need to reseti
?
– Barmar
Nov 12 '18 at 18:07
Because he surely doesn't want input being written behind former input at a 2nd, 3rd, ... call to the function.
– Swordfish
Nov 12 '18 at 18:08
3
i
is a static int. Subsequent calls to the function will keep incrementing without resetting it back to 0.
– Christian Gibbons
Nov 12 '18 at 18:08
Yeah, just noticed thestatic
declaration.
– Barmar
Nov 12 '18 at 18:08
The answer should go into more detail about how static variable initialization works. There's also no need fori
andpwd
to be static at all. The only variable that needs to be static isreturn_buffer
, since it's being returned.
– Barmar
Nov 12 '18 at 18:10
|
show 1 more comment
Why does he need to reseti
?
– Barmar
Nov 12 '18 at 18:07
Because he surely doesn't want input being written behind former input at a 2nd, 3rd, ... call to the function.
– Swordfish
Nov 12 '18 at 18:08
3
i
is a static int. Subsequent calls to the function will keep incrementing without resetting it back to 0.
– Christian Gibbons
Nov 12 '18 at 18:08
Yeah, just noticed thestatic
declaration.
– Barmar
Nov 12 '18 at 18:08
The answer should go into more detail about how static variable initialization works. There's also no need fori
andpwd
to be static at all. The only variable that needs to be static isreturn_buffer
, since it's being returned.
– Barmar
Nov 12 '18 at 18:10
Why does he need to reset
i
?– Barmar
Nov 12 '18 at 18:07
Why does he need to reset
i
?– Barmar
Nov 12 '18 at 18:07
Because he surely doesn't want input being written behind former input at a 2nd, 3rd, ... call to the function.
– Swordfish
Nov 12 '18 at 18:08
Because he surely doesn't want input being written behind former input at a 2nd, 3rd, ... call to the function.
– Swordfish
Nov 12 '18 at 18:08
3
3
i
is a static int. Subsequent calls to the function will keep incrementing without resetting it back to 0.– Christian Gibbons
Nov 12 '18 at 18:08
i
is a static int. Subsequent calls to the function will keep incrementing without resetting it back to 0.– Christian Gibbons
Nov 12 '18 at 18:08
Yeah, just noticed the
static
declaration.– Barmar
Nov 12 '18 at 18:08
Yeah, just noticed the
static
declaration.– Barmar
Nov 12 '18 at 18:08
The answer should go into more detail about how static variable initialization works. There's also no need for
i
and pwd
to be static at all. The only variable that needs to be static is return_buffer
, since it's being returned.– Barmar
Nov 12 '18 at 18:10
The answer should go into more detail about how static variable initialization works. There's also no need for
i
and pwd
to be static at all. The only variable that needs to be static is return_buffer
, since it's being returned.– Barmar
Nov 12 '18 at 18:10
|
show 1 more comment
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%2f53267683%2fcleaning-a-static-char-array-after-use%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
1
In what way is it not working?
– Christian Gibbons
Nov 12 '18 at 18:02
You'll write the
''
outside the array if the user entersSTRING_LEN
characters. You needwhile (i < STRING_LEN - 1)
to leave room for the null.– Barmar
Nov 12 '18 at 18:05
1
if(c == 13)
looks suspicious.– Christian Gibbons
Nov 12 '18 at 18:07
That depends on the platform.
– Swordfish
Nov 12 '18 at 18:09
1
13 is not
'n'
but'r'
(carriage return). Since you usegetch()
i suspect you are on windows where a "newline" consists of'r'
and'n'
, so checking for'r'
(= 13) is the right(tm) thing to do. Neither,'r'
nor'n'
should result in a crash, though.– Swordfish
Nov 12 '18 at 19:08