简体   繁体   English

将字符串转换为 integer 的问题

[英]Problem with converting string to integer

(I am very new to both coding and C.) (我对编码和 C 都很陌生。)

I want to check if the nr one string in "string argv" is one or more decimals and if it is convert it to an integer.我想检查“string argv”中的第一个字符串是否是一个或多个小数,以及是否将其转换为 integer。 I think I may need to iterate over the string but so far this code is not working.我想我可能需要遍历字符串,但到目前为止这段代码不起作用。 It says segmentation fault.它说分段错误。

This is my code:这是我的代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>

int main (int argc, string argv [])
{

    //check command line
    if (argc == 2)
    {
        printf("success");
    }
    else
    {
        printf("Usage: ./caesar key.");
    }

    //validate number
    if (isdigit (argv[1]))
    {
        int x = atoi(argv[1]);
        printf("I is now %i\n", x);
        return 0;
    }

    if (isalpha(argv[1]))
     {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

main function takes (int argc, char *argv[]) parameters. main function 采用 (int argc, char *argv[]) 参数。 argv is a pointer to pointer and argv[1] is a pointer to a char array too. argv 是指向指针的指针,而 argv[1] 也是指向 char 数组的指针。 You should send a character to isdigit and isalpha , not a pointer.您应该向isdigitisalpha发送一个字符,而不是一个指针。 You get a segmentation fault because of this probably.您可能因此而遇到分段错误。

check if... string... is one or more decimals and if it is convert it to an integer.检查...字符串...是否为一个或多个小数,是否将其转换为 integer。

isdigit (argv[1]) --> argv[1] is a pointer to a string . isdigit (argv[1]) --> argv[1]是一个指向字符串的指针。 isdigit(int ch) expects a single character, not a pointer. isdigit(int ch)需要单个字符,而不是指针。 Enable all compiler warnings for fast feedback.启用所有编译器警告以获得快速反馈。


A direct approach uses strtol()直接方法使用strtol()

bool test_and_convert(const char *s, long *valptr) {
  errno = 0; // set to 0 to later detect overflow
  int base = 10;
  char *endptr;
  long *valptr = strtol(s, &endptr, base);
  if (s == endtpr) {
    return false; // No conversion
  }
  if (errno == ERANGE) {
    return true; /* or false, OP's choice here */
    // *valptr is clamped to LONG_MIN or LONG_MAX
  }

  // Detect trailing non-numeric text if desired
  if (*endptr) {
    return false;
  }

  return true;
}

A plodding approach akin to OP's.一种类似于 OP 的缓慢方法。

bool test_and_convert_plodding(const char *s, int *valptr) {
  // Best to access characters as unsinged char for `is...()` functions.
  const unsigned char *us = (const unsigned char *) s; // 
  
  // Maybe skip leading spaces
  while (isspace(*us)) us++;

  unsigned char sign = *us;
  if (sign == '-' || sign == '+') {
    us++;
  }

  // Accumulate digits
  unsigned found = false;
  unsigned uvalue = 0;
  while (isdigit(*us)) {
    found = true;
    // Overflow detection code needed here is TBD
    uvalue = ulvaue*10 + (*us - '0');
  }

  if (!found) {
    return false; // No digits
  }

  // Maybe skip trailing spaces
  while (isspace(*us)) us++;

  // Maybe detect non-numeric junk at the end.
  if (*us) return false;
  
  // Overflow detection code needed here is TBD
  *valptr = (sign == '-'  && uvlaue > 0) ? (-(int)(uvalue - 1) - 1) : (int) uvalue;
  return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM