[英]Why do I get these warnings even though the headers are included?
GCC is spitting warnings, which make no sense for me. GCC 发出警告,这对我来说毫无意义。 I've the needed
stdio
library included.我已经包含了所需的
stdio
库。 The execution of this program works as expected and I got 100% points on EDX too.该程序的执行按预期工作,我在 EDX 上也获得了 100% 的分数。 Also what are these warnings about
size 6
and size 5
?还有关于
size 6
和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但是 gcc 正在抱怨这个
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 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.