void Students_Absence_Report()
{
	int i;
	double presence[NO_OF_STUDS], absence[NO_OF_STUDS];

	for(i=0; i<NO_OF_STUDS; i++)
	{
		presence[i] = ((double)student[i].presentDays/14)*100;
		absence[i] = 100 - presence[i];
	}

	printf("\n\nStudents Presence/Absence Report\n");
	printf("--------------------------------\n");
	printf("    Name\t\t%%Presence\t%%Absence\n");	

	for(i=0; i<NO_OF_STUDS; i++)
	{
		printf("%-3d %-19s %.2f\t\t%.2f\n", student[i].studNo, strcat(student[i].surname, student[i].givenName), presence[i], absence[i]);
		
	}
	printf("\n******** END OF ATTENDANCE REPORT ********\n");
}

Anythings wrong in this function? everytime i do loopinh, the strcat will generate larger and larger name...anywhr to solve tis??

Dani AI

Generated

The behavior you saw is normal: strcat appends the source string onto the destination in place. Calling strcat(student[i].surname, student[i].givenName) inside the loop modifies the stored surname field itself. On the next iteration that already-extended surname is used as the destination again, so the name grows each time and you also risk overflowing the array (undefined behavior).

was right to suggest using a temporary buffer instead of mutating the struct member. Two safer options that avoid repeated appends are shown below.

/* print surname and givenName without concatenating */
printf("%-3d %-19s %-10s %.2f\t\t%.2f\n",
       student[i].studNo,
       student[i].surname,
       student[i].givenName,
       presence[i],
       absence[i]);
/* build a safe combined name with bounds checking */
char namebuf[64];
snprintf(namebuf, sizeof namebuf, "%s %s", student[i].surname, student[i].givenName);
printf("%-3d %-19s %.2f\t\t%.2f\n", student[i].studNo, namebuf, presence[i], absence[i]);

Practical tips: before concatenating, compute the required space (strlen(surname)+strlen(givenName)+2 for a space and null) and ensure the buffer is big enough or allocate it dynamically. If you reuse a temporary buffer, reset it each loop (or use snprintf which always writes a fresh string). Do not call strcat on the struct field itself unless that field was intentionally sized to hold the joined name and you have checked bounds. These changes will stop the growing-name symptom and avoid buffer-overflow bugs.

Recommended Answers

All 3 Replies

Please take a look at .

First parameter of strcat is the destination string, second one is the source string that will be appended to destination string.

e.g.
Consider the code piece below:

dest = "Destination,"
src = "Source,"
call strcat (dest, src)  // this method call will assign new value to dest variable.

After above call dest variable will contain "Destination,Source,"

If this is done over and over again, dest variable will be appended with src text.

To avoid this case you should do something like this:

char tempString[100];  // this should be done outside loop.
                       // Array length should be large enough to hold both surname
                       // and firstname.

//in for look do the following
strcpy(tempString, student[i].surname);
strcat(tempString, student[i].givenName);
printf("%-3d %-19s %.2f\t\t%.2f\n", student[i].studNo, tempString, presence[i], absence[i]);

Note: I have written the code from memory, I haven't tested it. Please bear with it :D

oh yea~~thx for ur info~~~i can done my programme d~

Please mark the issue as closed if it is resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.