简体   繁体   English

在C中将指针转换为int

[英]Casting Pointers to int in c

I have to learn it for my study. 我必须学习才能学习。 Is their any way to cast a pointer to integer. 是他们将指针转换为整数的任何方式。 I have to give myEulerForward1 a pointer as a paramter and i always get this error message : 我必须给myEulerForward1一个指针作为参数,而我总是收到此错误消息:

eulerZahl.c: In function ‘main’:
eulerZahl.c:38:35: warning: passing argument 1 of ‘myEulerForward1’ makes pointer from integer without a cast [-Wint-conversion]
  double forward = myEulerForward1(k);
                                   ^
eulerZahl.c:16:8: note: expected ‘int *’ but argument is of type ‘int’
 double myEulerForward1(int *n1){
        ^~~~~~~~~~~~~~~

Can someone help me with it? 有人可以帮我吗?

#include <stdio.h>
#include <stdint.h>


double kFactorial(int k){


    if(k <= 1){
        return 1;
    }

    return k * kFactorial(k - 1);
}


double myEulerForward1(int *n1){


    double n = 1;   
    double euler, nFact = 1;
    for(int i = sizeof(&n1); i >= 0; i--){
        nFact*= i;
        euler = euler + (1.0/nFact);
    }

    return euler;

}


int main(){

    int k = 4;
    double factorial = kFactorial(k);

    printf("The factorial of %d is: %lf ", k, factorial); 

    double forward = myEulerForward1(k);

    printf("The Eulers Number: %lf", forward);
}

First i can see an error at sizeof(&n1); 首先,我可以在sizeof(&n1);处看到一个错误sizeof(&n1); Of course n1 is a pointeur. 当然,n1是指针。 His Value is a RAM address. 他的值是一个RAM地址。 But for see his Deferenced value, you must use * before n1. 但是要查看他的Deferenced值,必须在n1之前使用* & is used for get the adress of something. &用于获取地址。 * is used for get the inside of a pointer. *用于获取指针的内部。

Use : sizeof(*n1); 使用: sizeof(*n1);

In Second, i see that you get a pointer in the prototype of myEulerForward1 在第二部分中,我看到您在myEulerForward1的原型中获得了一个指针

double myEulerForward1(int *n1)

It's your compile error. 这是您的编译错误。 He said that your function need a pointer (an adress) and that you put everything except that. 他说您的函数需要一个指针(地址),除此以外,您都放置了其他所有内容。

So when you call this function, you must put a pointer (a RAM adress). 因此,当调用此函数时,必须放置一个指针(RAM地址)。 And for do that, in the calling of the function, you must use & of course for get the adress of n1 and not his number value. 为此,在调用函数时,您必须使用&当然获得n1的地址而不是其数值。

Use : double forward = myEulerForward1(&k); 用法: double forward = myEulerForward1(&k);

Well, yes, it is possible to convert a pointer to an int , and vice versa. 是的,可以将指针转换为int ,反之亦然。

However, you are making a serious mistake in asking that question. 但是,您在问这个问题时犯了一个严重的错误。 There are circumstances where converting an int to a pointer, or vice versa, makes sense. 在某些情况下,将int转换为指针,反之亦然。 But, in your code, you would be using it as a blunt instrument to force the compiler to accept bad code. 但是,在您的代码中,您将把它用作强制编译器接受错误代码的钝器。

Your compiler is complaining because you have passed an int to a function that expects an int * . 您的编译器在抱怨,因为您已将int传递给需要int *的函数。

Forcing the issue, by converting that int to a pointer, will stop the compiler complaining, but then you'll (possibly) get some form of runtime error, since the function will receive an invalid pointer. 通过将int转换为指针来强制问题,将停止编译器的抱怨,但是(可能)您将获得某种形式的运行时错误,因为该函数将收到无效的指针。

Your choices are 您的选择是

  • remove the * from double myEulerForward1(int *n1) . double myEulerForward1(int *n1)删除* This will mean, the function expects an int , so your code that passes an int will be correct. 这意味着,该函数需要一个int ,因此传递int代码将是正确的。
  • Call the function as myEulerForward1(&k) which passes an address of k (which is not k converted to a pointer) as a pointer. 调用函数myEulerForward1(&k) ,该函数将地址k (不是k转换为指针)传递为指针。

Looking at the body of myEulerForward1() there are other problems as well. 查看myEulerForward1()的主体,还有其他问题。 You need to read up and better understand what sizeof does. 您需要阅读并更好地了解sizeof作用。 Whether your function accepts an int or a pointer ( int * ) the logic of your function is faulty. 无论您的函数接受int还是指针( int * ),函数的逻辑都是错误的。

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

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