GCC is spitting warnings, which make no sense for me. I've the needed stdio
library included. The execution of this program works as expected and I got 100% points on EDX too. Also what are these warnings about size 6
and size 5
?
Code so far:
#include <stdio.h>
#include <stdlib.h>
struct student {
char name[50];
int age;
struct student *next;
};
struct student *createStudent(char studentName[50], int studentAge);
struct student * append(struct student * end, struct student * newStudptr);
void printStudents(struct student *start);
void copyStr(char [], char []);
/* add other prototypes here if needed*/
int main(void) {
struct student *start, *newStudptr, *end, *tmp;
int ageP, ageR, ageM;
scanf("%d %d %d", &ageP, &ageR, &ageM);
start = createStudent("Petra", ageP);
end = start;
newStudptr = createStudent("Remi", ageR);
end = append(end, newStudptr);
newStudptr = createStudent("Mike", ageM);
end = append(end, newStudptr);
printStudents(start);
tmp = start->next;
free(start);
start = tmp;
tmp = start->next;
free(start);
free(tmp);
return 0;
}
struct student* createStudent(char studentName[50], int studentAge) {
struct student* newStud;
newStud = (struct student*) malloc(sizeof(struct student));
copyStr(studentName, newStud->name);
newStud->age = studentAge;
newStud->next = NULL;
return newStud;
}
struct student* append(struct student* end, struct student* newStudPtr) {
end->next = newStudPtr;
end = newStudPtr;
return end;
}
void copyStr(char source[], char target[]) {
int i = 0;
while(source[i] != '\0') {
target[i] = source[i];
i++;
}
target[i] = '\0';
}
void printStudents(struct student* start) {
struct student* ptr = start;
while(ptr != NULL) {
printf("%s is %d years old.\n", ptr->name, ptr->age);
ptr = ptr->next;
}
}
/* Place your function definitions here. Be sure to include the definitions for
createStudent() and append() as well as any other functions you created for
the previous tasks. */
The warnings:
:!gcc -Wall -std=c17 edx_PrintLinkedList.c -o bin/edx_PrintLinkedList
edx_PrintLinkedList.c: In function ‘main’:
edx_PrintLinkedList.c:22:13: warning: ‘createStudent’ accessing 50 bytes in a region of size 6 [-Wstringop-overflow=]
22 | start = createStudent("Petra", ageP);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
edx_PrintLinkedList.c:22:13: note: referencing argument 1 of type ‘char *’
edx_PrintLinkedList.c:45:17: note: in a call to function ‘createStudent’
45 | struct student* createStudent(char studentName[50], int studentAge) {
| ^~~~~~~~~~~~~
edx_PrintLinkedList.c:25:18: warning: ‘createStudent’ accessing 50 bytes in a region of size 5 [-Wstringop-overflow=]
25 | newStudptr = createStudent("Remi", ageR);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
edx_PrintLinkedList.c:25:18: note: referencing argument 1 of type ‘char *’
edx_PrintLinkedList.c:45:17: note: in a call to function ‘createStudent’
45 | struct student* createStudent(char studentName[50], int studentAge) {
| ^~~~~~~~~~~~~
edx_PrintLinkedList.c:28:18: warning: ‘createStudent’ accessing 50 bytes in a region of size 5 [-Wstringop-overflow=]
28 | newStudptr = createStudent("Mike", ageM);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
edx_PrintLinkedList.c:28:18: note: referencing argument 1 of type ‘char *’
edx_PrintLinkedList.c:45:17: note: in a call to function ‘createStudent’
45 | struct student* createStudent(char studentName[50], int studentAge) {
we dont know which lines you can change or not
But gcc is complainng about this
you say
struct student* createStudent(char studentName[50], int studentAge)
but then you do
start = createStudent("Petra", ageP);
which is trying to invoke it as a
struct student* createStudent(char studentName[6], int studentAge)
function
can you do
struct student* createStudent(char studentName[], int studentAge)
That will work
edit
YOu can also fix it like this
char name[50];
....
strcpy(name, "Petra");
start = createStudent(name, ageP);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.