tags:

views:

77

answers:

3

If I have those functions:

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;
}

How to call prints function ???

A: 

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;
}
Ed Swangren
Did you mean to flip those functions around?
Justin Ethier
haha yes, and I forgot to add a part about forward declarations. Such a race on SO.
Ed Swangren
I totally would have downvoted my own answer before the third edit...
Ed Swangren
Tnx for your answer but i meant how to call it from main i mean HOW TO CALL it to make it that menu array gets double pointerso what i must put hereprints(**************); // here
ScReYm0
Well, considering that the code would not even compile, perhaps you should have posted some valid code? There are other problems as well, look at the comments.
Ed Swangren
I am looking at them and corrected that void to int
ScReYm0
A: 

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...

Justin Ethier
+4  A: 

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;
}
Amardeep
tnx really tnx for that explanation :)
ScReYm0
I'm glad it was helpful.
Amardeep