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? |
I suppose I can make a global pointer, and then have the global pointer point to the local variable after it gets inputed.
|
I have no idea what you are talking about, it's so simple:
Code:
int my_global_int; |
Hehe, I see now. Thanks a ton. Yea that's pretty bad.
|
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[]; |
Quote:
Code:
int *archive_global; |
Awesome, thank you.
|
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; } |
Quote:
Also, use code tags to post code. It's much more readable. |
All times are GMT -5. The time now is 01:42 PM. |