This question already has answers here:
Is there any way to loop through a struct with elements of different types in C?
(5 answers)
Closed 6 years ago.
I am writing program in C. I would like to print out values of each element in struct, so that I can print out according value for each bitmap header member. Hence, is it possible to iterate trough each element of struct?
Also is it possible to get number of elements in struct if each element is different size?
Regards
No, you cannot do this in C. There is no iterator over data types. You have to print each field in your struct separately in your function.
Addition:
One way to do this on your own would be to use X-Macros as suggested by coderredoc. But it might get a bit nast for different data types to print.
You can do this in C. Use some thing called X-macro . Not exactly iterator over data types but a smart way out.
link
Related
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.
This question already has answers here:
Array initialization C
(5 answers)
Closed 1 year ago.
I noticed a piece of code that made me raise a few eyebrows in the code base which I am working on.
Since I am not an expert of the C language, I first tried googling initializing fixed size arrays.
However, I could not find anything like that:
float array[7] = {0.0,};
To be precise, I would like to know how the comma is interpreted in C here? I doubt it is a typing error, because I find this kind of initialization all over the place and for all types of arrays.
Is there any difference to just writing:
float array[7] = {0.0};
This has nothing to do with floats. It goes for all arrays. Imagine that that rule did not exist and you you have this:
float arr[] = {
4.5,
2.3,
3.8
};
Later, you realize that you want to remove 3.8. Then you would ALSO need to remove the comma after 2.3. And the same thing goes if you want to insert a new value. Let's say that you have copied a value from somewhere. Now you cannot simply paste it. Because if the copied string has a comma, you could not simply paste it anywhere you want in the array.
The basic purpose is that you should not have to think about adding and removing commas. It's just convenience.
Imagine copying and pasting regular code and you would have to think about that for the semicolon everytime. That would be a bit annoying. That's how it is in Pascal. ;)
This question already has answers here:
Creating an atoi function
(5 answers)
atoi implementation in C
(6 answers)
Closed 4 years ago.
I should first start out by saying that I come from a Java background. That being said, I'm just starting to learn C and I really struggle with the use of pointers. The concepts are simple enough, but actually using them proves to be a rather difficult and frustrating experience for me.
At any rate, I'm trying to create a function that replicates atoi without using the stdlib.h library. I'm thinking it's a simple matter of casting, but when I test it I get some really strange results. What I have is as follows:
int myatoi(const char* str){
return (int)*str;
}
Given that I don't really know what I'm doing when it comes to pointers, I'm most certainly doing something wrong, but I have absolutely no idea what.
That will not work. Casting will result in the "reinterpretation" of the first character (at location *str) into its ASCII value as an integer.
You will need to process the string by iterating through its characters and parsing them.
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.
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.