Helpful Information
 
 
Category: C Programming
Problem copying pointer value using strcpy function

the following code compiles ok but I get an memory access error message when executing. I believe the problem is when I attempt to use the strcpy() function to copy a pointer value (token) to another variable (pat). Any help would be greatly appreciated.

# include <iostream.h>
# include <string.h>

int bmsearch(char *text,char *pat,int *no,int *pos)
{
int len=strlen(pat);
int compcount=0;
int ptct=len-1;
int i,table[256];
*no=0;
for(i=0;i<256;i++)table[i]=len;
for(i=0;i<len;i++)table[pat[i]]=len-(i+1);
while(ptct<strlen(text))
{
int count=0;
while(count<len)
{
if(text[ptct-count]!=pat[len-1-count]){compcount++;break;}
else count++;
}
if(count==len)
{
(*no)++;
*(pos+(*no)-1)=(ptct-count+1);
ptct+=len;
}
else
{ptct+=table[text[ptct-count]];}
}
return compcount;
}

************************************************/

# include <conio.h>
# include <stdio.h>

main(void)
// int argc;
// char *argv[];

{
FILE *fptrin;

char text[300],pat[100];
int no,count,pos[20],i;
int pos_ave = 0;
char txtline[140];
char txtfile[]=" ";
int length;
char *token;
char seps[] = " ,\t\n";


length=strlen("e003stmt.dat");
strncpy(txtfile,"e003stmt.dat",length);
if((fptrin=fopen(txtfile,"rb"))==NULL)
printf("Can't Open In File !\n");
while(fgets(txtline,139,fptrin)!=NULL)
{
printf("%s\n\nTokens:\n",txtline);
/* Establish string and get the first token: */
token = strtok(txtline,seps);
while(token != NULL)
{
/* While there are tokens in "string" */
printf(" %s\n", token);
/* Get next token: */
token = strtok(NULL, seps);
}

// printf("\n\nEnter the text string:");
// gets(text);
strcpy(pat,token);
count=bmsearch(txtline,pat,&no,pos);
printf("\n\nPattern string occurred %d times",no);
printf("\n%d comparisons required",count);
printf("\nPositions of occurrence:\n");
for(i=0;i<no;i++)
{
printf("%d\n",pos[i]);
switch(i)
{
case 0:
break;
default:
pos_ave = pos_ave + pos[i];
break;
}
}
if(pos_ave!=NULL)printf("pos_ave = %d\n",(pos_ave)/i++);
printf("Thank you\n\n");
return 0;
}
}
strcpy(pat,token); strcpy(pat,token);

It really helps if you use code tags!

char txtfile[]=" ";
int length;
char *token;
char seps[] = " ,\t\n";


length=strlen("e003stmt.dat");
strncpy(txtfile,"e003stmt.dat",length);


The strncpy() (last line shown above) is scribbling all over the program's address space and causing great havock with the operating system! You are attemping to copy 13 characters into a buffer that is only 1 character (or maybe 2 if that is actually a space inside the quotes). You need to define txtfile like this:


#define MAXSIZE 80 // or some other reasonable value
char txtfile[MAXSIZE];

// now copy the file name into the buffer
strcpy(txtfile,"e003stmt.dat");


Or if txtfile will always contain the same value, you could do it like this


char txtfile[] = "e003stmt.dat";

// printf("\n\nEnter the text string:");
// gets(text);
strcpy(pat,token);


And above is another problem -- what do you think is the value of "token" here? Look at the loop above this quote and you will see that the loop terminates when token == NULL. That means the strcpy shown above will crash because you are passing NULL as the second argument.










privacy (GDPR)