+ 1

c program bug

I need help with this code please, the code is supposed to read the info of employee and then print it, the info is printed successfully except for the first member of the the object which is the name, the name is not printed. https://code.sololearn.com/cCAuz8r77kJ5/#c

7th Apr 2019, 10:00 AM
Joseph Safwat Sobhy
Joseph Safwat Sobhy - avatar
12 Réponses
+ 4
~ swim ~ Thank you for kind assistance 👍 I wonder where the OP is now ... A bit off, just hours back I saw someone posted a code in a thread, whose author was also doing structure member modification, though in that code the author used char* as a member. The code works even with the char* member being placed as the first member. Does this case have anything to do with alignment or something? like what Babak covered in his feeds posts. For the record, I'm still not getting the alignment rules clearly. The said thread with the code here: https://www.sololearn.com/Discuss/1752394/?ref=app
9th Apr 2019, 5:40 PM
Ipang
+ 3
I tried something but it didn't work, I think ~ swim ~ can help you with this.
9th Apr 2019, 2:55 PM
Ipang
+ 3
~ swim ~ Let's just wait a little here before the OP came and say something, sorry, I didn't mean to get you in deep on this 😁 I also did try strcpy in the `reademp` function, made a temp string buffer to read, then copy it over to structure member, the funny thing was, in that function it worked, and I can print the `x->name`, but back at the `printemp` it's back to empty. Still thanking you for this anyways.
9th Apr 2019, 6:24 PM
Ipang
+ 3
~ swim ~ I understand the purpose, what I'm not sure about was the ordering of structure members to make it an optimal package (if I may say so) to read/write. Don't worry, I'll get through it in time 😁
9th Apr 2019, 6:42 PM
Ipang
+ 3
~ swim ~ Bravo! 👏👏👏 I missed that too, gotten too accustomed with int and "%d" that I skipped it, small thing that makes big difference 8-O Lesson learned: Careful with format specifiers.
9th Apr 2019, 7:45 PM
Ipang
+ 2
Yea, I also tried fiddling around with this one - doing print statements in the reademp() function about the value of x->name (and extra, strlen(x->name)) printing after every scanf() call gave me this observation: The name is saved and prints when 1 less of the struct’s ints are inputted (i.e. you input everything bar the last int value: deduc. If you remove deduc from the struct, input everything bar the next last int value: bonus). Providing input for the last int meant I saw the string for x->name change from a stored value to blanking I tried to look into using malloc for the pointer but I did something wrong I’m intrigued to know how to solve this!
9th Apr 2019, 4:21 PM
Jenine
+ 2
~ swim ~ ohhh that was it! I would not have got that 😅
9th Apr 2019, 5:25 PM
Jenine