Exforsys
+ Reply to Thread
Results 1 to 4 of 4

Store value in a global variable

This is a discussion on Store value in a global variable within the C and C++ forums, part of the Programming Talk category; [SIZE="1"]Dear Programmer, i have such a problem and need suggestion and Help from You. ^_^ i'm writing a program in ...

  1. #1
    Ketsuki is offline Junior Member Array
    Join Date
    May 2007
    Answers
    3

    Store value in a global variable

    [SIZE="1"]Dear Programmer,

    i have such a problem and need suggestion and Help from You. ^_^
    i'm writing a program in C. it seems like this.

    /*define the global variable*/
    float A[9][20];
    float B[9][20];
    float C[9][20];
    int s,c;
    float readfile();

    /*Function to read the input data file*/
    float readfile()
    {
    FILE *fp1, *fp2, *fp3;

    if ((fp1 = fopen("udf_inp_X0000.dat","r")) == NULL ){
    printf("Error opening file\n");
    exit(0);}

    if ((fp2 = fopen("udf_inp_X0025.dat","r")) == NULL ){
    printf("Error opening file\n");
    exit(0);}

    if ((fp3 = fopen("udf_inp_X0050.dat","r")) == NULL ){
    printf("Error opening file\n");
    exit(0);}

    for(r = 0 ; s < 9 ; r++){
    for(c = 0 ; c < 20 ; c++){
    fscanf(fp1, "%f", &A[s][c]);
    fscanf(fp2, "%f", &B[s][c]);
    fscanf(fp3, "%f", &C[s][c]); }
    }
    fclose(fp1);fclose(fp2);fclose(fp3);
    return A[s][c], B[s][c], C[s][c];
    }

    main()
    {

    /* in this main function the Array (A,B,C) will be called and used*/

    }


    The problem beginn here. i.e. in the main function i will use the value of A,B,C returned by the function.
    Question: is that possible to return the value of A,B,C to the global variable, so that i don't need to call the function readfile to the main-function every i run the program. (this Program will be used by another program often)
    My idea is to store the value of A,B and C in memory or register, so that once this program is runned, we just need to run the readfunction one time and the value of A,B,c will remain constant and used by the main function several time.

    can somebody give me a suggestion ???

    Thanks in Advance

    best regards
    Ketsuki


  2. #2
    cyrus is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    128
    It is not advisable to return the value of A, B, C to the global variable. Rather than that you can use pointers concept and return the value as pointer. You can then access the memory location using pointers defined wherever needed in your program. By this approach you would have stored the value of A, B and C in memory and also the value of A, B, C will remain constant and used wherever needed in your program.


  3. #3
    Ketsuki is offline Junior Member Array
    Join Date
    May 2007
    Answers
    3
    Hello Cyrus,

    thanks first for Your Reply.
    as you know, i'm still a beginner in Programming, so that i'm not really know how to use the pointer and cann't therefore understand, what you meaned.
    Cau You plz give me some sample or modify my code, so that i can better understand it.

    thanks in advanced.

    kind regards
    Ketsuki


  4. #4
    Ketsuki is offline Junior Member Array
    Join Date
    May 2007
    Answers
    3

    can somebody help me plz. i really need help.

    i have tried to figur out the suggestion from cyrus

    so that i defined some pointer like:

    float *p_A, *p_B, *p_C;

    and then in the "readfile-function", i initialize the pointers like:

    p_A = A;
    p_B = B;
    p_C = C;

    and at the end of the function, i return the value to the pointers.

    return *p_A++, *p_B++, *p_C++;

    but when i compiled it, i got the warning Message: assignment from incompatible pointer type. It seems that i have made some mistake in the initialisation of pointers, but i don't know how to fix it.

    can somebody help, plz


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...