Pointer Error

This is a discussion on Pointer Error within the C Programming forums, part of the General Programming Boards category; Good day, I am having a pointer error in my string compare. can somebody please correct me? the program is ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    Pointer Error

    Good day, I am having a pointer error in my string compare. can somebody please correct me? the program is reading have a rough text file of

    abcde
    ebcad
    acdbe

    and several same letters of the different orders and check which one is equal to the first one. I got the comparison right and it produces

    ae
    bb
    cc
    da
    ed

    and

    aa
    bc
    cd
    db
    ee

    so the output should be
    bb
    cc

    and

    aa
    ee

    but it's give me an error.

    Code:
    int main(void)
    {
    
       FILE *fp;
       char *status, *status2;
       
       int times;
       int looptimes;
        
        struct
        {
    	char numberofItems[2];
    	char answersToQuestions[50];
        } answerKey[SIZEANS];
        
        struct
       {
    	char studentID[7];
            char answers[50];
       } answer[SIZE];
     
       int y, szAns[] = {0,3,51};
       int i, sz[] = {0,6,51};
       char buff[1024];
       char buff2[1024];
       char insert[1024];
       int x = 0;   
       int w = 1;
    
       fp = fopen("Exam.txt","r");
       status = fgets(buff, sizeof(buff), fp);
       status2 = fgets(buff2,sizeof(buff2),fp);
       
    
       for(times = 0; times<1;times++)
       {
    	strncpy(answerKey[times].numberofItems, strtok(buff," "), szAns[1]);
            strncpy(answerKey[times].answersToQuestions,strtok(NULL," "), szAns[2]);
    	printf("%s %s",answerKey[times].numberofItems,answerKey[times].answersToQuestions);
            
    	for(w = 0; w <= strlen(answerKey[times].answersToQuestions) - 2; w++)
    	{
    	   printf("%c\n",answerKey[times].answersToQuestions[w]);
    	} 
    
       }
    
       while(status2 != NULL)
       {
    	strncpy(answer[i].studentID, strtok(buff2," "),sz[1]);
    	strncpy(answer[i].answers, strtok(NULL," "),sz[2]);
            printf("%s",answer[i].answers);
            
    	for(x = 0; x<=strlen(answer[i].answers) - 1; x++)
    	{	
    		
    		if(strcmp(answer[i].answers[x], answerKey[0].answersToQuestions[x]) == 0)
    		{
    		printf("%c",answer[i].answers[x]);
    		printf("%c\n",answerKey[0].answersToQuestions[x]);
    		}
    		
    	} 
    	x = 0;
    	i++;
    	status2 = fgets(buff2,sizeof(buff2),fp);
       }  
    
    printf("\n");
    fclose(fp);
    return 0;
    }
    Last edited by xaykogeki; 04-16-2010 at 01:17 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    to store string of 3 chars you need buffer of 4 chars
    your buffer of 2 chars is only good for strings of 1 letter length
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by vart View Post
    to store string of 3 chars you need buffer of 4 chars
    your buffer of 2 chars is only good for strings of 1 letter length
    I changed the 3 chars,

    but the problem in the string compare is that, when I put "&" as indicated by the pointer error,

    my text file has

    dbbac

    so how many letters are the same with

    "dabac"

    the output should show

    dd
    bb
    aa
    cc

    but it's only showing

    bb
    aa
    cc

    and in the succeeding comparison, the output doesn't show anymore.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
    if(strcmp(answer[i].answers[x], answerKey[0].answersToQuestions[x]) == 0)
    		{
    		printf("%c",answer[i].answers[x]);
    		printf("%c\n",answerKey[0].answersToQuestions[x]);
    		}
    strcmp is used to compare strings. if you want to compare single character just do it as you do with integer values:

    Code:
    if(answer[i].answers[x] == answerKey[0].answersToQuestions[x])
    {
    	printf("%c",answer[i].answers[x]);
    	printf("%c\n",answerKey[0].answersToQuestions[x]);
    }
    Why do you need to print the same value twice?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by vart View Post
    Code:
    if(strcmp(answer[i].answers[x], answerKey[0].answersToQuestions[x]) == 0)
    		{
    		printf("%c",answer[i].answers[x]);
    		printf("%c\n",answerKey[0].answersToQuestions[x]);
    		}
    strcmp is used to compare strings. if you want to compare single character just do it as you do with integer values:

    Code:
    if(answer[i].answers[x] == answerKey[0].answersToQuestions[x])
    {
    	printf("%c",answer[i].answers[x]);
    	printf("%c\n",answerKey[0].answersToQuestions[x]);
    }
    Why do you need to print the same value twice?
    This is for testing purposes if I am getting the desired output xD

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by vart View Post
    Code:
    if(strcmp(answer[i].answers[x], answerKey[0].answersToQuestions[x]) == 0)
    		{
    		printf("%c",answer[i].answers[x]);
    		printf("%c\n",answerKey[0].answersToQuestions[x]);
    		}
    strcmp is used to compare strings. if you want to compare single character just do it as you do with integer values:

    Code:
    if(answer[i].answers[x] == answerKey[0].answersToQuestions[x])
    {
    	printf("%c",answer[i].answers[x]);
    	printf("%c\n",answerKey[0].answersToQuestions[x]);
    }
    Why do you need to print the same value twice?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define SIZE 81
    #define SIZEANS 81
    
    int main(void)
    {
    
       FILE *fp, *outFp;
       char *status, *status2;
       
       int times;
       int looptimes;
        
        struct
        {
    	char numberofItems[3];
    	char answersToQuestions[50];
        } answerKey[SIZEANS];
        
        struct
       {
    	char studentID[7];
            char answers[50];
       } answer[SIZE];
     
       int y, szAns[] = {0,3,51};
       int i, sz[] = {0,6,51};
       char buff[1024];
       char buff2[1024];
       char insert[1024];
       int x = 0;   
       int w = 1;
       int s = 1;
       int temporary;
       const int percent = 100;
       int score = 0;
       int trialpo;
    
       fp = fopen("Examdat.txt","r");
       outFp = fopen("outputData.txt","w");
       status = fgets(buff, sizeof(buff), fp);
       status2 = fgets(buff2,sizeof(buff2),fp);
       
    
       for(times = 0; times<1;times++)
       {
    	strncpy(answerKey[times].numberofItems, strtok(buff," "), szAns[1]);
            strncpy(answerKey[times].answersToQuestions,strtok(NULL," "), szAns[2]);
    	printf("%s %s",answerKey[times].numberofItems,answerKey[times].answersToQuestions);
    
    	fprintf(outFp,"\tExam Report\nQuestion");
    
    	temporary = atoi(answerKey[times].numberofItems);
    	for (s = 1; s <= temporary; s++)
    	{
               fprintf(outFp,"\t%d",s);
    	}
    	
    	fprintf(outFp,"\nAnswer");
    	for(w = 0; w <= strlen(answerKey[times].answersToQuestions) - 2; w++)
    	{
    	   printf("%c\n",answerKey[times].answersToQuestions[w]);
    	   fprintf(outFp,"\t     %c", answerKey[times].answersToQuestions[w]);
    	}	    
       }
      
       fprintf(outFp,"\n\nID\t\tScore(%)\n");
       while(status2 != NULL)
       {
    	strncpy(answer[i].studentID, strtok(buff2," "),sz[1]);
    	strncpy(answer[i].answers, strtok(NULL," "),sz[2]); 
    
    	
    	fprintf(outFp,"%s", answer[i].studentID);
    	printf("%d",strlen(answer[i].answers) - 1 );
    
    	for(x = 0; x<=strlen(answer[i].answers) - 1; x++)
    	{	
    		if(answer[i].answers[x] == answerKey[0].answersToQuestions[x])
    		{
    		score += 1;
    		printf("%c",answer[i].answers[x]);
    		printf("%c\n",answerKey[0].answersToQuestions[x]);
    		}	
    	} 
    	x = 0;
    	
    	status2 = fgets(buff2,sizeof(buff2),fp);
    
    	
    	//printf("%d\n",score - 2);
    	//score /= temporary;
    	//score *= percent;
    	fprintf(outFp,"\t%d\n",score);
    	i++;
    	
    
    	
       }  
    
    printf("\n");
    fclose(outFp);
    fclose(fp);
    return 0;
    }
    The problem is now that every time I put the string length of each of the text file, it's giving me a 665
    instead of a 666,

    so that means, my string length is decreasing?


    is there a way to fix this? thanks.
    Last edited by xaykogeki; 04-17-2010 at 12:51 AM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're printing out "strlen(answer[i].answers) - 1", so if the strlen is 666, you will print out 665. If you want to see 666, don't subtract 1 before you print it.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by tabstop View Post
    You're printing out "strlen(answer[i].answers) - 1", so if the strlen is 666, you will print out 665. If you want to see 666, don't subtract 1 before you print it.
    if I don't put -1,
    it will print 776

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Oh you mean the last one is one shorter than the rest? Does the last one not contain a \n character at the end of the line that the other ones do have?

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by tabstop View Post
    Oh you mean the last one is one shorter than the rest? Does the last one not contain a \n character at the end of the line that the other ones do have?
    Yeah, the last one is shorter. but I think the last one should contain a \n since the second one has it too.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by xaykogeki View Post
    Yeah, the last one is shorter. but I think the last one should contain a \n since the second one has it too.
    If it came from a file that does not end with a \n character, then the last line that you read in will not have a \n character, while the other ones will.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21