error in auto loop

why there is a compilation error for the second code while first code is running file while i am using

auto loop

code 1:
enter code here

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string dic[] = {"hello ","india","go"};
	for(auto &i:dic){
		cout<<i<<endl;
	}

code 2:

#include<bits/stdc++.h>
using namespace std;
void solve(string dic[])
{
	for(auto &i:dic){
		cout<<i<<endl;
	}
}
int main()
{
	string dic[] = {"hello ","india","go"};
	solve(dic);
}

here are two sample :

code 1 : https://ideone.com/blf5W2

code 2 : https://ideone.com/AvKmIT

why case 2 is wrong ?

take this with a grain of salt as I am not that experienced with C++ ( started learning C++ from 2016-2017 ), I believe this is caused by the fact that in the second code, the dic[] has become a pointer and as such caused an error, scratch that, I am sure that this is caused by the fact that dic[] has become pointer and as foreach ( that for auto is what’s called foreach ) is not supposed to traverse through pointer, it caused an error. why not make the function accept an array along with the size, and traverse through the pointer using normal for

2 Likes

I checked normal for loop is working fine.
Pointer must be the trouble issue in it…
Thanks for explaining!

1 Like