如何修复“赋值使得整数没有强制转换”**更新**

时间:2022-02-26 22:45:35

Okay here's the program I have typed up(stdio.h is included also):

好的,这是我输入的程序(也包括stdio.h):

/* function main begins program execution */
int main (int argc, char *argv[])
{
    int x; /*first number input*/
    int y; /*second number input*/
    int sum; /* variable in which sum will be stored */
    int product; /* variable in which product will be stored */
    int quotient; /* variable in which x divided by y will be stored */
    int md; /* variable in which the modulo division of x divided by y */

    x = argv[2]; /*assign total to x*/
    y = argv[3]; /*assign total to y*/

    if (argc ==3) {
       sum = x + y; /* assign total to sum */
       printf("%d\n",sum); /*print sum*/

       product = x * y; /*assign total to product*/
       printf("%d\n", product); /*print product*/

       quotient = x / y; /*assign total to quotient*/
       printf("%d\n", quotient); /*print quotient*/

       md = x % y; /*assign total to md*/
       printf("%d\n", md); /*print md*/
    } /*end if*/

    if (argc !=3) {
       printf("need two integers\n"); /*need two integers*/
    }

    return 0; /*indicate program ran successfully*/
} /*end of main*/

When I run it through the compiler it says that in lines 15 and 16 (x= and y= lines) "assignment makes integer from pointer without a cast." How do I fix this?

当我通过编译器运行它时,它表示在第15行和第16行(x =和y =行)中“赋值从指针生成整数而没有强制转换。”我该如何解决?

**I changed it to x=atoi(argv[2]) and y=atoi(argv[3]) and it fixed that problem. But as always something else has now screwed up. Now when I run the program I get:

**我将它改为x = atoi(argv [2])和y = atoi(argv [3])并修复了这个问题。但一如既往其他事情已经搞砸了。现在当我运行程序时,我得到:

163 [main] a_4312 _cygtls::handle_exceptions: Error while dumping state

163 [main] a_4312 _cygtls :: handle_exceptions:转储状态时出错

Segmentation fault

I read that this means I didn't allocate memory for the output or something like that...can anyone give me a hand here?**

我读到这意味着我没有为输出分配内存或类似的东西......任何人都可以帮我一把吗?**

5 个解决方案

#1


The compiler error was due to you assigning text to a number. You actually need to call a function to convert between the 2 atoi.

编译器错误是由于您将文本分配给数字。你实际上需要调用一个函数来转换2 atoi。

The most important runtime error is the seg fault, this is caused by using the wrong indexes into the array, c has 0 based arrays so the first element is 0, this means the 2 arguments you want are 1 and 2 (since the first argument is the executable's name).

最重要的运行时错误是seg错误,这是由于在数组中使用了错误的索引,c有0个基于数组,所以第一个元素是0,这意味着你想要的2个参数是1和2(因为第一个参数是可执行文件的名称)。

Finally, you may want to check the y value before the divide and mod since if it's 0 you'll get divide by zero problems.

最后,你可能想要在除法和mod之前检查y值,因为如果它为0,你将得到除以零的问题。

x = atoi(argv[1]); /*assign total to x*/
y = atoi(argv[2]); /*assign total to y*/

#2


The numbers you get passed are argv[1] and argv[2], not argv[2] and argv[3].

你传递的数字是argv [1]和argv [2],而不是argv [2]和argv [3]。

#3


Aside from the provided solution, you should also check whether the required arguments are there (move the last if block to before the assignment to to x and y) before trying to pass them to atoi or you will get run-time issues.

除了提供的解决方案之外,您还应该检查是否存在所需的参数(在分配到x和y之前将最后一个if块移动到之前),然后再尝试将它们传递给atoi,否则您将遇到运行时问题。

#4


argv[2] gives you a char *. You need conversion to an int:

argv [2]给你一个char *。你需要转换为int:

x = atoi(argv[2]);

#5


If your command line is:

如果您的命令行是:

prompt>> myprogram 5 6

提示>> myprogram 5 6

argv[0] should be the program name (myprogram) argv[1] should be the first parameter ("5") argv[2] should be the second parameter ("6")

argv [0]应该是程序名称(myprogram)argv [1]应该是第一个参数(“5”)argv [2]应该是第二个参数(“6”)

I think you're referring to argv[3], which doesn't exist.

我认为你指的是argv [3],它不存在。

#1


The compiler error was due to you assigning text to a number. You actually need to call a function to convert between the 2 atoi.

编译器错误是由于您将文本分配给数字。你实际上需要调用一个函数来转换2 atoi。

The most important runtime error is the seg fault, this is caused by using the wrong indexes into the array, c has 0 based arrays so the first element is 0, this means the 2 arguments you want are 1 and 2 (since the first argument is the executable's name).

最重要的运行时错误是seg错误,这是由于在数组中使用了错误的索引,c有0个基于数组,所以第一个元素是0,这意味着你想要的2个参数是1和2(因为第一个参数是可执行文件的名称)。

Finally, you may want to check the y value before the divide and mod since if it's 0 you'll get divide by zero problems.

最后,你可能想要在除法和mod之前检查y值,因为如果它为0,你将得到除以零的问题。

x = atoi(argv[1]); /*assign total to x*/
y = atoi(argv[2]); /*assign total to y*/

#2


The numbers you get passed are argv[1] and argv[2], not argv[2] and argv[3].

你传递的数字是argv [1]和argv [2],而不是argv [2]和argv [3]。

#3


Aside from the provided solution, you should also check whether the required arguments are there (move the last if block to before the assignment to to x and y) before trying to pass them to atoi or you will get run-time issues.

除了提供的解决方案之外,您还应该检查是否存在所需的参数(在分配到x和y之前将最后一个if块移动到之前),然后再尝试将它们传递给atoi,否则您将遇到运行时问题。

#4


argv[2] gives you a char *. You need conversion to an int:

argv [2]给你一个char *。你需要转换为int:

x = atoi(argv[2]);

#5


If your command line is:

如果您的命令行是:

prompt>> myprogram 5 6

提示>> myprogram 5 6

argv[0] should be the program name (myprogram) argv[1] should be the first parameter ("5") argv[2] should be the second parameter ("6")

argv [0]应该是程序名称(myprogram)argv [1]应该是第一个参数(“5”)argv [2]应该是第二个参数(“6”)

I think you're referring to argv[3], which doesn't exist.

我认为你指的是argv [3],它不存在。