Like all functions in <string.h>, it requires that the character array ends in '\0'. However, if you pass NULL as a pointer to any of those functions, the function will attempt to dereference it first so it can check for '\0' and thus the end of the string, but since you passed NULL, it can't dereference NULL, thus, segmentation fault.
why do I get Runtime error in problem 673? Please help.
[cpp]#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
char b[129];
void init(){
for(int i=0;i<129;i++)
b='\0';
}
class stack{
public:
int index;
stack(){index=0;}
int data[129];
void push(int n){data[index++]=n;}
int pop(){return(index==0)?-5:data[--index];}
};
int main()
{
stack s;
bool flag;
int res;
int c,i=0;
init();
scanf("%d\n",c);
for (int k=0;k<c;k++){
init();
i=0;
scanf("%s",b);
flag=true;
while(i<strlen(b)){
switch (b){
case '(': s.push(0);
break;
case '[': s.push(1);
break;
case ')':{
res=s.pop();
if (res!=0) flag=false;
}
break;
case ']':{
res=s.pop();
if (res!=1) flag=false;
}
break;
}
i++;
}
if (!flag || s.index!=0 || b!='\0') cout<<"No\n";
else cout<<"Yes\n";
}
return 0;
}[/cpp]
thnx
I have tried many inputs as are mentioned in the former topics but I still have no idea about WA with my code. Could anybody spare some time checking with my code and tell me the matter with my code? Thank you in advance.
[cpp]
#include <iostream.h>
#include <stdio.h>
int solve(char *);
int main()
{
long int n,count ;
int result;
char *chs = new char[129];
cin>>n;
for(count=0;count<n;count++)
{
gets(chs);
result = solve(chs);
if(result==1)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
..........
}[/cpp]
Last edited by AaronWu on Wed Jan 28, 2004 6:41 am, edited 1 time in total.
The problem is when you read the first line (n) ... using cin >> n, I guess it won't advance to the next line or something like that ... causing problems on the next cases ... try to use gets(), followed by atoi() to obtain n ... you should get AC after that.
-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
turuthok wrote: I guess it won't advance to the next line or something like that
-turuthok-
I have succeeded solving other problems in the same way before.and they goes so well on the online judge as they work in my computer.So,do you mean that my codes are all right except for the above?
Looking forward to reply~
turuthok wrote: I guess it won't advance to the next line or something like that
-turuthok-
I have succeeded solving other problems in the same way before.and they goes so well on the online judge as they work in my computer.So,do you mean that my codes are all right except for the above?
Looking forward to reply~
turuthok wrote: I guess it won't advance to the next line or something like that
-turuthok-
I have succeeded solving other problems in the same way before.and they goes so well on the online judge as they work in my computer.So,do you mean that my codes are all right except for the above?
Looking forward to reply~