c++ - Guess the output. (Declaration/ initialization of a character array) -
here piece of code. i'm using g++
void initialize(int p, char a[], char b[]) {     for(int i=0;i<6*p;i++)     {         a[i]='-';         b[i]='-';     } }  int main() {     int p=9;     char a[2*p],b[4*p];     initialize(p,a,b);     cout<<a<<endl<<b<<endl; } here, way , b declared... should of size 18 , 36 respectively. aren't seen in output. happening?
output: ------------------------------------------------------ ------------------------------------------------------------------------------------------------------ 
you're initialising far outside bounds of memory have allocated a , b
you need do
for(int = 0; < 2*p; ++i) {     a[i]='-'; }  for(int = 0; < 4*p; ++i) {     b[i]='-'; } assigning memory outside of memory own literally anything. send fax dog.
Comments
Post a Comment