[SOLVED] C programming isdigit(argv[i]) is getting a seg fault .. why?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C programming isdigit(argv[i]) is getting a seg fault .. why?
[please take your time and read the text first then look at the code.]
I am just pecking at this one peice at a time. right now I am trying to figure out how to get a line of digits off of the command line - check first to be sure they are digits then store them perhaps in an array. (the last part I have not gotten to yet.)
I have tyied this two seperate ways. which neither are working. the first is off of a example on how to use isdigit(data) which gives me an seg fault.
Code:
/* get char numbers off command line
*
* Apr 13 2017
*
* ***/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main( int argc, char *argv[]) {
int i=1;
int d = 10;
for ( i = 1 ; i < argc ; i++ ) {
// if ( isdigit(atoi(argv[i])) ) {
if (isdigit(argv[i])) {
printf("argc is %d, i = %d and is a number%d\n",argc, i, argv[i]);
}
else
{
printf("argc is %d - i is %d is not a number%c\n", argc, i, argv[i]);
}
}
return 0;
}
then changed it to the upper commented out line so please look again. Because I do not bleive that I have to convert char to int using atoi(data)
Code:
/* get char numbers off command line
*
* Apr 13 2017
*
* ***/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main( int argc, char *argv[]) {
int i=1;
int d = 10;
for ( i = 1 ; i < argc ; i++ ) {
if ( isdigit(atoi(argv[i])) ) {
//if (isdigit(argv[i])) {
printf("argc is %d, i = %d and is a number%d\n",argc, i, argv[i]);
}
else
{
printf("argc is %d - i is %d is not a number%c\n", argc, i, argv[i]);
}
}
return 0;
}
but it still is not even getting what is on the command line. Instead I get something completely different.
Code:
userx@slackwhere⚡~/bin/C-files $./a.out 3 4 5
argc is 4 - i is 1 is not a number;
argc is 4 - i is 2 is not a number=
argc is 4 - i is 3 is not a number?
as argc is the total count of whatever is on the command line.
argv[ ] is the array it gets put into.
looping trough it starting at 1 (one) should return the value within the array. yes or no?
example writen as I type
Code:
for ( int i = 1; i < argc ; i++ ) {
printf("this is argc %d, this is i %d, and this is what is on the command line%c\n",argc, i, argv[i]);
}
Now let me go test that first.
that gets the same thing
Code:
userx@slackwhere⚡~/bin/C-files $./a.out 3 4 5
this is argc 4, this is i 1, and this is what is on the command line;
this is argc 4, this is i 2, and this is what is on the command line=
this is argc 4, this is i 3, and this is what is on the command line?
Although you are entering a number argv is still a character string.
Even though, isdigit() takes integer as an argument, character is passed to the function. Internally, the character is converted to its ASCII value for the check. argv is a character string.
Although you are entering a number argv is still a character string.
Even though, isdigit() takes integer as an argument, character is passed to the function. Internally, the character is converted to its ASCII value for the check. argv is a character string.
I just got it (just now)
by changing it argument adding the symbol for a pointer * to the equation it now works.
Code:
/* get char numbers off command line
*
* Apr 13 2017
*
* ***/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main( int argc, char *argv[]) {
int i=1;
int d = 10;
for ( i = 1 ; i < argc ; i++ ) {
// here is added the pointer reference to argv[ ]
if (isdigit(*argv[i]) )
{
printf("argc is %d, i = %d and argv[i] is a number: %d\n",argc, i, atoi(argv[i]));
}
else
{
printf("argc is %d - i is %d argv[i] is not a number: %s\n", argc, i, argv[i]);
}
}
return 0;
}
now it returns properly
Code:
userx@slackwhere⚡~/bin/C-files $./a.out 4 goop
argc is 3, i = 1 and argv[i] is a number: 4
argc is 3 - i is 2 argv[i] is not a number: goop
so isdigit() takes anything and checks to see if it is a digit type char - that is the purpose of it.
Quote:
isdigit()
checks for a digit (0 through 9).
and isalpha()
Quote:
isalpha()
checks for an alphabetic character; in the standard "C" locale, it is equivalent to (isupper(c) ||
islower(c)).
In some locales, there may be additional characters for which isalpha() is true-letters which are
neither upper case nor lower case.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.