Questions tagged [subscript-operator]
58 questions
43
votes
2 answers
Accessing arrays by index[array] in C and C++
There is this little trick question that some interviewers like to ask for whatever reason:
int arr[] = {1, 2, 3};
2[arr] = 5; // does this line compile?
assert(arr[2] == 5); // does this assertion fail?
From what I can understand, a[b] gets…

NullUserException
- 83,810
- 28
- 209
- 234
24
votes
6 answers
Swift operator `subscript` []
I am beginner with the Swift having no advance knowledge with operators.
I have the following class
class Container {
var list: [Any] = [];
}
I want to implement the operator subscript [] in order to access the data from list.
I need something…

Colateral
- 1,736
- 2
- 18
- 22
11
votes
4 answers
Overloading subscript operator "["
I am trying to overload the subscript operator ("[") for a custom class I've created. I am trying to figure out how to deal with the following issues.
How can you figure out if the operator is on lhs or rhs ? i.e. a[x] = foo vs foo = a[x]
When…

Pavan Yalamanchili
- 12,021
- 2
- 35
- 55
9
votes
3 answers
Do std::strings end in '\0' when initialized with a string literal?
I know string objects aren't null terminated but why should this work?
std::string S("Hey");
for(int i = 0; S[i] != '\0'; ++i)
std::cout << S[i];
So the constructor copies the null terminator as well, but does not increment the length? Why does…

nek28
- 259
- 1
- 2
- 6
9
votes
3 answers
is int[pointer-to-array] in the C++ - standard?
As I have learned, one can write the following code:
char *a = new char[50];
for (int i = 0; i < 50; ++i) {
i[a] = '5';
}
It compiles. It works. It does exactly the same as
char *a = new char[50];
for (int i = 0; i < 50; ++i) {
a[i] =…

styrofoam fly
- 578
- 2
- 9
- 25
7
votes
1 answer
Do pointers support "array style indexing"?
(Self-answered Q&A - this matter keeps popping up)
I assume that the reader is aware of how pointer arithmetic works.
int arr[3] = {1,2,3};
int* ptr = arr;
...
*(ptr + i) = value;
Teachers/C books keep telling me I shouldn't use *(ptr + i) like in…

Lundin
- 195,001
- 40
- 254
- 396
7
votes
7 answers
Overloading the subscript operator "[ ]" in the l-value and r-value cases
I have overloaded [] operator in my class Interval to return minutes or seconds.
But I am not sure how to assign values to minutes or second using [] operator.
For example : I can use this statement
cout << a[1] << "min and " << a[0] << "sec" <<…

Searock
- 6,278
- 11
- 62
- 98
7
votes
1 answer
Struct type "does not provide a subscript operator"
I am trying to read values from a file into an array of structs. However, I keep getting compiler errors that tell me that my struct, Books, does not provide a subscript operator and I am lost.
The struct is contained in a header file while…

jshapy8
- 1,983
- 7
- 30
- 60
6
votes
2 answers
Comma Operator in subscript operator?
I am quite confused with the comma operator. I have never seen such code with such syntax? but I am curious if it useful at any place? why is it deprecated in c++20?
#include
int main()
{
int a[5]{1,2,3,45,5};
std::cout <<…

Nilesh Solanki
- 336
- 4
- 19
6
votes
3 answers
Stack overflow when defining subscript on CKRecord in Swift
This question asks whether one can use subscripting with CKRecord in Swift. While I already knew how to do what the questioner wanted, every permutation of it gives me a stack overflow:
subscript(key: String) -> CKRecordValue? {
get {
…

Gregory Higley
- 15,923
- 9
- 67
- 96
6
votes
2 answers
What type does C++ expect for array subscripts?
In C, array subscription: a[b] is merely the syntactic sugar equivalent of dereferencing after pointer arithmetic: *(a+b) (as explained, say, here).
How is array subscription interpreted in C++, for base types? (Not for classes, for which we have…

einpoklum
- 118,144
- 57
- 340
- 684
5
votes
1 answer
Why does operator[] only take one argument?
There are plenty of questions related to operator[] only taking one argument, but I can't find one that actually says why.
For example, it seems a very natural extension of the language to have matrix[0, 3] call an ElementT& operator[](SizeT x,…

user673679
- 1,327
- 1
- 16
- 35
4
votes
5 answers
How do I access a C++ subscript operator from within the class in which it resides?
Where, ClassA has an operator as such, that returns ClassB:
class ClassA
{
public:
ClassA();
ClassB &operator[](int index);
}
If I want to access said operator from within ClassA's constructor, as so:
ClassA::ClassA()
{
// How do I…

Nick Bolton
- 38,276
- 70
- 174
- 242
4
votes
8 answers
Can C++'s operator[] take more than one argument?
Is it possible to define an overloaded operator[] that takes more than one argument? That is, can I define operator[] as follows:
//In some class
double operator[](const int a, const int b){
return big_array[a+offset*b];}
and later use it…

Dan
- 12,157
- 12
- 50
- 84
4
votes
1 answer
How can I overload the subscript operator to return an optional which can be an lvalue?
I'm learning some C++ features by implementing an octree class. I want the subscript operator on this class to return the octant corresponding to an index. How should I define the subscript operator on the class so that I can both (i) assign to the…

holomenicus
- 151
- 5