LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2011, 05:40 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
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?
 
Old 12-26-2011, 06:25 PM   #2
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
I suppose I can make a global pointer, and then have the global pointer point to the local variable after it gets inputed.
 
Old 12-26-2011, 06:46 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
1 members found this post helpful.
Old 12-26-2011, 06:52 PM   #4
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Hehe, I see now. Thanks a ton. Yea that's pretty bad.
 
Old 12-26-2011, 07:04 PM   #5
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
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?

Last edited by trist007; 12-26-2011 at 07:10 PM.
 
Old 12-26-2011, 07:29 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by trist007 View Post
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;
}
 
Old 12-26-2011, 07:57 PM   #7
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Awesome, thank you.
 
Old 12-26-2011, 08:08 PM   #8
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
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;
}

Last edited by trist007; 12-26-2011 at 08:09 PM.
 
Old 12-26-2011, 08:31 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by trist007 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Local variables in GNU C. stf92 Programming 3 08-18-2011 12:27 PM
[SOLVED] Local Variables anomaly!! man s Programming 4 03-22-2010 01:22 AM
virtual address of local variables dale_chip Programming 1 06-28-2007 10:56 AM
Garbage Collection in C : Local variables question duryodhan Programming 13 12-04-2006 07:16 AM
Local vs Global variables wujee Programming 1 03-11-2005 11:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration