1

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

3
  • 2
    prints(menu), but you forgot to add a NULL element at the end of your menu array, since that's what prints needs to know when to break the loop. Also it should be int main(), not void main(void) - and if your C compiler does not complain at the latter, throw it away and find a better one (e.g. gcc). Commented Jul 22, 2010 at 23:48
  • 2
    I'm going to start using void main in the obfuscated C contest -- it automatically blinds people to all other problems Commented Jul 22, 2010 at 23:52
  • Pavel Minaev tnx i will know that in future... i meant how to call it from main i mean HOW TO CALL it to make it that menu array gets double pointer so what i must put here prints(**************); // here Commented Jul 23, 2010 at 0:01

3 Answers 3

4

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

tnx really tnx for that explanation :)
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;
}

4 Comments

Did you mean to flip those functions around?
haha yes, and I forgot to add a part about forward declarations. Such a race on SO.
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 pointer so what i must put here prints(**************); // here
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.
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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.