Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 27th, 2005, 2:25 AM   #1
mguarin
Newbie
 
Join Date: Oct 2005
Posts: 18
Rep Power: 0 mguarin is on a distinguished road
STACK- acessing argc and argv outside main()

good day!

can anyone show me how access data of argc and argv if outside main(int argc, char *argv[])?

thnx u very much..
mguarin is offline   Reply With Quote
Old Oct 27th, 2005, 5:00 AM   #2
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
pass them as parameters to 'outside of main'.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Oct 27th, 2005, 5:03 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
....or copy them somewhere so they can be accessed globally .....
grumpy is offline   Reply With Quote
Old Oct 27th, 2005, 5:10 AM   #4
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
merci Monsieur grumpy

my answer was unacceptably incomplete as it stood.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Oct 27th, 2005, 11:24 AM   #5
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
#include <stdlib.h>
char **argvptr=NULL;
int    *argcptr=NULL;

int main(int argc, char **argv)
{
     argcptr=&argc;
     argvptr=argv;
     /* now you can reference them as argcptr and argvptr in any function */
     return 0;
}
jim mcnamara is offline   Reply With Quote
Old Oct 28th, 2005, 7:12 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
I fail to see the point of storing a pointer to argc rather than just its value.
grumpy is offline   Reply With Quote
Old Oct 28th, 2005, 8:40 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Mguarin, are you the type that ignores things you hear that you don't like? This looks suspiciously like part of your other thread. Stack access is implicit in most implementations of the language. It isn't something you do directly or need to do directly. That fact hasn't changed since I posted it, like it or not.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2005, 10:53 AM   #8
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
Quote:
Originally Posted by grumpy
I fail to see the point of storing a pointer to argc rather than just its value.
The whole concept is not a good one. OP wants direct access to the "stack", only he doesn't understand how stacks work in C.

What I think he wants is access to arc and argv everywhere, which sounds like a design problem to me. Probably he doesn't know about getopt() or whatever passes for function that in Windows.
jim mcnamara is offline   Reply With Quote
Old Oct 28th, 2005, 11:18 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Actually, it begins like this:
Quote:
and is it possible to access content of int argc and char * argv[] to other function like sample blue(int argc, char *argv[])?
Even using the word, "stack", in my reply, was obviously a bad call on my part.
Quote:
Argc and argv are values pushed onto the stack by the entry code when main is called. There is nothing magic about them or their names, it's a convention. Putting those labels as arguments for another function will not make them magically appear there. When you write the call to the second function, the compiler will require of you that you supply arguments with the call, just as always.

argv is an array of character pointers. Use atoi on the values pointed to by the contents of each element, just as you would with any other array of the same type. Bear in mind that the thangys to be converted need to be validly convertible.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2005, 9:38 PM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Oh ..... Okaaay.

I hadn't picked up that this was a continuation of another thread.

The "stack" is an implementation detail associated with a compiler and the machine you're running on. It is also a term used sometimes by C programmers to refer to the place where memory for local variables exist (as opposed to the "heap" or something similar, which is the place where dynamically memory is accessed from). The history of the terms "stack" and "heap" are historical: on some machines a decade or so ago, stack and heap were physically implemented as different banks of memory chips. Dawei's reference to the stack for passing arguments to functions dates back to that type of machine: compilers passed arguments to functions by copying their values to the stack.

With modern compilers, operating systems and computer hardware, the distinction between "stack" and "heap" is meaningless - physically, there is rarely any distinction made between different types of memory, even if programmers use antiquated terms like "heap" and "stack" to understand differences between mechanisms to access memory.

Trying to access the "stack" in any way is meaningless --- particularly on machines that don't have a stack. Even if you can, them mechanisms are highly specific to compiler and operating system.

With access to arguments of main(int argc, char **argv), there is no point in trying to access the "stack" in which the arguments (argc and argv) exist. The compiler takes care of that for you, and every compiler does it differently. With modern compilers, there may even be no stack to access. If you want to access those values, the way you do it is to access the arguments within main, and copy their values elsewhere, as described by stevengs and myself (with an example of sorts by jim). The C standard requires compilers to provide you the ability to access command line arguments like this, but leaves it up to compiler vendors as to how they implement it.

And, if you are somehow looking for a means of intercepting argc and argv before a program sees them --- forget it. If you had a genuine and useful reason for doing that, you would already know how. The ONLY reasons I can think of to want to do such things are malicious and childish (eg writing a virus).
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC