structures and pointers

structures and pointers

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

here is the problem

when i create structure

Citat:
struct emp {

int ID;
char *name;
char *surname;
};

typedef struct emp employee;

void new_employee( employee *new, int counter );
void print_employee( employee *print, int counter2);

int main(int argc, char *argv[]){
int counter;
int counter2;
char c;
employee clen[100];


for(counter=0; counter < 100; counter++){
printf("enter new employee\n");
new_employee(clen, counter);

printf("\nenter new employee? ");
fflush(stdin);
scanf("%c", &c);
if(c != 'd')
break;
}

for(counter2=0; counter2 <= counter; counter2++){
printf("\n\nprint employee");
print_employee(clen, counter2);


printf("\nsee another employee? ");
fflush(stdin);
scanf("%c", &c);
if(c != 'd')
break;

}

return 0;
}



void new_employee( employee *new , int counter){

printf("ID: ");
fflush(stdin);
scanf("%d", &new[counter].ID);
printf("\nname: ");
fflush(stdin);
scanf("%s", &new[counter].name);
printf("\nsurname: ");
fflush(stdin);
scanf("%s", &new[counter].surname);

}


void print_employee( employee *print, int counter2){

printf("\nemployee No %d", print[counter2].ID);
printf("\nname %s", &print[counter2].name);
printf("\nsurname %s", &print[counter2].surname);

}



when i enter name: jim and surname: john , it prints me like this, name:jimjohn and surname: john.

any help??

you can write in serbian



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • Pridružio: 18 Apr 2003
  • Poruke: 5001
  • Gde živiš: Beograd

Based on the output "name:jimjohn" i would say that name is not finished with zero. But the real problem is that you never initialize your dynamic char arrays inside emp structure.
Before using scanf("%s", &new[counter].name); and scanf("%s", &new[counter].surname); you have to allocate memory to it: new[counter].name = new char(length_of_name);



offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

so how would you inject in the code above?

Dopuna: 24 Dec 2007 13:58

i tried with this
new[counter].name = new char(20 * sizeof(char));

but still doesnt work...

Dopuna: 25 Dec 2007 13:20

problem solved.

because the pointer is a 4 bytes = 4 characters and let's say the name pointer is at 0x0000-0x0004 address, the next pointer surname start at 0x0005-0x0008. if in the name place i type 4 character, then there is no place for the null character, so it continious to print untill to the nulll character and that is in the surname pointer.

so i replace char *name with char *name[5], so in this address i can place 20 characters + null character, because 5 * 4 (4 bytes is a pointer) = 20

offline
  • Pridružio: 17 Mar 2004
  • Poruke: 293
  • Gde živiš: UK

Citat:because the pointer is a 4 bytes = 4 characters and let's say the name pointer is at 0x0000-0x0004 address, the next pointer... ... because 5 * 4 (4 bytes is a pointer) = 20

Ugh!

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

am i wrong in something? Smile

offline
  • Pridružio: 26 Jul 2004
  • Poruke: 1472
  • Gde živiš: Vojvodina,Zrenjanin

Hmm..., this is a C not C++ program.In this program new is variable!!!
You should rename this if you try to compile this program in c++ compiler.There were a lot of errors when i try this in VS 2008 expres until I rename this "new" in something else. Wink

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

i didnt say that this is a c++ program.
the wrong is in that post , but in the real source code, new pointer is written under other name.... i was changing the names of variables,so you can understand better.

offline
  • Pridružio: 18 Apr 2003
  • Poruke: 5001
  • Gde živiš: Beograd

RutiX ::char *name[5], so in this address i can place 20 characters + null character, because 5 * 4 (4 bytes is a pointer) = 20

With char *name[5] you have created five char pointers and not array of five characters, technically you wont get an error because you have allocated enough space for 20 characters, but that is not a good way to be programming. You should have created you structure like this:
struct emp {    int ID;    char name[21];    char surname[21]; };
that way you have 20 characters to input name and 20 characters to input last name.

offline
  • Pridružio: 30 Sep 2006
  • Poruke: 137

i know, but i wanted to use pointers. nvm, problem solved

Ko je trenutno na forumu
 

Ukupno su 543 korisnika na forumu :: 2 registrovanih, 3 sakrivenih i 538 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3028 - dana 22 Nov 2019 07:47

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: cole77, hyla