Why the size of array "g1" is 6 after strcpy ? It should be 9

#include<iostream>
#include<string.h>
using namespace std;

int main(){
char g1[] = "hello";
char g2[] = "12345678";
std::cout<<"g1 size Before -> "<<sizeof(g1)<<endl;
strcpy(g1, g2);
std::cout<<"g1 -> "<<g1<<endl;
std::cout<<"g1 size after-> "<<sizeof(g1)<<endl;
}

The sizeof(g1) is 6 and will not change. If you were to use the safe string copy on line 5:

 
strcpy_s(g1, sizeof(g1), g2);


You would find out that you are exceeding the boundary of g1 with that string copy.
Thanks for the response. But why sizeof(g1) will not change ?
why sizeof(g1) will not change ?

Because the array size is still the same.
we are copying the contents of g2 array to g1 array. Size should change. Please mark me if I am wrong on strcpy function usage.
The size of the buffer will not change (sizeof is a keyword not a function).

To get the length of the string use strlen():

http://www.cplusplus.com/reference/cstring/strlen/?kw=strlen
Great !!! Thanks a lot everyone for your responses. :)
Topic archived. No new replies allowed.