First, you only allow for 3 names in you structure (name[3][20]) so copying 5 is going to do potentially bad things.
p->count++ increments the counter. There is no if is does, if it doesn't
Also, I'm confused by your comments:
... p->count will be 4. for this the loop will break cause(p->count==loop_count==5)...
p->count is either 4 or 5 - not both. I think what you are trying to do is use p-count as a loop terminator and that is fine. You need to allocate enough room for that in your structure or lower the expected maximum loop count. Perhaps something like:
#define MAX_NAMES 5
#define NAME_LENGTH 20
struct name_struct {
int count;
char names[MAX_NAMES][NAME_LENGTH];
};
// ... somewharere later in the code
for (p->count = 0; p->count < MAX_NAMES; p->count++) {
fgets(input_name,NAME_LENGTH,stdin);
strncpy (p->name[p->count], input_name, NAME_LENGTH);
}
The macro names add to the readability of the code and help to ensure that you don't introduce and off-by-one error somewhere.
L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7
The p-count value is incremented by the ++ operator. No check is made by strncpy at any time for the availability of space - you need to ensure that on your own. If you call the strncpy function the way you have things set up you will have an incremented counter if the call returns.
L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7
This:
strncpy(p->name[(p->count)++], q, 20);
Is roughly equivalent to this:
strncpy(p->name[p->count], q, 20);
++p->count;
I'm not sure where your confusion lies, but I'm all but certain you're overcomplicating things.
deceptikon
Challenge Accepted
3,456 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
I dont understand your question. The call happens (roughly) like this:
strncpy (p->name[(p->count)++], input_name, NAME_LENGTH);
- store value of
p->count somewhere (call that v1) - increment value of
p->count (p->count is now one more than it was in the previous step) - use
v1 (original p->count value) to index into p->name. Looks something like p->name[v1] - Use result of previous step as first argument to
strncpy
So the value is incremented previous to the actual call to strncpy but you can not access the variable until the call returns. The call itself uses the previous value of the variable because you are using the post increment operator. The story would be different if you were using the pre increment operator (placing the ++ before the variable).
The example I provided in my previous post removes the confusion entirely. If you are not certain how these things work it might be better to take an approach like that to avoid pitfalls until you understand things better.
L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7
thnx guys.those two comments made my confusions clear.that is all i wanted to know.
Cool. Actually, I think the function call entry sequence point is the most difficult of them to wrap your head around because often it's academic in nature and makes no apparent difference in practice.
All of the other sequence points are pretty straightforward, with the possible exception of the comma operator because not every use of a comma in C is the comma operator (what kind of existential mess is that?).
deceptikon
Challenge Accepted
3,456 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57