字符串作为函数指针的参数

时间:2019-01-24 11:55:30

标签: c

下面的程序在for循环的第二次迭代中未接受输入。 请给我一些解决方案。

下面的程序具有函数指针,其中字符串是函数指针的输入。

/ *******代码******* /

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size (100)

int GetName(char *str_Name)
{
    printf("\n Name of the Employee :%s",str_Name);
    return strlen(str_Name);
}

int GetPlaceName(char *str_Name)
{
    printf("\n Name of the Place :%s",str_Name);
    return strlen(str_Name);
}

int GetStateName(char *str_Name)
{
    printf("\n Name of the State :%s",str_Name);
    return strlen(str_Name);
}

int main()
{
    char Names[size] = {'\0'},*Ptr_Names;
    int rtn_len,i;
    int (*char_fun_ptr[])(char *str) = {GetName,GetPlaceName,GetStateName};
    printf("\n First Enter 'Your Name', Then Enter your 'Place Name' and then Enter your 'State Name'");
    for(i = 0; i < 3; i++)
    {
        printf("\n Enter the Name : ");
        scanf("%[^\n]s",Names);
        Ptr_Names = Names;
        rtn_len = char_fun_ptr[i](Ptr_Names);
        printf("\n Length is :%d",strlen(Names));
        memset(Names,0,size*sizeof(char));
    }
    getch();
    return 0;
}

0 个答案:

没有答案