0

私はかなり長い間このコードを見つめていましたが、このコードの何が問題で、どのように修正すればよいのかわかりません。割り当てられたものを超えて配列の1つが書き込まれていると思います。

デバッガーはコンパイルを許可していますが、実行すると次のようになります。

Unhandled exception at 0x774615de in HW6_StringProcessing.exe: 0xC0000005: Access violation.

コードは次のとおりです。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_SIZE 100

void convertToPigLatin (char * strPtr, char * pStrPtr);

int main(int argc, char *argv[])
{
   char str[MAX_STR_SIZE];
   char pStr[MAX_STR_SIZE];
   FILE *fileInPtr;                             //Create file name
   FILE *fileOutPtr;    

   fileInPtr = fopen("pigLatinIn.txt", "r");    //Assign text to file
   fileOutPtr = fopen("pigLatinOut.txt", "w");

   if(fileInPtr == NULL)                        //Check if file exists
   {
      printf("Failed");
      exit(-1); 
   }
   fprintf(fileOutPtr, "English Word\t\t\t\tPig Latin Word\n");
   fprintf(fileOutPtr, "---------------\t\t\t\t----------------\n");

   while(!feof(fileInPtr))                  //Cycles until end of text
   {

      fscanf(fileInPtr, "%99s", str);       //Assigns word to *char

      str[99] = '\0';                       //Optional: Whole line

      convertToPigLatin(str, pStr); 

      //fprintf(fileOutPtr, "%15s\t\t\t\t%15p\n", str, pStr); 

      printf("%s\n", str); 
   }  

   system("pause"); 
}

void convertToPigLatin (const char * strPtr, char * pStrPtr)
{
   int VowelDetect = 0; 
   int LoopCounter = 0; 
   int consonantCounter = 0; 
   char cStr[MAX_STR_SIZE] = {'\0'};
   char dStr[] = {'-','\0'}; 
   char ayStr[] = {'a','y','\0'};
   char wayStr[] = {'w','a','y','\0'};

   while (*strPtr != '\0')
   {
      if (*strPtr == 'a' || *strPtr == 'e' || 
          *strPtr == 'i' || *strPtr == 'o' || 
          *strPtr == 'u' || VowelDetect ==1)
      {
         strncat(pStrPtr, strPtr, 1); 
         VowelDetect = 1; 
      }
      else
      {
         strncat(cStr, strPtr, 1); 
         consonantCounter++; 
      }
      *strPtr++;
   }
   strcat(pStrPtr, dStr); 
   if (consonantCounter == 0)  
   {
      strcat(pStrPtr, wayStr);
   }
   else
   {
      strcat(cStr,ayStr);
      strcat(pStrPtr, cStr);
   }  
   printf("%s\n", pStrPtr);                         

}
4

4 に答える 4

0

宣言:

void convertToPigLatin (char * strPtr, char * pStrPtr);

は関数の定義と一致しません:

void convertToPigLatin (const char * strPtr, char * pStrPtr)
{
   ...                       
}
于 2013-07-25T06:03:59.557 に答える