C language determine length of array depending on amount of digits entered by the userHow do I determine the size of my array in C?Improve INSERT-per-second performance of SQLite?Fastest sort of fixed length 6 int arrayC program: Breaking an int into an int array[]Restrict user input to 8 digit numberC program: Checking digits entered are 1s and 0s only using arraysUnexpected behaviour when scanf used inside while loop to read an arrayArray size different from what is entered - CNeed to write a program that asks the user to fill in a 2D array then checks how many times a digit between 0 and 9 was entered in those two rowsUser defined array elements and array size in C
Who must act to prevent Brexit on March 29th?
Can the electrostatic force be infinite in magnitude?
Invariance of results when scaling explanatory variables in logistic regression, is there a proof?
I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?
Word describing multiple paths to the same abstract outcome
No idea how to draw this using tikz
Freedom of speech and where it applies
Why is delta-v is the most useful quantity for planning space travel?
How can a jailer prevent the Forge Cleric's Artisan's Blessing from being used?
Is there an Impartial Brexit Deal comparison site?
How do I rename a LINUX host without needing to reboot for the rename to take effect?
Bob has never been a M before
Books on the History of math research at European universities
What will be the benefits of Brexit?
Reply ‘no position’ while the job posting is still there (‘HiWi’ position in Germany)
How to color a zone in Tikz
Giant Toughroad SLR 2 for 200 miles in two days, will it make it?
In Star Trek IV, why did the Bounty go back to a time when whales were already rare?
How can I raise concerns with a new DM about XP splitting?
Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?
Pronouncing Homer as in modern Greek
For airliners, what prevents wing strikes on landing in bad weather?
A workplace installs custom certificates on personal devices, can this be used to decrypt HTTPS traffic?
Proving by induction of n. Is this correct until this point?
C language determine length of array depending on amount of digits entered by the user
How do I determine the size of my array in C?Improve INSERT-per-second performance of SQLite?Fastest sort of fixed length 6 int arrayC program: Breaking an int into an int array[]Restrict user input to 8 digit numberC program: Checking digits entered are 1s and 0s only using arraysUnexpected behaviour when scanf used inside while loop to read an arrayArray size different from what is entered - CNeed to write a program that asks the user to fill in a 2D array then checks how many times a digit between 0 and 9 was entered in those two rowsUser defined array elements and array size in C
I have the below code in which a user enters their card number, with this each digit is stored as an element in the variable array digits[]
.
What I am trying to achieve is to have the for
loop stop after it has stored all digits, however, I am struggling to set the loop as per the length of the entered card number.
I have tried the below code using sizeof(cardNo)
stored in the variable length and setting length as the loop condition.
Let's say the user enters 1234 the result I am receiving with the below code being run is 43210000, when what I am looking for is just 4321.
if I set int digits
to digits[4]
and the loop condition to < 4 it will give me 4321 but obviously this restricts me to user only entering 4 digits when in fact I want this to be flexible i.e. if they enter 6 digits it will give me 123456 if they enter 8 digits it will give me 12345678 etc.
Any ideas or suggestions, please?
long cardNo = get_long("Enter Card Number: ");
int length = sizeof(cardNo);
int count = 0;
int digits[length];
for (int i = 0; i < length; i++)
digits[i] = cardNo % 10;
cardNo /= 10;
printf("%in", digits[i]);
printf("n");
c
|
show 1 more comment
I have the below code in which a user enters their card number, with this each digit is stored as an element in the variable array digits[]
.
What I am trying to achieve is to have the for
loop stop after it has stored all digits, however, I am struggling to set the loop as per the length of the entered card number.
I have tried the below code using sizeof(cardNo)
stored in the variable length and setting length as the loop condition.
Let's say the user enters 1234 the result I am receiving with the below code being run is 43210000, when what I am looking for is just 4321.
if I set int digits
to digits[4]
and the loop condition to < 4 it will give me 4321 but obviously this restricts me to user only entering 4 digits when in fact I want this to be flexible i.e. if they enter 6 digits it will give me 123456 if they enter 8 digits it will give me 12345678 etc.
Any ideas or suggestions, please?
long cardNo = get_long("Enter Card Number: ");
int length = sizeof(cardNo);
int count = 0;
int digits[length];
for (int i = 0; i < length; i++)
digits[i] = cardNo % 10;
cardNo /= 10;
printf("%in", digits[i]);
printf("n");
c
1
Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.
– Eugene Sh.
Mar 7 at 15:31
First: Use malloc to this allocate dynamically memoery. Second:length
should besize_t
type. I thinkcardNo
too unless you want negative number handle.
– Igor Galczak
Mar 7 at 15:33
4
sizeof(cardNo)
is not what you think it is.
– Jabberwocky
Mar 7 at 15:36
@IgorGalczak It doesn't have to besize_t
.
– StaceyGirl
Mar 7 at 15:38
@StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…
– Igor Galczak
Mar 7 at 15:44
|
show 1 more comment
I have the below code in which a user enters their card number, with this each digit is stored as an element in the variable array digits[]
.
What I am trying to achieve is to have the for
loop stop after it has stored all digits, however, I am struggling to set the loop as per the length of the entered card number.
I have tried the below code using sizeof(cardNo)
stored in the variable length and setting length as the loop condition.
Let's say the user enters 1234 the result I am receiving with the below code being run is 43210000, when what I am looking for is just 4321.
if I set int digits
to digits[4]
and the loop condition to < 4 it will give me 4321 but obviously this restricts me to user only entering 4 digits when in fact I want this to be flexible i.e. if they enter 6 digits it will give me 123456 if they enter 8 digits it will give me 12345678 etc.
Any ideas or suggestions, please?
long cardNo = get_long("Enter Card Number: ");
int length = sizeof(cardNo);
int count = 0;
int digits[length];
for (int i = 0; i < length; i++)
digits[i] = cardNo % 10;
cardNo /= 10;
printf("%in", digits[i]);
printf("n");
c
I have the below code in which a user enters their card number, with this each digit is stored as an element in the variable array digits[]
.
What I am trying to achieve is to have the for
loop stop after it has stored all digits, however, I am struggling to set the loop as per the length of the entered card number.
I have tried the below code using sizeof(cardNo)
stored in the variable length and setting length as the loop condition.
Let's say the user enters 1234 the result I am receiving with the below code being run is 43210000, when what I am looking for is just 4321.
if I set int digits
to digits[4]
and the loop condition to < 4 it will give me 4321 but obviously this restricts me to user only entering 4 digits when in fact I want this to be flexible i.e. if they enter 6 digits it will give me 123456 if they enter 8 digits it will give me 12345678 etc.
Any ideas or suggestions, please?
long cardNo = get_long("Enter Card Number: ");
int length = sizeof(cardNo);
int count = 0;
int digits[length];
for (int i = 0; i < length; i++)
digits[i] = cardNo % 10;
cardNo /= 10;
printf("%in", digits[i]);
printf("n");
c
c
edited Mar 7 at 16:02
static_cast
1,07211521
1,07211521
asked Mar 7 at 15:29
CoderCoder
9911
9911
1
Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.
– Eugene Sh.
Mar 7 at 15:31
First: Use malloc to this allocate dynamically memoery. Second:length
should besize_t
type. I thinkcardNo
too unless you want negative number handle.
– Igor Galczak
Mar 7 at 15:33
4
sizeof(cardNo)
is not what you think it is.
– Jabberwocky
Mar 7 at 15:36
@IgorGalczak It doesn't have to besize_t
.
– StaceyGirl
Mar 7 at 15:38
@StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…
– Igor Galczak
Mar 7 at 15:44
|
show 1 more comment
1
Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.
– Eugene Sh.
Mar 7 at 15:31
First: Use malloc to this allocate dynamically memoery. Second:length
should besize_t
type. I thinkcardNo
too unless you want negative number handle.
– Igor Galczak
Mar 7 at 15:33
4
sizeof(cardNo)
is not what you think it is.
– Jabberwocky
Mar 7 at 15:36
@IgorGalczak It doesn't have to besize_t
.
– StaceyGirl
Mar 7 at 15:38
@StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…
– Igor Galczak
Mar 7 at 15:44
1
1
Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.
– Eugene Sh.
Mar 7 at 15:31
Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.
– Eugene Sh.
Mar 7 at 15:31
First: Use malloc to this allocate dynamically memoery. Second:
length
should be size_t
type. I think cardNo
too unless you want negative number handle.– Igor Galczak
Mar 7 at 15:33
First: Use malloc to this allocate dynamically memoery. Second:
length
should be size_t
type. I think cardNo
too unless you want negative number handle.– Igor Galczak
Mar 7 at 15:33
4
4
sizeof(cardNo)
is not what you think it is.– Jabberwocky
Mar 7 at 15:36
sizeof(cardNo)
is not what you think it is.– Jabberwocky
Mar 7 at 15:36
@IgorGalczak It doesn't have to be
size_t
.– StaceyGirl
Mar 7 at 15:38
@IgorGalczak It doesn't have to be
size_t
.– StaceyGirl
Mar 7 at 15:38
@StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…
– Igor Galczak
Mar 7 at 15:44
@StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…
– Igor Galczak
Mar 7 at 15:44
|
show 1 more comment
3 Answers
3
active
oldest
votes
The length of a number is based on its logarithm base 10:
#include <math.h> //log10
....
int len = log10(cardNo) + 1;
...
Don't forget to link with the math library (gcc ... -lm
)
It's a long not an int. FPU roundoff burns man.
– Joshua
Mar 7 at 15:39
@Joshua: the argumentcardNo
is automatically converted todouble
– pmg
Mar 7 at 15:42
@pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)
– Coder
Mar 7 at 15:44
3
This is what logarithms do.log10(x)
is the number you have to raise 10 to to obtainx
.10^3
(power, not xor) is1000
, solog10(1000)
is 3; I add1
to get 4.log10(4863)
is3.<something>
and it needs 4 digits. The assigment to an integer discards anything after the decimal point.
– pmg
Mar 7 at 15:49
@Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999
– Joshua
Mar 7 at 16:13
|
show 1 more comment
Idiom: measure-allocate-generate.
size_t n =0;
for (; i < cardno; )
cardNo /= 10;
n++;
if(!n)n++;//0
int*digits=malloc(n*sizeof(int));
sizeof(int)
is not a good idea, modifications may yield errors.
– Jose
Mar 7 at 16:34
@Jose: Calls to malloc without*sizeof()
are really bad ideas unlesschar
.
– Joshua
Mar 7 at 16:45
2
I think @Jose meant thatint *digits = malloc(n * sizeof *digits);
will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;
). @Jose: excuse me for butting in :)
– pmg
Mar 7 at 20:53
Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.
– Jose
Mar 8 at 8:11
add a comment |
The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STRINGIFY(x) STRINGIFY2(x)
#define STRINGIFY2(x) #x
#define MAX_NUMBERS 16
int main()
char cardNum[MAX_NUMBERS + 1];
size_t sizeCard;
printf("Enter card numbersn");
scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
sizeCard = strlen(cardNum);
for (int i = 0; i < sizeCard; i++)
printf("%cn", cardNum[i]);
printf("n");
add a 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%2f55047405%2fc-language-determine-length-of-array-depending-on-amount-of-digits-entered-by-th%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The length of a number is based on its logarithm base 10:
#include <math.h> //log10
....
int len = log10(cardNo) + 1;
...
Don't forget to link with the math library (gcc ... -lm
)
It's a long not an int. FPU roundoff burns man.
– Joshua
Mar 7 at 15:39
@Joshua: the argumentcardNo
is automatically converted todouble
– pmg
Mar 7 at 15:42
@pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)
– Coder
Mar 7 at 15:44
3
This is what logarithms do.log10(x)
is the number you have to raise 10 to to obtainx
.10^3
(power, not xor) is1000
, solog10(1000)
is 3; I add1
to get 4.log10(4863)
is3.<something>
and it needs 4 digits. The assigment to an integer discards anything after the decimal point.
– pmg
Mar 7 at 15:49
@Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999
– Joshua
Mar 7 at 16:13
|
show 1 more comment
The length of a number is based on its logarithm base 10:
#include <math.h> //log10
....
int len = log10(cardNo) + 1;
...
Don't forget to link with the math library (gcc ... -lm
)
It's a long not an int. FPU roundoff burns man.
– Joshua
Mar 7 at 15:39
@Joshua: the argumentcardNo
is automatically converted todouble
– pmg
Mar 7 at 15:42
@pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)
– Coder
Mar 7 at 15:44
3
This is what logarithms do.log10(x)
is the number you have to raise 10 to to obtainx
.10^3
(power, not xor) is1000
, solog10(1000)
is 3; I add1
to get 4.log10(4863)
is3.<something>
and it needs 4 digits. The assigment to an integer discards anything after the decimal point.
– pmg
Mar 7 at 15:49
@Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999
– Joshua
Mar 7 at 16:13
|
show 1 more comment
The length of a number is based on its logarithm base 10:
#include <math.h> //log10
....
int len = log10(cardNo) + 1;
...
Don't forget to link with the math library (gcc ... -lm
)
The length of a number is based on its logarithm base 10:
#include <math.h> //log10
....
int len = log10(cardNo) + 1;
...
Don't forget to link with the math library (gcc ... -lm
)
edited Mar 7 at 15:40
answered Mar 7 at 15:35
pmgpmg
84.6k999170
84.6k999170
It's a long not an int. FPU roundoff burns man.
– Joshua
Mar 7 at 15:39
@Joshua: the argumentcardNo
is automatically converted todouble
– pmg
Mar 7 at 15:42
@pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)
– Coder
Mar 7 at 15:44
3
This is what logarithms do.log10(x)
is the number you have to raise 10 to to obtainx
.10^3
(power, not xor) is1000
, solog10(1000)
is 3; I add1
to get 4.log10(4863)
is3.<something>
and it needs 4 digits. The assigment to an integer discards anything after the decimal point.
– pmg
Mar 7 at 15:49
@Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999
– Joshua
Mar 7 at 16:13
|
show 1 more comment
It's a long not an int. FPU roundoff burns man.
– Joshua
Mar 7 at 15:39
@Joshua: the argumentcardNo
is automatically converted todouble
– pmg
Mar 7 at 15:42
@pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)
– Coder
Mar 7 at 15:44
3
This is what logarithms do.log10(x)
is the number you have to raise 10 to to obtainx
.10^3
(power, not xor) is1000
, solog10(1000)
is 3; I add1
to get 4.log10(4863)
is3.<something>
and it needs 4 digits. The assigment to an integer discards anything after the decimal point.
– pmg
Mar 7 at 15:49
@Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999
– Joshua
Mar 7 at 16:13
It's a long not an int. FPU roundoff burns man.
– Joshua
Mar 7 at 15:39
It's a long not an int. FPU roundoff burns man.
– Joshua
Mar 7 at 15:39
@Joshua: the argument
cardNo
is automatically converted to double
– pmg
Mar 7 at 15:42
@Joshua: the argument
cardNo
is automatically converted to double
– pmg
Mar 7 at 15:42
@pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)
– Coder
Mar 7 at 15:44
@pmg this seems to do the trick, thanks. Could you be kind enough to give me a quick explanation behind the code? This way I will know what's going on for next time :)
– Coder
Mar 7 at 15:44
3
3
This is what logarithms do.
log10(x)
is the number you have to raise 10 to to obtain x
. 10^3
(power, not xor) is 1000
, so log10(1000)
is 3; I add 1
to get 4. log10(4863)
is 3.<something>
and it needs 4 digits. The assigment to an integer discards anything after the decimal point.– pmg
Mar 7 at 15:49
This is what logarithms do.
log10(x)
is the number you have to raise 10 to to obtain x
. 10^3
(power, not xor) is 1000
, so log10(1000)
is 3; I add 1
to get 4. log10(4863)
is 3.<something>
and it needs 4 digits. The assigment to an integer discards anything after the decimal point.– pmg
Mar 7 at 15:49
@Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999
– Joshua
Mar 7 at 16:13
@Coder: Fails for 999999999999999, 9999999999999999, 99999999999999999, 999999999999999999
– Joshua
Mar 7 at 16:13
|
show 1 more comment
Idiom: measure-allocate-generate.
size_t n =0;
for (; i < cardno; )
cardNo /= 10;
n++;
if(!n)n++;//0
int*digits=malloc(n*sizeof(int));
sizeof(int)
is not a good idea, modifications may yield errors.
– Jose
Mar 7 at 16:34
@Jose: Calls to malloc without*sizeof()
are really bad ideas unlesschar
.
– Joshua
Mar 7 at 16:45
2
I think @Jose meant thatint *digits = malloc(n * sizeof *digits);
will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;
). @Jose: excuse me for butting in :)
– pmg
Mar 7 at 20:53
Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.
– Jose
Mar 8 at 8:11
add a comment |
Idiom: measure-allocate-generate.
size_t n =0;
for (; i < cardno; )
cardNo /= 10;
n++;
if(!n)n++;//0
int*digits=malloc(n*sizeof(int));
sizeof(int)
is not a good idea, modifications may yield errors.
– Jose
Mar 7 at 16:34
@Jose: Calls to malloc without*sizeof()
are really bad ideas unlesschar
.
– Joshua
Mar 7 at 16:45
2
I think @Jose meant thatint *digits = malloc(n * sizeof *digits);
will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;
). @Jose: excuse me for butting in :)
– pmg
Mar 7 at 20:53
Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.
– Jose
Mar 8 at 8:11
add a comment |
Idiom: measure-allocate-generate.
size_t n =0;
for (; i < cardno; )
cardNo /= 10;
n++;
if(!n)n++;//0
int*digits=malloc(n*sizeof(int));
Idiom: measure-allocate-generate.
size_t n =0;
for (; i < cardno; )
cardNo /= 10;
n++;
if(!n)n++;//0
int*digits=malloc(n*sizeof(int));
edited Mar 7 at 16:16
answered Mar 7 at 15:38
JoshuaJoshua
24.2k550103
24.2k550103
sizeof(int)
is not a good idea, modifications may yield errors.
– Jose
Mar 7 at 16:34
@Jose: Calls to malloc without*sizeof()
are really bad ideas unlesschar
.
– Joshua
Mar 7 at 16:45
2
I think @Jose meant thatint *digits = malloc(n * sizeof *digits);
will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;
). @Jose: excuse me for butting in :)
– pmg
Mar 7 at 20:53
Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.
– Jose
Mar 8 at 8:11
add a comment |
sizeof(int)
is not a good idea, modifications may yield errors.
– Jose
Mar 7 at 16:34
@Jose: Calls to malloc without*sizeof()
are really bad ideas unlesschar
.
– Joshua
Mar 7 at 16:45
2
I think @Jose meant thatint *digits = malloc(n * sizeof *digits);
will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;
). @Jose: excuse me for butting in :)
– pmg
Mar 7 at 20:53
Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.
– Jose
Mar 8 at 8:11
sizeof(int)
is not a good idea, modifications may yield errors.– Jose
Mar 7 at 16:34
sizeof(int)
is not a good idea, modifications may yield errors.– Jose
Mar 7 at 16:34
@Jose: Calls to malloc without
*sizeof()
are really bad ideas unless char
.– Joshua
Mar 7 at 16:45
@Jose: Calls to malloc without
*sizeof()
are really bad ideas unless char
.– Joshua
Mar 7 at 16:45
2
2
I think @Jose meant that
int *digits = malloc(n * sizeof *digits);
will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;
). @Jose: excuse me for butting in :)– pmg
Mar 7 at 20:53
I think @Jose meant that
int *digits = malloc(n * sizeof *digits);
will make it easier to avoid errors in case digits change type (struct baseddigit *digits = malloc(n * sizeof (int)) /* error here */;
). @Jose: excuse me for butting in :)– pmg
Mar 7 at 20:53
Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.
– Jose
Mar 8 at 8:11
Yes, exactly @pmg. Thank you for your explanation, definitely better than mine.
– Jose
Mar 8 at 8:11
add a comment |
The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STRINGIFY(x) STRINGIFY2(x)
#define STRINGIFY2(x) #x
#define MAX_NUMBERS 16
int main()
char cardNum[MAX_NUMBERS + 1];
size_t sizeCard;
printf("Enter card numbersn");
scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
sizeCard = strlen(cardNum);
for (int i = 0; i < sizeCard; i++)
printf("%cn", cardNum[i]);
printf("n");
add a comment |
The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STRINGIFY(x) STRINGIFY2(x)
#define STRINGIFY2(x) #x
#define MAX_NUMBERS 16
int main()
char cardNum[MAX_NUMBERS + 1];
size_t sizeCard;
printf("Enter card numbersn");
scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
sizeCard = strlen(cardNum);
for (int i = 0; i < sizeCard; i++)
printf("%cn", cardNum[i]);
printf("n");
add a comment |
The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STRINGIFY(x) STRINGIFY2(x)
#define STRINGIFY2(x) #x
#define MAX_NUMBERS 16
int main()
char cardNum[MAX_NUMBERS + 1];
size_t sizeCard;
printf("Enter card numbersn");
scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
sizeCard = strlen(cardNum);
for (int i = 0; i < sizeCard; i++)
printf("%cn", cardNum[i]);
printf("n");
The code may be simpler, considering a maximum amount of numbers (being developer's choice to store the fixed-size container on the stack, as static, etc):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STRINGIFY(x) STRINGIFY2(x)
#define STRINGIFY2(x) #x
#define MAX_NUMBERS 16
int main()
char cardNum[MAX_NUMBERS + 1];
size_t sizeCard;
printf("Enter card numbersn");
scanf("%" STRINGIFY(MAX_NUMBERS) "s", cardNum);
sizeCard = strlen(cardNum);
for (int i = 0; i < sizeCard; i++)
printf("%cn", cardNum[i]);
printf("n");
edited Mar 8 at 8:20
answered Mar 7 at 15:53
JoseJose
1,291516
1,291516
add a comment |
add a 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%2f55047405%2fc-language-determine-length-of-array-depending-on-amount-of-digits-entered-by-th%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
Allocate the maximum. And again, as in every question for this exercise (and there are hundresds around) I suggest to use string representation of the number rather than numerical value, unless there are strict requirements.
– Eugene Sh.
Mar 7 at 15:31
First: Use malloc to this allocate dynamically memoery. Second:
length
should besize_t
type. I thinkcardNo
too unless you want negative number handle.– Igor Galczak
Mar 7 at 15:33
4
sizeof(cardNo)
is not what you think it is.– Jabberwocky
Mar 7 at 15:36
@IgorGalczak It doesn't have to be
size_t
.– StaceyGirl
Mar 7 at 15:38
@StaceyGirl I write should be because of: wiki.sei.cmu.edu/confluence/display/c/…
– Igor Galczak
Mar 7 at 15:44