So, I was trying to learn pointers from scratch and I hit up on this. I tried doing the following:
void func(int* ptr){
//Yada-yada
}
int main(){
int a[] = {1,2,3,4,5};
func(a);
}
which worked out great. But then, I tried to do the following:
void func(int* ptr){
//Yada-yada
}
int main(){
int a[] = {1,2,3,4,5};
func({1,32});
}
which returned an error saying
too many initializer values So I replaced it with
func({1});
which ended up with
a value of type "int" cannot be used to initialize an entity of type "int *"
So I'd like to know why this happens. Does the compiler not allocate space for an array that is not being used as an rvalue for a variable?? Because if it does, the expected result would be that ptr is passed the address of the first variable in either of the {1,32} or {1} cases.
Thanks in advance. P.S : Only reply to this if you intend to help. If you have comments to tell me to go read a book or something, I already am. But to the ones who actually want to be helpful, links to references are very much appreciated.
{1,32}is astd::initialized_list<int>, which is not convertible to anint *.{1,32}is not an array. It doesn't have a type at all according to the C++ language specification. It's a syntactic construct that can be used to initialize objects. Sometimes, the properties of the object can be deduced from it, but those are the exceptions, not the rule.