LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2017, 10:24 AM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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;
}
gets seg fault
Code:
userx@slackwhere⚡~/bin/C-files $./a.out 3 4 5                                   
Segmentation fault
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?
It should return 3 4 5 not ; = ?

what is wrong with me?
 
Old 04-13-2017, 11:06 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927
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.

Here is a quick snippet.

Code:
  char e;
 
  e=argv[1][0];

  if ( isdigit{e) )
     printf("Is True");
  else
     printf("Is False");
Code:
char e;
  
 e='A';
 printf("e=%c and e=%d\n",e,e);

Last edited by michaelk; 04-13-2017 at 11:16 AM.
 
1 members found this post helpful.
Old 04-13-2017, 11:15 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by michaelk View Post
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.

Here is a quick snippet.

Code:
  char e;
 
  e=argv[1][0];

  if ( isdigit{e) )
     printf("Is True");
  else
     printf("Is False");

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.
reference:
https://linux.die.net/man/3/isdigit

Last edited by BW-userx; 04-13-2017 at 11:22 AM.
 
Old 04-13-2017, 11:21 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927
That works too... I was trying to show a few code examples...
 
Old 04-13-2017, 11:25 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
so this
Code:
 e=argv[1][0];

then the first element is what is on the command line, and the second element is the place keeper it?

Code:
programName 7 8
argv[programName][0]
argv[7][1]
argv[8][2]
that would be how it is stored then. correct?
 
Old 04-13-2017, 11:34 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927
argv[1][0] = The first character of string argv[1].
 
Old 04-13-2017, 11:37 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by michaelk View Post
argv[1][0] = The first character of string argv[1].
thanks!

Last edited by BW-userx; 04-13-2017 at 12:22 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] int main(int argc, char **argv[]) having trouble getting value out of array argv[ ] C programming. BW-userx Programming 57 04-25-2017 07:18 PM
C programming linux copying argv to wchar_t argv babbab Programming 1 08-09-2012 05:32 AM
seg fault / mem fault - in cron, but works in shell? kauaikat Red Hat 1 04-29-2008 04:24 PM
seg fault / mem fault - in cron, but works in shell? kauaikat Linux - Software 1 04-29-2008 08:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration