Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将字符串(作为用户的输入)传递给c中的函数? #包括 无效添加(内部a、内部b) { INTC=a+b; printf(“\nSum=%d”,c); } void hello(char*name) { printf(“你好%s”,*名称); } int main() { INTA,b; 字符名[20]; 无效(*ptr)(整数,整数)=&add; void(*hello)(char*)=你好; printf(“输入您的姓名:”); scanf(“%s”、&name); 您好(&name); printf(“输入两个值\n”); scanf(“%d%d”、&a和&b); ptr(a,b); 返回0; }_C - Fatal编程技术网

如何将字符串(作为用户的输入)传递给c中的函数? #包括 无效添加(内部a、内部b) { INTC=a+b; printf(“\nSum=%d”,c); } void hello(char*name) { printf(“你好%s”,*名称); } int main() { INTA,b; 字符名[20]; 无效(*ptr)(整数,整数)=&add; void(*hello)(char*)=你好; printf(“输入您的姓名:”); scanf(“%s”、&name); 您好(&name); printf(“输入两个值\n”); scanf(“%d%d”、&a和&b); ptr(a,b); 返回0; }

如何将字符串(作为用户的输入)传递给c中的函数? #包括 无效添加(内部a、内部b) { INTC=a+b; printf(“\nSum=%d”,c); } void hello(char*name) { printf(“你好%s”,*名称); } int main() { INTA,b; 字符名[20]; 无效(*ptr)(整数,整数)=&add; void(*hello)(char*)=你好; printf(“输入您的姓名:”); scanf(“%s”、&name); 您好(&name); printf(“输入两个值\n”); scanf(“%d%d”、&a和&b); ptr(a,b); 返回0; },c,C,我想从用户那里获取输入,然后将其传递给函数,但我无法这样做 以下是我的编译器显示为错误的内容:您不需要访问数组地址,当您将其传递给函数(同时传递给scanf和hello)时,它将隐式转换为char* 我看不到函数指针的使用,因此为了简化代码,我将重写如下: #include<stdio.h> void add(int a,int b) { int c=a+b; printf("\nSum=%d",c); } void hello(char *nam

我想从用户那里获取输入,然后将其传递给函数,但我无法这样做


以下是我的编译器显示为错误的内容:

您不需要访问数组地址,当您将其传递给函数(同时传递给
scanf
hello
)时,它将隐式转换为
char*

我看不到函数指针的使用,因此为了简化代码,我将重写如下:

#include<stdio.h>
void add(int a,int b)
{
    int c=a+b;
    printf("\nSum=%d",c);
}
void hello(char *name)
{
    printf("Hello %s",*name);
}
int main()
{
    int  a,b;
    char name[20];
    void (*ptr)(int,int)=&add;
    void (*hello)(char*)=hello;
    printf("Enter your Name:");
    scanf("%s",&name);
    hello(&name);
    printf("Enter the two values\n");
    scanf("%d%d",&a,&b);
    ptr(a,b);
    return 0;
}

将编译器错误添加为文本请…A)您声明名称
hello
void(*hello)(char*)
,然后将其赋值为
hello
。B) 您正在引用的
name
已经是c-string类型(
char*
),只需将其传递给
scanf
/
printf
。C) 您很可能希望打印字符串,而不仅仅是函数
hello
中的第一个字符。是否有任何特定的原因使其他琐碎的代码与所有这些函数指针复杂化?@gkhaos 20 16[Error]无法将参数中的'char(*)[20]'转换为'char*'passing@HarryK. 是的,我被要求使用指针函数求解我知道这一点,但我要求使用指针函数来求解
#include <stdio.h>

void add(int a, int b)
{
    printf("Sum = %d\n", a + b);
}

void hello(char *name)
{
    printf("Hello %s\n", name);
}

int main()
{
    int a = 0, b = 0;
    char name[20];

    printf("Enter your Name:\n");
    scanf("%s", name);
    hello(name);

    printf("Enter the two values\n");
    scanf("%d%d", &a ,&b);
    add(a, b);
    
    return 0;
}
int main()
{
    int  a = 0, b = 0;
    char name[20];

    void (*add_ptr)(int, int) = &add;
    void (*hello_ptr)(char *) = &hello;

    printf("Enter your Name:\n");
    scanf("%s", name);
    hello_ptr(name);

    printf("Enter the two values\n");
    scanf("%d%d", &a, &b);
    add_ptr(a, b);

    return 0;
}