C 将类型转换为a(双*)?

C 将类型转换为a(双*)?,c,C,我试图通过一个函数分配内存,并在内存中存储正确的数据类型,但是如果没有(*int),我的int无法工作,我如何将其作为double应用?(*double)不起作用 #include <stdio.h> #include <stdlib.h> int newInt (); double newDouble (); int main() { int userChoice; int userValue

我试图通过一个函数分配内存,并在内存中存储正确的数据类型,但是如果没有(*int),我的int无法工作,我如何将其作为double应用?(*double)不起作用

    #include <stdio.h>
    #include <stdlib.h>

    int newInt ();
    double newDouble ();

    int main() {

        int userChoice;
        int userValue;
        int *intPtr;
        double *doublePtr;


printf("Please choose which type of value you'd like to store (1-4).");
puts("\n 1.) Int \n 2.) Double \n");
scanf("%d", &userChoice);

if (userChoice == 1) {

    intPtr = (int*)newInt();

    printf("You chose an Int, please enter an Int value\n");
    scanf("%d", *&intPtr); 

    printf("Memory value is %p \n", intPtr);
    printf("Stored value is %d", *intPtr);

} else if (userChoice == 2) {

    doublePtr = (double*)newDouble();

    printf("You chose a Double, please enter a Double value\n");
    scanf("%lf", *&doublePtr);

    printf("Memory value is %p \n", doublePtr);
    printf("Stored value is %lf", *doublePtr);
     return 0;
    }

    int newInt () {  
        malloc(sizeof(int));  
    }

    double newDouble () {
        malloc(sizeof(double));
    }
#包括
#包括
int newInt();
double newDouble();
int main(){
int用户选择;
int用户值;
int*intPtr;
双*双PTR;
printf(“请选择要存储的值类型(1-4)。”;
放置(“\n 1.)Int\n 2.)Double\n”);
scanf(“%d”、&userChoice);
if(userChoice==1){
intPtr=(int*)newInt();
printf(“您选择了一个Int,请输入一个Int值\n”);
scanf(“%d”、*&intPtr);
printf(“内存值为%p\n”,intPtr);
printf(“存储值为%d”,*intPtr);
}else if(userChoice==2){
doublePtr=(double*)newDouble();
printf(“您选择了一个双精度值,请输入一个双精度值\n”);
扫描频率(“%lf”、*&doublePtr);
printf(“内存值为%p\n”,doublePtr);
printf(“存储值为%lf”,*doublePtr);
返回0;
}
int newInt(){
malloc(sizeof(int)),;
}
double newDouble(){
malloc(sizeof)(double);;
}
我把它修好了。
评论中的更正

#包括
#包括
int*newInt();//更改了返回类型
double*newDouble();//更改了返回类型
int main(){
int用户选择;
//int userValue;删除未使用的变量。
int*intPtr;
双*双PTR;
printf(“请选择要存储的值类型(1-4)。”;
放置(“\n 1.)Int\n 2.)Double\n”);
scanf(“%d”、&userChoice);
if(userChoice==1){
intPtr=newInt();//已删除强制转换
printf(“您选择了一个Int,请输入一个Int值\n”);
scanf(“%d”,intPtr);//额外删除*&
printf(“内存值为%p\n”,intPtr);
printf(“存储值为%d”,*intPtr);
}else if(userChoice==2){
doublePtr=newDouble();//已删除强制转换
printf(“您选择了一个双精度值,请输入一个双精度值\n”);
scanf(“%lf”,doublePtr);//额外删除*&
printf(“内存值为%p\n”,doublePtr);
printf(“存储值为%lf”,*doublePtr);
}
返回0;
}//添加了缺少的括号。
int*newInt(){//更改了返回类型
return malloc(sizeof(int));//添加了一个return语句
}
double*newDouble(){//更改了返回类型
return malloc(sizeof(double));//添加了一个return语句
}

不要发布代码的图像。发布我们可以编译、调试和运行的实际代码。可能您希望
返回malloc…
Re“,但如果没有
(*int)
”,我的int无法工作,错误的修复方法。修复方法不是强制转换结果;修复方法是正确定义函数返回的值的类型。它返回
int*
(一旦添加缺少的
返回值
),而不是
int
。打开编译器警告。它会为您识别许多问题。重新“打开编译器警告”。这是肯定的。对于
gcc
,我使用
-Wall-Wextra-pedantic
注意声明和定义应该是
foo(void)
(无参数),而不是
foo()
(参数数量未明)。我只是想澄清一下,以便我理解,这些函数是否返回内存地址?@abelenky--
//在%d之后添加了一个空格
:在
scanf()中使用尾随空格字符几乎总是一个错误
格式化字符串。输入将被阻止,直到输入一些非空白字符。。请注意,
%d
指令无论如何都会自动跳过前导空白字符(除了
%c
%[]
%n
之外的所有其他
scanf()
指令也是如此)因此,除了干扰用户输入之外,它似乎没有任何作用。另外:当使用
%p
打印时,您需要将指针投射到
void*
,以避免未定义的行为。
#include <stdio.h>
#include <stdlib.h>

int* newInt ();       // Changed the return type
double* newDouble (); // Changed the return type

int main() {
    int userChoice;
    // int userValue; Removing unused variable.
    int *intPtr;
    double *doublePtr;

    printf("Please choose which type of value you'd like to store (1-4).");
    puts("\n 1.) Int \n 2.) Double \n");
    scanf("%d", &userChoice);

    if (userChoice == 1) {
        intPtr = newInt(); // Removed cast

        printf("You chose an Int, please enter an Int value\n");
        scanf("%d", intPtr); // Removed extra *&

        printf("Memory value is %p \n", intPtr);
        printf("Stored value is %d", *intPtr);

    } else if (userChoice == 2) {

        doublePtr = newDouble(); // Removed cast

        printf("You chose a Double, please enter a Double value\n");
        scanf("%lf", doublePtr); // Removed extra *&

        printf("Memory value is %p \n", doublePtr);
        printf("Stored value is %lf", *doublePtr);
    }
    return 0;
} // Added a missing bracket.

int* newInt () {                  // Changed the return type
    return malloc(sizeof(int));   // Added a return statement
}

double* newDouble () {             // Changed the return type
    return malloc(sizeof(double)); // Added a return statement
}