Is it legal to access class members as an array? [duplicate] - c++

This question already has answers here:
Can I treat a struct like an array?
(8 answers)
Is it legal to index into a struct?
(10 answers)
Closed 2 years ago.
Consider this code:
struct Point {
double x;
double y;
double z;
void setByIndex(int i, double value) {
(&x)[i] = value;
}
};
The setByIndex() function works and does what I want. But can I be sure this is not a UB (when called with a proper i)? Could you please provide references to the standard with the explanation?

Related

what is for(auto& x : s) in C++ [duplicate]

This question already has answers here:
C++11 range based loop: How does it really work
(3 answers)
Range based for-loop with &
(3 answers)
How to correctly implement custom iterators and const_iterators?
(9 answers)
How to make my custom type to work with "range-based for loops"?
(10 answers)
Closed 21 days ago.
Since I am new to C++, I do not know what the expression means?
and how can i co-relate this expression with C language.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// string to be converted to lowercase
string s = "GEEKSFORGEEKS";
for (auto& x : s) {
x = tolower(x);
}
cout << s;
return 0;
}

What is the difference of sending the parameter like this from the pointer [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
When to pass by reference and when to pass by pointer in C++?
(8 answers)
Closed 3 years ago.
i used integer but i am asking for too much big datas. Will memory usage be the same ? Or speed ?
void func1(int *a)
{
}
void func2(int &a)
{
}
int main()
{
int a;
func1(&a); // pointer way
func2(a); // what is it ? is this also pointer way ?
}

Why output of 'p' and '&p' is not same for 'int* p' in c++;? [duplicate]

This question already has answers here:
Address of pointer
(5 answers)
C: Why do pointer and &pointer have different values?
(4 answers)
Closed 5 years ago.
Here p is int type of pointer variable.In my code as per I understand output will be same for two line.But it doesn't.So I want to konw why it doesn.t?
#include<iostream>
using namespace std;
int main(){
int* p;
cout<<p<<endl; //0x41c2de
cout<<&p<<endl; //0x28ff0c
}
Question: Why output of two line is not same??

Can I call a struct from within itself? [duplicate]

This question already has answers here:
self referential struct definition?
(9 answers)
Closed 6 years ago.
I want to know if its possible to make this in C++:
struct Hello
{
Hello A, B;
};
I would appreciate any kind of help
No because such usage will consume an infinite amount of memory.
You can store pointers to the struct instead, and this is a common way, for example, in implementing a linked list.
struct Hello
{
Hello *A, *B;
};

Meaning of a structname inside namespace of struct? [duplicate]

This question already has an answer here:
Why are redundant scope qualifications supported by the compiler, and is it legal?
(1 answer)
Closed 8 years ago.
What does it mean if I write:
struct Str1 {
char val1;
};
struct Str2 {
struct Str1::Str1 valS;
};
what does the added qualification ::Str1 mean?
Bonus points: How to match this one with a clang ASTMatcher ;-)
Greetings
It's the same as:
Str1 valS;
the extra struct Str1:: is redundant. See N0444