how do i declare a whole bunch of integer variables
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how do i declare a whole bunch of integer variables
hi,
i have 14 variables that are integers. i would like to "hard code" them into my c++ code. is there a way to declare them all as integers (as a group) with one command or do i have to declare them each individually as ints in the "usual way"?
please forgive me if i'm not explaining my question very well. i'm very new to coding
In c you would declare it like this I think. Then you only have to pass a single pointer to the struct instead of each int separately. I think C++ would be the same. An object isn't hugely different from a struct as I've learned thus far. Initialize the struct in the code with the values and off you go.
Code:
struct int_load {
int1;
int2;
int3;
// and so forth
};
Last edited by jmgibson1981; 12-19-2023 at 10:52 AM.
Well, if you simply have "a bunch of variables and things," a struct is indeed a very convenient way to handle them. For instance, many of my applications will define a static struct common. And I just put "everything that everyone needs to know about and use" in there.
Now, you'll see throughout the code references to things like common.blahblahblah. And this immediately clues the next programmer that blahblahblah is a "common variable," and where to find it. In other words, it's descriptive.
If you go in my code to the place where this struct is defined, you will there find comments.Lots of them. Explaining what each element is and what it's used for. (I'm a very "gabby" coder, and it has served me very well.)
Last edited by sundialsvcs; 12-19-2023 at 08:46 PM.
i know a little bit about vectors, but i'm not very good with them, same for structures.
i don't think these are really variables, per se. they have a constant/unchanging value, separated by white space. even though it's not the most eloquent way, i'd like to "hard code" them.
If they are constants, as they certainly appear to be, then they are usually just declared in a mutually-included file. And they traditionally use ALL_UPPERCASE names. Various languages have various accepted ways to deal with "constants."
Last edited by sundialsvcs; 12-20-2023 at 08:32 AM.
pan64, i think that's exactly what i want to do. now i just have to figure out how to use them in the rest of my code.
from another part of the code, i will get an integer value, and then i need to correlate that integer value with the description string. so if the code gives me a 2 i will correlate it with C.
If they are constants, as they certainly appear to be, then they are usually just declared in a mutually-included file. And they traditionally use ALL_UPPERCASE names. Various languages have various accepted ways to deal with "constants."
sundialsvcs, yours is probably the preferred way of doing it. with the #define method i know it's just brute force, but i think that will be the easiest way to do it for a beginner like me.
Basically with a #define as posted the preprocessor will replace all instances of A with 0 in your source code.
Just a few ideas. Without knowing anything about what you are trying to accomplish mapping in your case can be as simple as:
char string[] = "ABCD...";
So that string[0]=A, string[1]=B and so on. If you have a description string of more then one character you can create an array of strings i.e.
char arr[3][10] = {"string 1", "string 2 ", "string 3"};
so that
arr[0]="string 1"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.