2 votes

Pourquoi ai-je ces avertissements alors que les en-têtes sont inclus ?

GCC crache des avertissements, qui n'ont aucun sens pour moi. J'ai les stdio bibliothèque incluse. L'exécution de ce programme fonctionne comme prévu et j'ai obtenu 100% de points sur EDX également. Par ailleurs, que signifient ces avertissements size 6 y size 5 ?

Le code jusqu'ici :

#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. */

Les avertissements :

:!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) {

1voto

pm100 Points 8303

Nous ne savons pas quelles lignes vous pouvez changer ou non

Mais gcc se plaint de ceci

vous dites

   struct student* createStudent(char studentName[50], int studentAge)

mais ensuite vous le faites

   start = createStudent("Petra", ageP);

qui essaie de l'invoquer comme un

  struct student* createStudent(char studentName[6], int studentAge)

función

pouvez-vous faire

  struct student* createStudent(char studentName[], int studentAge)

Ça va marcher.

modifier


Vous pouvez aussi le régler comme suit

  char name[50];
  ....
  strcpy(name, "Petra");
  start = createStudent(name, ageP);

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X