Search code examples
cfunctioncoordinatesreturn-valuecoordinate-systems

How to return a co-ordinate structure in C


float generateCoor()
{
    srand(time(NULL));

    coordinate cd; //structure variable
    cd.rx = genRand();
    printf("X-coordinate: %.2f\n",rx);

    cd.ry = genRand();
    printf("Y-coordinate: %.2f\n",ry);
}

Now since one function cannot return two variables simultaneously, than how can I return the co-ordinates back to the main function?


Solution

  • You can use either of following :-

    coordinate generateCoor() { ... return cd;} 
    
    int main(){
    coordinate c = generateCoor();
    }
    

    OR

    void generateCoor(coordinate *cd) { ... }  
    
    //Give it a proper name something like constructCoor 
    
    int main(){
    coordinate c;
    
    generateCoor(&c);
    }