How give array name from string variable in C [duplicate] - c

This question already has answers here:
Variable with char[] name
(3 answers)
Closed 8 years ago.
I am stuck with a situation where I need to give array name from string variable.
Basically I want to create an array with same name as value in another string variable "name":
char *name="arr_name";
In my case the string being hold by variable name may change. Hence advice accroding.
Thanks!

I think you're looking for some mechanism, similar to the ones found in the higher level languages as in python (introspection), or C# (reflections). C doesn't provide this kind of insight from the runtime, not even the variable names are existing in the bytecode - so basically it's not possible and doesn't make any sense in the terms of the way how C works.
I don't know if that helps, but one thing you could do is to statically (so in the compilation time, not while it's running!) populate char* and create variable with the same name, given that the value of the string is a proper name for the variable (Naming convention for C/C++). You can achieve that by defining a proper macro (#define your_macro(...) code_to_populate_char_and_declare_variable), but I cannot see any point in doing so.

Related

Get length of any function? [duplicate]

This question already has answers here:
Getting The Size of a C++ Function
(17 answers)
Closed 1 year ago.
Assuming there's some function in my program or any linked library to it, is there a way to get the length of the function in bytes? By the length I mean the size of the whole code inside the function, from the start to the very end (functions branch out, so the end would be e.g. last 'ret' instruction, or something like that). Is there a simple way to do that, maybe some API functions or is it too much non cost-effective to create such a function?
There are probably tools to dump the contents of an object file, and then you can parse the output of this dump, and there is a chance that you can find out more or less reliably what the length of s function is. Adding data that is used by the function is more tricky. Or adding things like lambdas / closures that should probably be counted towards the length of the function.

Pascal - how sets work? [duplicate]

This question already has answers here:
What is the implementation of sets used in pascal?
(2 answers)
Closed 6 years ago.
I'm going to a high school programming competition tomorrow, and they use Pascal, about which I can't find much information on the internet, or if I do, I can't really understand it (English isn't my native language).
It would be much appreciated if - someone who still remembers, would explain me: what is a set? Or, how would it look like in C programming language? I guess it's something related to arrays, but I'm not sure though.
Thanks for help in advance!
A set is an unordered collection of elements in which each element can occurr only once.
Depending on what the unique identification of an element is, there can be many ways to implement a set, in any language.
For example, the unique identification is a name and it is mapped onto a number from zero to the size of the set in some way, and this number is used as an index into an array where each array element is [a pointer to] the element. Or there is an array of 32 bit ints and each bit tells whether the element exists in the set and the elements themselves are stored by number in an ordered linked list.
So you see, whithout having more information of what is to be stored in the set, there are numerous implementations possible.

generating new variables in c [duplicate]

This question already has an answer here:
Creating variable names from parameters
(1 answer)
Closed 8 years ago.
Is is possible for your code to generate new variables in c? For example, if I made "example_variable = 15", is there any way to automatically generate 15 new variables such as: "generated_variable_1", "generated_variable_2", "generated_variable_3", all the way to "generated_variable_15"?
I'm very new to c, and I haven't had a proper introduction to it, so I only know the basics, especially when it comes to variables. I am pretty sure this is really high-level stuff, so I'm sorry if the question doesn't make sense. I am open to any suggestions for alternate ways of generating the variables.
I know there are probably answers already out there, but I've had trouble finding them and would like answers specific to what I'm looking for, as opposed to piecing together what I need from what I can find.
What you are talking about - generating variables at runtime - is not possible in C. The reason is that C is a low-level language and does not expose an API for runtime manipulation. In fact, once compiled, C programs don't use variables - are values are stored directly in memory using memory addresses.
The closet equivalent to what you're looking for that's available in C is an "array". To declare an array, you can do:
int var[15];
int var2[n]; // in C99+, n is a variable saying how many elements you want in the array
You can also do this with malloc, but this is a bit more complicated and then you must free the values.
A running C program doesn't use your variable names at all. Those names were useful for the compiler to build the program, but are discarded before you run it. This means that in C (but not in interpreted languages like python):
If you rename your variables, you get the exact same program
If you do strings <your program> you won't see any variable names (unless you retained debugging symbols)
Hence, runtime is too late to create new variables. In C, variables are compile-time only. Of course, you can use arrays, or dictionaries, to simulate run-time variable creation, like the other answer, and a few commenters, suggest.

What is the way to find the String literal size limit [duplicate]

This question already has answers here:
Is there any limitation on string length defined in variable argument list
(3 answers)
Closed 8 years ago.
I wanted to know what are the ways to find out the String literal size limit.I guess different compilers do provide the max size limit of string literal but how do I find it programmatically or there is some standard header files which maintain this size limit as some macro??
I checked the C99 draft, all it says is that at least 4,095 characters should be supported in a string literal; there doesn't seem to be a maximum length. This makes sense to me; why impose such a limit?
I really don't think you can "detect" this at run-time. Of course you should be able to detect it, crudely speaking, at compile-time by checking if the compilation succeeds. Write a program that generates a program containing a string literal of a given length, then try to build that output and iterate until building fails. Of course you will only have learnt something about your particular compiler, not a general lesson.
Perhaps you should try to state your actual problem, it seems you're kind of hinting at it instead.

Do Perl's sigils have anything to do with memory allocation? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why do Perl variables need to start with $, %,#?
Other scripting languages seems to get along just fine without this or something similiar.
I guess it has something to do with memory allocation and helping the interpreter in order to speed things up, but I couldn't find anything specific on it. $scalar would probably be put into stack, #array into heap and %hash? Into heap as well? And what about ?subroutine?
Could someone help me figure this out or point me to some documentation? I am still trying to grasp some fundamentals and understand how everything works under the hood...
Because it makes it easier to read.
You know which identifiers are nouns, and whether they're singular or plural, because of the sigaldry. It's the same reason in English we have singular and plural determiners and agreement, as in this species is vs these species are. It's nice to know which is which.
Perl stores all data associated with a name in a single symbol table entry. The structure stored there is called a typeglob. The values for $foo, #foo, %foo, and &foo (subroutine reference) are all stored in the typeglob for "foo". The entire typeglob is denoted *foo (where * indicates "all sigils"). All this is explained in the perldata section of the Perl documentation.

Resources