-1

according to DDD I'm getting a seg fault from strcpy but I can't quite figure out what I'm doing wrong (still quite new to C). Any help would be greatly appreciated, thanks in advance.

int compare_people(PERSON* first, PERSON* second)
{
    char firstName[32];
    char secondName[32];

    strcpy(firstName, first->name);
    strcpy(secondName, second->name);

    int returnVal = strcmp(firstName, secondName);

    return returnVal;
}
Sammdahamm
  • 11
  • 10
  • 2
    If either name is longer than 31 chars, it will write to invalid memory, since the buffers you made are only that big. – mukunda Dec 01 '14 at 21:45
  • The names are on average only 5-10 characters – Sammdahamm Dec 01 '14 at 21:46
  • 2
    I'd guess `first` or `second` is `NULL`. Use a debugger. – Retired Ninja Dec 01 '14 at 21:46
  • 1
    Need to show us the calling code so we know what `first` and `second` are. Followed of course by why bother with the `strcpy` calls. Why not just use `strcmp` on the `PERSON.name` fields? – John3136 Dec 01 '14 at 21:46
  • Okay, are you sure that first and second are always non-null, and their names are non-null? – mukunda Dec 01 '14 at 21:46
  • Now you mention it, I had taken the error to be generic, and hadn't thought about the possibility of the passed values being out of range. Thanks, I'll check that now. – Sammdahamm Dec 01 '14 at 21:49
  • @John3136, I already tried that, but strcmp gave a seg fault in that case. My above comment should explain why I tried changing to strcpy – Sammdahamm Dec 01 '14 at 21:50
  • @Sammdahamm - just more evidence that you have bad inputs - same problem, it just now happening in a different call. Basic debugging would find this problem! – John3136 Dec 01 '14 at 21:52

2 Answers2

2

It seems that either first or second is equal to NULL or first->name or second->name is equal to NULL or has non-zero terminated data that due to using strcpy exceeds 32 characters. The other reason can be is that first->name or second->name has invalid pointer for example a pointer to a local data that is already destroyed.

Insert a check in the function. For example

assert( first != NULL && second != NULL && 
        first->name != NULL && second->name != NULL &&
        strlen( first->name ) < 32 && strlen( second->name ) < 32 );

Or you can split this assert in several separate asserts.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
 just  try that code.

   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   typedef struct{

     char name[25];
     }PERSON;

   int compare_people(PERSON* first, PERSON* second);
   main()
  {
    PERSON *first,*second;
    first=(PERSON *)malloc(sizeof(PERSON));
    printf("Enter the first name\n");
    scanf("%s",first->name);
    second=(PERSON *)malloc(sizeof(PERSON));
    printf("Enter the second name\n");
    scanf("%s",second->name);

    if((compare_people(first,second)) == 0)
       printf("Two names are same \n");
    else
      printf("Two names are different\n");


   }

   int compare_people(PERSON* first, PERSON* second)
   {
    char firstName[32];
    char secondName[32];

    strcpy(firstName, first->name);
    strcpy(secondName, second->name);

    int returnVal = strcmp(firstName, secondName);
    return returnVal

   }

~