Why i am getting SIGSEGV in my program

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int t;
scanf("%d",&t);
while(t–)
{
char a;
int n,i,x,y;
x=0;
y=0;
a=malloc(n
sizeof(char));
scanf("%s",a);
n=strlen(a);
for(i=0;i<n;i++)
{
if(a[i]==’<’)
x++;
else
y++;
}
int z,count;
z=0;
count=0;
if(x>y)
{
for(i=0;i<2y;i++)
{
if(a[z]==’>’)
{
i=2
y;
}
if(a[i]==’<’)
count++;
else
count–;
if(count==0)
{
z=i+1;
}
else
continue;
}
}
else
{
for(i=0;i<2x;i++)
{
if(a[z]==’>’)
{
i=2
x;
}
if(a[i]==’<’)
count++;
else
count–;
if(count==0)
{
z=i+1;
}
else
continue;
}
}
printf("%d\n",z);
}
}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also - what Problem are you trying to solve? :slight_smile:

for ques COMPILER
https://www.codechef.com/viewsolution/33417890
why i am getting SIGSEGV

Read the compiler warnings, dude :slight_smile:

aryan_basu-COMPILER.cpp:14:16: warning: ‘n’ may be used uninitialized in this function [-Wmaybe-uninitialized]
 a=(char*)malloc(n*sizeof(char));

2 Likes

sorry but i am beginner i dont know what to made change in code could you please explain it

just add (char*) this line in as he clearly stated it there
a=malloc(n* sizeof(char));
a=(char*)malloc(n*sizeof(char));
and initialize value of n as it it not initialized before the use of n it is defined.

thanks a lot to both of you thank u so much

1 Like