Newbie question

C

c_learner

goose said:
Now see that it doesn't happen again :)
/* Thanks */
/* coded by c_learner, released under GPL v2
(http://www.gnu.org/copyleft/gpl.html) */
#include <stdlib.h>
#define NUM 5

int main(void)
{
int i = 5;

Since you have #define NUM, why not use NUM here? When the
array size changes to 7, you will then only have to change
the #define, and not hunt down all the places where you used
the magic number 5.
ah!
char a[NUM+1] = "12345";
int b[5] = {0};

while(--i > -1)
{
b = atoi(&a);


Once again, you've used the dreaded atoi(). Why not
change this as suggested above by MM?


i think i did it.
a='\0';
}

return 0;
}


/* Thanks */
/* coded by c_learner, released under GPL v2
(http://www.gnu.org/copyleft/gpl.html) */

#include <stdlib.h>
#define NUM 5

int main(void)
{
int i=NUM;
char a[NUM+1] ="12345";
int b[NUM] = {0};

while(--i > -1)
{
b = atoi(&a);
a='\0';
}

return 0;
}
 
M

Michael Mair

c_learner said:
goose wrote:

Now see that it doesn't happen again :)
/* Thanks */
/* coded by c_learner, released under GPL v2
(http://www.gnu.org/copyleft/gpl.html) */
#include <stdlib.h>
#define NUM 5

int main(void)
{
int i = 5;

Since you have #define NUM, why not use NUM here? When the
array size changes to 7, you will then only have to change
the #define, and not hunt down all the places where you used
the magic number 5.

ah!

char a[NUM+1] = "12345";
int b[5] = {0};

while(--i > -1)
{
b = atoi(&a);


Once again, you've used the dreaded atoi(). Why not
change this as suggested above by MM?



i think i did it.


Nope. You took up my suggestion about getting rid of c --
which, ironically, was the least important and most
peril-ridden part...

atoi() does not give you a chance for error checking.
I.e.
- use strtol() if you want to stick with your approach
- use isdigit() from <ctype.h> to check whether you really
have a digit before either
- using atoi() (unnecessarily); or
- using b = a - '0'


Cheers
Michael
a='\0';
}

return 0;
}



/* Thanks */
/* coded by c_learner, released under GPL v2
(http://www.gnu.org/copyleft/gpl.html) */

#include <stdlib.h>
#define NUM 5

int main(void)
{
int i=NUM;
char a[NUM+1] ="12345";
int b[NUM] = {0};

while(--i > -1)
{
b = atoi(&a);
a='\0';
}

return 0;
}
 
C

c_learner

Michael said:
Nope. You took up my suggestion about getting rid of c --
which, ironically, was the least important and most
peril-ridden part...

atoi() does not give you a chance for error checking.
E-Mail: Mine is an /at/ gmx /dot/ de address.

error checking, it's easy.

/* coded by c_learner, released under GPL v2
(http://www.gnu.org/copyleft/gpl.html) */
#include <stdio.h>
#include <stdlib.h>
#define NUM 5

int main(void)
{
int i=NUM;
char a[NUM+1] ="12345";
int b[NUM] = {0};

while(--i > -1)
{
if(a<'0' || a>'9')
{
perror("Invalid number!\n");
exit(1);
}
b = atoi(&a);
a='\0';
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top