LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   not calling function inside switch case??? (https://www.linuxquestions.org/questions/programming-9/not-calling-function-inside-switch-case-616837/)

sarathius 01-28-2008 06:37 AM

not calling function inside switch case???
 
int add_provider(struct pstentry *pst)

{
int length;
printf("length:");
scanf("%d",pst->length);
return;
}

int main()
{
int i;
printf("1.Add Provider");
printf("2.Add User");
printf("Enter your choice:");
scanf("%d",&i);

switch(i)
{
case 1:
{
int add_provider(struct pstentry *pst);

break;
}

In the above program its not calling the function inside switch case. It is asking for the choice, once we give the value for switch case after that it comes out of program.
please let me know the solution

Matir 01-28-2008 07:22 AM

For one, you have an extra curly brace in the case. Secondly, you need to declare pst first, and then pass it to the function. You can't do both at once.

orgcandman 01-29-2008 10:38 AM

Quote:

Originally Posted by Matir (Post 3037537)
For one, you have an extra curly brace in the case.

There are legitimate reasons for structuring one's code thusly.
Example:

Code:

  switch(foo)
  {
      case BAR:
      {
          int i = 0;
          for(;i < 10; i++;)
            printf("INDEX HERE!\n");
      }
      break;
      case BAZ:
      {
          float d = 1.0;
          printf("%f = a floating point number\n", d);
      }
      break;
      default:
          printf("ugh..\n");
  }

Of course, this will only apply to C++, and not C. However, using functors within a case statement as part of a block is some good.

EDIT: For C, the braces just serve as a good way of denoting the beginning and end of the case block. It's a formatting thing.

Quote:

Secondly, you need to declare pst first, and then pass it to the function. You can't do both at once.
Actually, there's a few things wrong. The OP is not calling a function. Just prototyping one. Further, OP doesn't handle cases where pst is null (something very important to handle).

A correct call to the function would look like:
Code:

  result = add_provider(&provider_object);
OR
Code:

  add_provider(&provider_object);
OR
Code:

  add_provider(NULL);
Where you could replace the arguments given with an actual object of the appropriate type.

-Aaron


All times are GMT -5. The time now is 06:55 PM.