开发者

Problem with calling a function?

If I have those functions:

void main(void)
{
    char *menu[] = {"data", "coming", "here"};

    prints(**************); // here

    printf("\n");

}




void prin开发者_开发技巧ts(char **menu)
{
    int a;
    while(*menu)
    {
        printf("%s", **menu);
        menu ++;
    }

    a = 0;
}

How to call prints function ???


Here is a version with several issues fixed:

#include <stdio.h>

// declare function before using it
void prints(char **menu)
{
    // make sure parameter is valid
    if (menu != NULL)
    {
        while(*menu)
        {
            // spaces so they don't run together
            // pass *menu not **menu to printf
            printf("%s  ", *menu);
            menu ++;
        }
    }
}

// proper return type for main()
int main(void)
{
    // array terminator added
    char *menu[] = {"data", "coming", "here", NULL};

    prints(menu); // here

    printf("\n");

    // return with no error
    return 0;
}


In C you must declare your function before another function that uses it. So...

void prints(char **menu)
{
    int a;
    while(*menu)
    {
        printf("%s", **menu);
        menu ++;
    }

    a = 0;
}

void main(void)
{
    char *menu[] = {"data", "coming", "here"};
    prints(**************); // here 
    printf("\n");
}

That, or you can forward declare the function:

void prints(char **menu);

void main(void)
{
    char *menu[] = {"data", "coming", "here"};
    prints(**************); // here 
    printf("\n");
}

void prints(char **menu)
{
    int a;
    while(*menu)
    {
        printf("%s", **menu);
        menu ++;
    }

    a = 0;
}


You can either move the prints function above main, or you can put a prototype for prints above main, like so:

void prints(char **menu);

Then, you can call prints from main just like any other function...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜