1

look guys, i have the code above

#include <stdio.h>
#include <string.h>
#define MAX 5
typedef struct Estudante{
    char nome[MAX];
    char apelido[MAX];
    char residencia[MAX];
    int telefone;
}TEstudante;
int equalEnty(TEstudante *estudante_1,TEstudante *estudante_2);
int comesFirst(TEstudante *estudante_1,TEstudante *estudante_2);
int main(){

    TEstudante estudantes[2];
    strcpy((estudantes[0]).nome,"Angelo");
    strcpy(estudantes[0].apelido,"Pelon");
    strcpy(estudantes[0].residencia,"3B");
    estudantes[0].telefone = 33813001;

    strcpy(estudantes[1].nome,"Angelo");
    strcpy(estudantes[1].apelido,"Pelon");
    strcpy(estudantes[1].residencia,"3C");
    estudantes[1].telefone = 33813001;

    printf("%d",comesFirst(&(estudantes[0]),&estudantes[1]));

    return 0;
}

int equalEnty(TEstudante *estudante_1,TEstudante *estudante_2){

    if(strcmp((*estudante_1).nome,(*estudante_2).nome)!=0)
        return 0;
    if(strcmp((*estudante_1).apelido,(*estudante_2).apelido)!=0)
        return 0;
    if(strcmp((*estudante_1).residencia,(*estudante_2).residencia)!=0)
        return 0;
    if((*estudante_1).telefone != (*estudante_2).telefone)
        return 0;
    return 1;
}

int comesFirst(TEstudante *estudante_1,TEstudante *estudante_2){
    printf("%s %s",(*estudante_1).nome,(*estudante_2).nome);
    return strcmp((*estudante_1).nome,(*estudante_2).nome);
}

Then when i printf the content insite the comesFirst function its are showing something like that:

AngeloPelon3B AngeloPelon3C-1

Why if i added separetly the strins to the structs areas !?

  • 2
    Try `#define MAX 80`. – Peter - Reinstate Monica Jun 23 '14 at 23:43
  • 4
    This doesn't completely answer your question, but you defined the `nome` field as having length 5 and then copied a 6 character null terminated string into it. That's not going to fit. – murgatroid99 Jun 23 '14 at 23:43
  • 1
    @murgatroid99 But that does answer it. – Peter - Reinstate Monica Jun 23 '14 at 23:44
  • 1
    Yeah, the main problem is that the char arrays are too small to hold the data, so stuff gets corrupted. C does not give you any sort of error or warning when you do this. – Hot Licks Jun 23 '14 at 23:47
  • In a complete answer I probably would have explained null terminators in more detail, and what exactly `strcpy` does. Plus, I'm still trying to figure out why the "o" isn't getting stripped off of "Angelo" in the output. – murgatroid99 Jun 23 '14 at 23:47
  • @murgatroid99 - Alignment of variables within a structure. – Hot Licks Jun 23 '14 at 23:48
  • Oh, of course. But then what's in between the "o" and the "P"? – murgatroid99 Jun 23 '14 at 23:49
  • 1
    As another issue, don't use an `int` to encode a telephone `number`. Phone numbers are not actually numbers but digit strings. Why? A leading `0` is meaningful; you can't do meaningful arithetic with them. – Keith Jun 23 '14 at 23:53
  • @Keith in many areas the leading 0 can be inferred from context (e.g. if all numbers are 2-digit area code plus 7-digit number, then an 8-digit value must have had a leading zero). Although I agree that using a stirng is better except in situations where memory/storage use becomes an issue. – M.M Jun 24 '14 at 00:14
  • Note that you can [initializes structs directly](http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c) instead of assigning each properties separately like that – phuclv Jun 24 '14 at 02:06

1 Answers1

1

You need 7 characters in an array to copy "Angelo" to it, not five. The length of the string is 6 and you need an extra character for the terminating null character.

You are writing over memory of other fields when you execute:

strcpy((estudantes[0]).nome,"Angelo");

Define MAX go be 1 more than the maximum length of the strings you are going to copy into nome, apelido, and residencia.

R Sahu
  • 204,454
  • 14
  • 159
  • 270