Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8791011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:47:03+00:00 2026-06-13T22:47:03+00:00

I am very new to c programming. I have written the fallowing code float

  • 0

I am very new to c programming. I have written the fallowing code

 float value;  //golbal variable
 unsigned int data; //golbal variable

 void Maxphase(void) 
 { 
  float  MAX = 0.0;
   unsigned int i,index;
 for (i=0;i<=360;i++) 
 {              
    phaseset(i); 
    data = readvalue(); 
    value = voltage(data);    
    if(value>MAX)   //find max value 
    { 
      MAX = value;    //max voltage 
      index = i;   
     }  
  }                          
    printf("Max Voltage Value:%f\r\n", MAX); 
    printf("Related index Value:%d\r\n", index); 
} 

the above code working perfectly and printing maximum voltage and index. I want return both values “Max” and “index” from this function and I have to save Max value in one variable and index value in other variable like.

void runCom(void){ 
     c=getchar();
 switch(c){ 
    case '1': 
           Maxphase();
          Vin= (I want to store MAX value of that function)
          p1= ( I want to store Index of that function)
          break; 
    default:
         break;
    }
  }

Actually I want call that function and it has to return two variables MAX and index value, thus I want to store those two values in different variables.

I know function can’t return two values.

I have searched, i found it is possible with a struct or make the function to handle the arguments with pointers. I tried with struct as shown below.

typedef struct {
   float v;
   unsigned int p;
  }volphase;

I have declared this struct in header file. I am including this header file in all files where i am calling.

volphase Maxphase()
{
   volphase vp;
float  MAX = 0.0;
   unsigned int i,index;
 for (i=0;i<=360;i++) 
 {              
    phaseset(i); 
    data = readvalue(); 
    value = voltage(data);    
    if(value>MAX)   //find max value 
    { 
      MAX = value;    //max voltage 
      index = i;   
     }  
  }                          
   vp.v=MAX;
   vp.p=index; 
   return vp;
  } 

This is written in “bvr.c” file.
But I am thinking how to call this “struct” in case’1′(main.c) and how to store vp.v in one variable and vp.p in another variable.

Please suggest me if any thing wrong in writing struct. or any other easiest way that will return two values.

please help me how to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:47:04+00:00Added an answer on June 13, 2026 at 10:47 pm

    Returning a struct from the function is the least common of the two ways to return multiple values. Using pointers is more common:

    void Maxphase(float *max, unsigned int *index) 
    {
        *max = 0.0;
        float value;
        unsigned int i, data;
        for (i=0;i<=360;i++) 
        {              
            phaseset(i); 
            data = readvalue(); 
            value = voltage(mux1);    
            if(value > *max)   //find max value 
            { 
                *max = value;    //max voltage 
                *index = i;   
            }  
        }                          
        printf("Max Voltage Value:%f\r\n", *max); 
        printf("Related index Value:%d\r\n", *index); 
    }
    

    Here is how you call this function:

    int main() {
        float max;
        unsigned idx;
        Maxphase(&max, &idx);
        printf("Max Voltage Value:%f\r\n", max); 
        printf("Related index Value:%d\r\n", idx); 
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to python programming and have yet to buy a textbook
I'm very much new to programming and have been doing fairly well so far.
I'm quite new to programming larger applications. I have now written a command line
I am very new to programming in FLEX. I have inherited a FLEX 4
I'm very new to IOS programming and i have doubt with NSThread. My problem
I am very new to iOS Programming and have been learning for only a
I am very new to C Programming and have a doubt... I've been asked
I'm very new to programming in Android, but have been struggling all day with
I'm still very new to programming and I want to write the cleanest code
Still very new to programming... I have 9x1 Vectors at time t, t+1, t+2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.