C++ correct references [duplicate] - c++

This question already has answers here:
Whats the difference between these two C++ syntax for passing by reference? [duplicate]
(1 answer)
Placement of the asterisk in pointer declarations
(14 answers)
Closed 4 years ago.
Can someone explain what the difference is, if any, between these two lines:
int& i;
int &i;
I know these are both references and both seem to work fine. Is there a reason to use one over the other? Is there any rule saying what is the right way?
Thanks in advance.

There is absolutely no difference in meaning between these two, it is a purely stylistic matter. Just pick one and try to be consistent within a project.
I believe the examples in the language standard put the & symbol on the left - that's as good a reason as any to prefer one way over the other, I suppose.
That said, as you've written it, neither line is valid code, because you can't have an uninitialised reference. You would need something like:
int a = 10;
int& b = a;

Related

A quick c++ array initialization question using non const variable [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 9 days ago.
I am very new to C++ and I am wondering if I can do the following,
int a =5;
int b[a];
If so, what would happen if value of a changed? and any drawback using this.
If not,why it that?
thanks!
I tried the same code on an online c++ compiler and it does work. but i just dont know if this is a standard practice or not. if not, what would be the standard practices of refering a dynamic value to initialize an array?
No you cannot do that. Array bounds in C++ must be constants. This is legal
const int a = 5; // a is a constant
int b[a];
Some compilers do allow your code, but it is an extension to the C++ language called a variable length array or VLA.
But even on the compilers that do allow it, nothing happens to b if you change a, it will still have the same size as when it was created.
You can avoid all these issues by using a vector, like this std::vector<int> b(a); That is the correct way to do what you are asking for. You need to #include <vector> for the std::vector class.

char () and int () are functions in c++? [duplicate]

This question already has answers here:
What is the difference between static_cast<> and C style casting?
(7 answers)
Closed 4 years ago.
after 30 min of googling I decided to ask here my doubt
It maybe invalid question if I am not understanding below line of code correctly
result += char(int(text[i]+s-65)%26 +65);
In above code char() is a function ? If yes so I am unable to find any information about it and if no so what is this ?
And same doubt for inner int() .
Above code is copied from a C++ program.
They're not functions. They're just alternate syntax for type-casting. char(x) is more-or-less equivalent to static_cast<char>(x).
In general, in C++, one should prefer the C++-specific constructs for casting objects (static_cast, dynamic_cast, const_cast, and reinterpret_cast), as those help ensure you don't do anything dumb when casting objects. So in your code example, I'd recommend rewriting it as
result += static_cast<char>(static_cast<int>(text[i]+s-65)%26 +65);
But functionally, it's all identical.

What does mean "int(i)=1;"? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
I am new to C++, I see following syntax in c++ to initialize variable.
int(i)=1;
Then, I have compiled in G++ compiler and compiler did not give any error or warning.
So, What does mean int(i)=1; in C and C++?
Also, I have tested in C, I thought, the C compiler give an error but it's also working fine.
It's basically a strange way to write
int i = 1;
Nothing to worry about.
Sometimes, parenthesis around the variable name are necessary in defintions (eg. pointer to functions), and there is no reason to prohibit them for other cases, so it's allowed without any deeper reason.
Maythe the author didn't like spaces (such people exist).

What does this incremental syntax mean? [duplicate]

This question already has answers here:
scope resolution operator without a scope
(6 answers)
Closed 5 years ago.
I came across a line of code that is quite confusing to me. I tried searching but wasn't sure what to even search for.
The line is as follows:
int i = ++::i;
I am lost to whether this line evaluates to:
int i+= i ;
Any help would be appreciated
In
int i = ++::i;
the :: is there to tell the compiler use the i from the global scope. Without it the compiler is going to use the i you just declared which is undefined behavior. For more on that see Using newly declared variable in initialization (int x = x+1)?

Is multiple assignment expression (a=123, b=456, c=789) well-defined? [duplicate]

This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 7 years ago.
Edit: Apologies for posting a duplicate and such a basic question! I thought it was a trickier one than it actually was, and failed to discover the cited article in my original search.
For the language standard experts out there, is the value of an expression like:
(a=1, b=2, c=3)
... defined? From my tests, it appears that all of our compilers evaluate this expression as 3 (GCC, MSVC, Clang).
However, I'm not sure if we should lean on this behavior. I have no intention of writing code like this, but encountered some obscure code which was leaning on this behavior with multiple assignment in a conditional, and was wondering if it was well-defined.
If a, b, and c are all already defined, you are using a comma operator, which would return the value of c = 3, which is 3.
So to answer your question, yes, it is well defined.