0

I am reading What is indirect expansion? What does ${!var*} mean? about variable indirection in bash (the general case, not exceptional case).

I am more familiar with C than bash.

Is there a corresponding concept in C for "variable indirection" in bash?

Or what is the closet concept in C to "variable indirection" in bash?

Community
  • 1
  • 1
Tim
  • 1
  • 141
  • 372
  • 590
  • Can you please stick to one language at a time please? Python and C and C++ are all different enough. – Martijn Pieters Dec 05 '15 at 18:21
  • The question you linked to talks about specific exceptions to indirect expansion. Did you want to know about equivalents to those exceptions, or to the original concept of indirect expansion? You are basically asking about *introspection* here, the ability to access names in the code. – Martijn Pieters Dec 05 '15 at 18:23
  • Thanks, @MartijnPieters. I change to limit to C at first. But I am afraid if I can ask the same question again except for a different langauge. I am asking about the general case, not exceptional case. – Tim Dec 05 '15 at 18:25
  • I don't think C has such a concept; it doesn't have introspection capabilities at least. – Martijn Pieters Dec 05 '15 at 18:27
  • Does C++ or Python have? In any of the languages, is the name of a variable served as the address of the variable? – Tim Dec 05 '15 at 18:27
  • Python has. `globals()` and `locals()` give you access to namespaces, reflected as dictionaries. `globals()['foo']` gives you the value of the global variable `foo`, for example. – Martijn Pieters Dec 05 '15 at 18:30
  • can you define what untrospection is? – Tim Dec 05 '15 at 19:18
  • @Tim: I improved my answer to give more references. Some languages are able to query something about their own call stack (reflection, introspection, ...) – Basile Starynkevitch Dec 05 '15 at 19:22
  • 2
    _"I am asking about the general case"_ That's a little suspect. Which practical problem could you possibly have that requires knowing this (whatever "this" is) for multiple languages? – Lightness Races in Orbit Dec 05 '15 at 19:30
  • @MartijnPieters: "introspection" is the ability of a program to examine the type or properties of an object at runtime (https://en.wikipedia.org/wiki/Type_introspection), while "variable indirection" in bash is to reference the value of a variable using the value of another variable which is the name of the first variable. I don't find them related. Is "variable indirection" in bash an example of "introspection"? Or how are they related? – Tim Dec 06 '15 at 14:38
  • @Tim: I always mix up [reflection](https://en.wikipedia.org/wiki/Reflection_(computer_programming)) and introspection, because in Python they are often one and the same. You can do what Bash does in Python using reflection; you look up the value associated with a 'variable variable'. – Martijn Pieters Dec 06 '15 at 14:46
  • @MartijnPieters: Thanks. Could you recommend some general programming language books for concepts including reflection, introspection, ...? – Tim Dec 07 '15 at 04:55
  • In the broadest terms, pointers in C are sort of like indirect variables in Bash. A regular `int` has a value; a pointer to an `int` tells you where you can find the `int` value you want. A named variable in Bash has a value; an indirect variable reference tells you the name of the variable which contains the value you want. – tripleee Dec 07 '15 at 07:50

1 Answers1

2

Your question is unclear, and the concept of variable (and of environment) is very different in C, in bash, and in Scheme or Python or Javascript.

You might want to become familiar with some Lisp or Scheme (e.g. read SICP) then read Queinnec's Lisp In Small Pieces (explaining in great details about Lisp interpreters & compilers). Read also Scott's Programming Languages Pragmatics. Then you'll get a broader view of the notion of variable (and you might study the operational semantics and/or the denotational semantics of some programming language).

First, C is a low-level language. It has pointers, pointer arithmetic, address-of operator (unary &). Its variables exist only at compile time (at runtime, there are mostly locations in the store -e.g. in heap or on the call stack or in global data segment). Look into ACSL for describing incompletely some specification of the behavior of some C functions.

On Linux, you could get the address of a global variable or function from its name using dlsym(3), and you might get the name of a global variable or function from its address using dladdr(3)

Read also about homoiconic languages (which C is not) and reflection (which C has not) and type introspection (which C has not; but see RTTI in C++).

Some programming languages (but not C) are expliciting & reifying their environments (mapping variables to values) and their continuations. In standard C, the call stack is not inspectable or introspectable (and that would be useful for garbage collection). See GNU libc backtraces functions (they cannot be written in portable C). Scheme has call/cc.

Notice that a language with introspectable call stack could be compiled into C or into C++ (see my MELT DSL as an example), essentially by reifying each call frame into some local struct ....

Read also about the halting problem and Rice's theorem. There is a intrinsic limitation in what optimizing compilers or static program analyzers can achieve (copied from this other answer of mine). See also J.Pitrat's blog and books about metaknowledge and reflexive/introspective Artificial Intelligence systems.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks. How are variable indirection in my post and "introspection" related? – Tim Dec 06 '15 at 06:56
  • Because your question is unclear, since the notion of variable and of environment vary *widely* between different languages. – Basile Starynkevitch Dec 06 '15 at 06:57
  • Thanks. My question was: is "variable indirection" in bash "introspection"? Or how are they related? "introspection" is the ability of a program to examine the type or properties of an object at runtime, while "variable indirection" in bash is to reference the value of a variable using the value of another variable which is the name of the first variable. – Tim Dec 06 '15 at 14:35