LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-28-2008, 07:37 AM   #1
sarathius
LQ Newbie
 
Registered: Jan 2008
Posts: 12

Rep: Reputation: 0
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
 
Old 01-28-2008, 08:22 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
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.
 
Old 01-29-2008, 11:38 AM   #3
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Matir View Post
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

Last edited by orgcandman; 01-29-2008 at 11:40 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to call a function inside switch case sarathius Programming 4 01-28-2008 06:36 AM
calling a function from within another function in php jayakrishnan Programming 2 06-19-2007 09:36 AM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 04:15 PM
switch statements calling a function tekmorph Programming 2 10-19-2004 06:53 PM
calling a c++ binary inside a perl script Blue_muppet Programming 3 08-29-2004 12:31 AM


All times are GMT -5. The time now is 09:55 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration