0
static void retrieveData(const char* filename)
{
    FILE *f;
    char s[256];
    int a[10];
    int b[10];
    char c[10][10];
    ...
    .....
    long j[10];

    sprintf(s, "some code %s", filename);
    if ((f = popen(s, "r")) != NULL) {
        while (fgets(s, sizeof(s), f)) {
            if (strncmp(s, "A........ {
                sscanf (s,"........
                a[0] = var1;
                b[0] = var2;
                c[0] = var3;
                ...
                .....
            }
            else if (strncmp(s, "B........ {
                sscanf (s,".........
                a[1] = var4;
                b[1] = var5;
                c[1] = var6;
                ...
                .....
            }
            else if .........more codes
        }
        pclose(f);
    }
}

I would like to get all data in arrays a,b,c....,j.

void getData(int argc, char **argv)
{
    int n;
    int a[10];
    int b[10];
    char c[10][10];
    ...
    .....
    long j[10];

    retrieveData("filename1");

    for (n = 0; n < 10; ++n) {
        printf("%d\n", a[n]);
    }

    for (n = 0; n < 10; ++n) {
        printf("%d\n", b[n]);
    }

    ...... more codes


    retrieveData("filename2");

    for (n = 0; n < 10; ++n) {
        printf("%d\n", a[n]);
    }

    for (n = 0; n < 10; ++n) {
        printf("%d\n", b[n]);
    }

    ...... more codes

}

filename1 and filename2 contain same data structure but difference values. Please someone show me how... I'm compiling under linux c. sorry for my poor english.

1 Answer 1

1

You probably need to define them as global variables, and not local, then you can access them in any function.

Put this at the top of the .cpp file that contains both functions.

int a[10];
int b[10];
char c[10][10];

And please use better names :)

Alternatively, since in your case the one function calls the other, you can pass the arrays in as pointers as arguments to the function, but that will be painful in the case of the 2D pointers atleast.

A third, syntactically simpler, option is to wrap the 3 arrays within a struct and pass a pointer to that struct around, thanks Basile Starynkevitch.

typedef struct{
    int a[10];
    int b[10];
    char c[10][10];
} Arrays;

void getData(int argc, char **argv)
{
    ...
    Arrays a;
    retrieveData("...",&a);
    ...
}

void retrieveData(const char* filename, Arrays *a){
    a->b[1] = 1;
    ....
}
Sign up to request clarification or add additional context in comments.

5 Comments

You could also pack these fields inside a struct, and pass the address of that struct as an argument.
@BasileStarynkevitch very true! added
Thanks Basile and Karthik for your suggestion. Can you show me an example using struct or if can pls reedit my code. Im still learning.
@user1996004 pls see my answer for a brief example.
Thank you very much Karthik. Your solution works as i wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.