LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A question about local variables in C... (https://www.linuxquestions.org/questions/programming-9/a-question-about-local-variables-in-c-920622/)

trist007 12-26-2011 05:40 PM

A question about local variables in C...
 
So I run main where I get input via command line. I convert the input to an int with atoi(). Now, is there any way to convert that local variable in main to a global variable so that other functions besides main can access it?

Any tricks for this? Or do I have to bounce around the variable as a pointer from one external function to the next?

trist007 12-26-2011 06:25 PM

I suppose I can make a global pointer, and then have the global pointer point to the local variable after it gets inputed.

MTK358 12-26-2011 06:46 PM

I have no idea what you are talking about, it's so simple:

Code:

int my_global_int;

int main(int argc, char **argv)
{
    my_global_int = atoi(argv[1]);
   
    // do stuff...

    return 0;
}

Unless I didn't understand what you want.

trist007 12-26-2011 06:52 PM

Hehe, I see now. Thanks a ton. Yea that's pretty bad.

trist007 12-26-2011 07:04 PM

Now what if I wanted to have a global integer array but I don't know the number of elements until input in main is made.
Code:

int archive_global[];

int main(int argc, char **argv)
{
      int m;
      m = atoi(argv[1]);
      archive_global[m];

    return 0;
}

This works but it's a little sloppy. Is there a better way?

MTK358 12-26-2011 07:29 PM

Quote:

Originally Posted by trist007 (Post 4558895)
Now what if I wanted to have a global integer array but I don't know the number of elements until input in main is made.
Code:

int archive_global[];

int main(int argc, char **argv)
{
      int m;
      m = atoi(argv[1]);
      archive_global[m];

    return 0;
}

This works but it's a little sloppy. Is there a better way?

It won't work, you're creating a local variable in main() called "archive_global". The only nice way to do this is malloc():

Code:

int *archive_global;
size_t archive_global_count; // if the array's size can vary, it's probably important that the other functions know its size.

int main(int argc, char **argv)
{
    archive_global_count = atoi(argv[1]);
    archive_global = malloc(archive_global_count * sizeof(int));

    // do stuff

    free(archive_global);
    return 0;
}


trist007 12-26-2011 07:57 PM

Awesome, thank you.

trist007 12-26-2011 08:08 PM

I got one more. So say instead of an integer array, it would be an array of pthreads. The same thing would apply? Even though it has a special data type? What if it were type void, still the same?

pthread_t *tid;

int main(void) {
int i;
m = atoi(argv[2]);

tid = malloc(m * sizeof(pthread_t));
for(i = 0; i < m; i++) {
pthread_create(&tid[i], NULL, threadprocess, NULL);
}
// do stuff

for(i = 0; i < m; i++) {
pthread_join(tid[i], NULL)
}

free(tid);
return 0;
}

MTK358 12-26-2011 08:31 PM

Quote:

Originally Posted by trist007 (Post 4558931)
I got one more. So say instead of an integer array, it would be an array of pthreads. The same thing would apply? Even though it has a special data type? What if it were type void, still the same?

Yes, it should be fine.

Also, use code tags to post code. It's much more readable.


All times are GMT -5. The time now is 01:42 PM.