Thread: How to use local variables in other functions

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    2

    How to use local variables in other functions

    So I am new to C programming but was tasked to create a program that reads data from a file and then prints it out.

    One of the sub-tasks is to calculate distance between 2 points through the haversine formula.

    Now here is where my question comes in. How can I use some local variables defined from one of my functions in another function? If I can work this out, I will be able to do most of the rest of my task so I am really hoping there is a way to do this. I have tried working this out for the last day and have gotten no where.

    I have included the code here. I will highlight the variables I want to use between functions. If it isn't immediately obvious, I am hoping to keep token[1] and token[2].


    Code:
    int main() {    int i = 0;
        double readfile, stage2;
        readfile = read_file(i);
        stage2 = stage_2_distance(i);
        printf("%f %f", readfile, stage2);
        
        return 0;
    }
    
    
    double read_file(double i) {
        /* This function takes the data from the input file,reading and printing the 
        User ID, Location (longitude and latitude), Date, Time, and Distance*/
        char length[256];
        char *token[6];
        double r_lat, r_long;
    
    
        if (fgets(length, 256, stdin) != NULL) {
            token[0] = strtok(length, " ");
            int i = 0;
            double dist = 0;
            for (i = 1; i < 6; i++)
                token[i] = strtok(NULL, " "); /*C programming is fun*/
                printf("Stage 1\n==========\n");
                printf("User: #%s\n", token[0]); 
                printf("Location: <%s, %s>\n", token[1], token[2]); /* token [1] and token [2] are the variables I want to use elsewhere. */
                printf("Date: %s\n", token[3]);
                printf("Time: %s\n", token[4]);
                printf("Distance to reference: %.2f\n", distance(dist));
                r_lat = *token[1]; /* These next two lines are what I have been playing around with*/
                r_long = *token[2];
        }
        else {
            printf("Error opening file. Check file and try again.");
            exit(EXIT_FAILURE);
        }
        exit(EXIT_SUCCESS);
    }
    
    
    double distance(double d) {
        /* This function is designed to calculate the distance between the check-in 
        POI and the reference point provided*/
        double angle_distance, chord_length;
        double lat_2, long_2;
        int i = 0;
        
        char length[256];
        char *token[6];
        
        token[0] = strtok(length, " ");
        token[i] = strtok(NULL, " ");
        double dist;
        
        lat_2 = atof(token[3]);
        long_2 = atof(token[4]);
        
        double var_lat = toRadian(lat_2 - LAT_1);
        double var_long = toRadian(long_2 - LONG_1);
     
        double nLAT_1 = toRadian(LAT_1);
        double nLat_2 = toRadian(lat_2);
            
        chord_length = pow(sin(var_lat/2),2) + (cos(nLAT_1) * cos(nLat_2) * 
        pow(sin(var_long/2),2));
        
        angle_distance = 2 * atan2(sqrt(chord_length), sqrt(1 - chord_length)); 
        
        dist = 6371 * angle_distance;
        
        return dist;
    }
    
    
    double toRadian(double x) {
        x = PI/DEGREES;
        return x;
    }
    Last edited by HassSugma; 09-21-2019 at 01:02 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In your case, I'd suggest reading from the file into an array of location struct objects, each of which consists of two doubles (i.e., the longitude and latitude). You can then pass this array to the read function along with the array size so that the read function reads into this array rather than an array local to the function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2019
    Posts
    2
    I'm only very barely familiar with the struct function and am not sure how I would go about reading the file into it. How would I do that and what would I be looking to have the struct function look like?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A struct is not a function; it's a way of grouping data such that you can refer to disparate pieces of data (the members) as a single object. This makes it both easier to work with the data (since it is grouped together) and to reason about it (since you name the struct type and the object, thereby conferring meaning to the data).

    What you're doing right now with read_file is to mostly read, print, then discard the data. What I'm suggesting is to come up with a struct (or two) to represent the data read, and from there you can figure out how to get the data out of the function instead of discarding it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays between functions, dependent on local variables
    By usernamer in forum C Programming
    Replies: 3
    Last Post: 11-24-2012, 06:11 PM
  2. Replies: 6
    Last Post: 12-02-2009, 08:47 AM
  3. Local vs Global Variables
    By BENCHMARKMAN in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2007, 05:17 AM
  4. local variables
    By Gil22 in forum C++ Programming
    Replies: 14
    Last Post: 04-09-2003, 09:13 PM
  5. local variables
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 03-20-2002, 02:55 PM

Tags for this Thread