![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 18
Rep Power: 0
![]() |
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.. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
pass them as parameters to 'outside of main'.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
....or copy them somewhere so they can be accessed globally .....
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
merci Monsieur grumpy
![]() my answer was unacceptably incomplete as it stood.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
#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;
} |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
I fail to see the point of storing a pointer to argc rather than just its value.
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#9 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Actually, it begins like this:
Quote:
Quote:
__________________
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 |
||
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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). |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|