I am experimenting with pointers and strings. In the following code, everything is tested (you can compile and run yourselves), but I keep crashing when I am trying to use
strcmp. The code runs without warnings in codeblocks. Can you please point out my mistake that makes the program crash??
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int choice,x=7,k,q,cmp,j=0,i=0,N;
char v[5],str[80],c[5];
char *B[11][2]=
{"-----","0",
".----","1",
"..---","2",
"...--","3",
"....-","4",
".....","5",
"-....","6",
"--...","7",
"---..","8",
"----.","9",
NULL,NULL};
printf("Give a stream");
if(gets(str)) printf("%s\n",str);
fflush(stdin);
i=0;
while(str[i])
{
q=0;
j=i+1;
while(q%5!=0)
{
v[j]=str[j];
puts(v);
j++;
q++;
}
printf("Hello");
for(k=0; k<11; k++)
{
strcpy(c,B[k][0]);
printf("%s",c);
x=strcmp(v,c);
//Problem:
printf("%d",c);
if(x==0) printf("hi");
}
i=i+1;
j++;
}
}
You aren’t filling the
vwith data. The conditionwhile(q%5!=0)is false on entry sinceq == 0. This leavesvuninitialized, i.e. containing garbage.