简体   繁体   English

为什么即使包含标头我也会收到这些警告?

[英]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 6size 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.

相关问题 一个简单的hashing-array-loop,即使有效,我也会收到错误警告,为什么呢? - A simple hashing-array-loop, I get error warnings even though it works, why is that? 为什么我会收到此错误? “[变量]没有命名类型”在 C++ 中用于 std::string 数组,即使它已包含在同一个 scope 中 - Why do I get this error? “[variable] does not name a type” in C++ for std::string array even though it has been included and is in the same scope 即使我有一个检查条件,为什么我仍然超出列表索引范围? - Why do I get list index out of range even though I've kept a condition to check so? 为什么即使指针看起来有效,我也得到EXC_BAD_ACCESS? - Why do I get EXC_BAD_ACCESS even though the pointer appears to be valid? 为什么即使我将它转换为数组,我的 querySelectorAll 也不起作用? - Why is my querySelectorAll not working even though I converted it to an array? 未定义索引通知,即使我可以获取数据 - Undefined index notice even though i can get the data 为什么即使我已将数字转换回字符串,我也不能将数字推送到我的字符串数组中? - Why can I not push a number into my array of strings even though I have converted the number back to a string? 为什么我仍然收到 InputMissmatchException 即使我有一个 catch 语句 - Why am I still getting a InputMissmatchException even though i have a catch statement 为什么即使我用 C 语言扫描小数字,我的数组也会打印大数字 - Why I my array prints large numbers even though i am scanning the small numbers in C language 为什么在初始化后仍收到消息,表明我的数组为空? - Why do I get a message that my array is empty even after initializing it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM