-2
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

struct  Position 
{
    char* x1[2];
    char* x2[2];
};

int main(void)
{ 

     struct Position positions[4];  
     struct Position p1[4];
     char* p;
     matrix[10][10];

    if(r==0) //stores horizontal words in matrix
    {
        emptyH=0;        
        do
        {
            x=(rand()%size);
            y=(rand()%10);
            emptyH=0;

            for(k=0;k<ans;k++)
            {
                if ( matrix[y][x+k]!='1')
                {
                    emptyH=1;
                    k=ans; 

                } // checks whether word fits in matrix and position is empty
            } 
        }while(emptyH);


      token = strtok(words[random],search); //strtok to seperate char by char

        matrix[y][x]=token;
        if(x!=0)
        {
            p=(char)x;
            p1[i].x1[0] = p;//SAVING the positions of x and y
        } 
        else
            p1[i].x1[0]='\0';
        if(y!=0)
        {
            p=(char)y;
            p1[i].x1[1]=p;
        } 
        else 
            p1[i].x1[1]='\0';


         x++; 

         while(token!=NULL) 
        {
            token = strtok(NULL, search); 

            if(token!=NULL)
            {
                matrix[y][x]=token;

                p=(char*)x;  
                p1[i].x2[0]=p;
                p=(char*)y;
                p1[i].x2[1]=p; // saving positions of x and y

                x++;  


                positions[i] = p1[i];


            } 
        }
    }

    correct=0;
    do
    {

        puts("\nenter the word:");
        scanf("%s",&name);
        printf("\n Enter the start and end position of the word");
        puts("\nstart:");
        scanf("%s",&start);
        puts("\n\tend:");
        scanf("%s",&end);
        for(k=0;k<4;k++)
        {

            if((strcmp(positions[k].x1,start)==0)&&(strcmp(positions[k].x2,end)==0))
            {
                puts("viola!");
                correct=1;
            } 
            else 
                puts("incorrect answer! try again");
        }  
    }
    while(correct!=1);
}

this is my code. however string compare always gives wrong answer.also sometimes there is a runtime error as i store the positions in the 'p1[i].x1[0]'. any ideas why? thankyou for your help in advance.

zwarrior
  • 101
  • 3
  • 11
Mar
  • 13
  • 6
  • what's happening with the code format, and did you attempt to compile your code yet? – artm Dec 10 '15 at 11:28
  • there are no syntax errors. however the strcmp always gives me " incorrect answer try again!" even if it is correct – Mar Dec 10 '15 at 11:30
  • What is `words`? What is `random`? What is `search`? – M Oehm Dec 10 '15 at 11:31
  • @chri please indent your code properly. It is very difficult to read it this way. – wimh Dec 10 '15 at 11:34
  • Words is a list of strings, random generates a random number and search is used to search for the next blank space to seperate words. those parts have been tested and are working accordingly which is why i left out info about them – Mar Dec 10 '15 at 11:35
  • Apparently you want to fill a grid with words and then ask the user to find them, right? Then you don't want `strcmp`, which compares strings, i.e. null-terminated char arrays, anyway. Compare the characters one by one. – M Oehm Dec 10 '15 at 11:35
  • i will try that! thanks very much @MOehm – Mar Dec 10 '15 at 11:38
  • are you teling me to compare than with an equals ? @MOehm – Mar Dec 10 '15 at 11:44
  • Because you have left out random parts of your program, it is not clear what you want exactly, but, yes, I thought of comparing individual chars with the equals operator `==`. (I'm not sure I understand what the position struct is for, for example.) – M Oehm Dec 10 '15 at 11:47
  • What is 'matrix[10][10];' ?? – Martin James Dec 10 '15 at 12:00
  • How is `name` defined? – Ian Abbott Dec 10 '15 at 12:14
  • And how are `start` and `end` defined? The fact that you are using `scanf("%s", &start)` etc. suggests you are doing it wrong. The argument corresponding to the `%s` conversion specification should be of type `char *` and should point to the start of an array of `char` large enough to hold the expected input plus a null terminator. – Ian Abbott Dec 10 '15 at 12:28

1 Answers1

1

You are not setting up your pointers properly. You do this:

 p1[i].x1[0]='\0';

So you are initializing x1[0] to point to the zero address. That's bad. You need to allocate some space and init x1[0] to point to that space.

You could add:

p1[i].x1[0] = malloc(sizeof(char));  // Now x1[0] points to some allocated space.

and then you could dereference x1[0] properly:

 *(p1[i].x1[0])='\0';
nicomp
  • 4,344
  • 4
  • 27
  • 60