Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ERROR IN C with STRUCT, POINTER, SCANF_S?

This is my code…. I have some problem with pointers, scanf_s and struct Persona.

ERRORS in line 51, 53, 59 — > C6011 , C6064, C6067.

I don’t know how to resolve it… I must put Name and Surname into lista_nomi and i don’t know if the method of putting data that i use, is the right one!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please HELP ME , i’m a begginer on programming in C language.

#include <stdio.h>
#include <stdlib.h>

/*
    Sia dato un insieme di nomi e cognomi di persone. Si scriva un programma in linguaggio
    C che raggruppi le persone con ugual cognome, stampando di seguito tutti i nomi delle
    persone che hanno lo stesso cognome.
    I nomi ed i cognomi devono essere letti da tastiera, come nell’esempio, finché non viene
    letto il nome FINE LISTA. L’ordine con cui si stampano i nomi ed i cognomi non è importante.

*/

#define MAX_PEOPLE 100

struct persona
{
    char* nome[30];
    char* cognome[30];
};

typedef struct persona Persona;

int main(void)
{
    int i;

    do
    {
        printf("Inserisci numero di persone da registrare!\n->");
        scanf_s("%d", &i);
        
        if (i <= 0 || i > MAX_PEOPLE)
        {
            printf("Numero inserito non valido.\nRANGE -> [1 to %d]", MAX_PEOPLE);
        }
    } while (i <= 0 || i > MAX_PEOPLE);
    
    Persona * lista_nomi = (Persona *) malloc(i * sizeof(Persona));

    if (lista_nomi == NULL)
    {
        printf("Errore nell' inizializzazione dell'array lista_nome");
    }

    // Inserimento dei nomi e cognomi

    for (int e = 0; e < i; e++)
    {
        printf("\nInserisci %d persona\n", e + 1);
        printf("\tInserisci Nome -> ");
        scanf_s("%s", &lista_nomi[e].nome);
        printf("\tInserisci Cognome -> ");
        scanf_s("%s", &lista_nomi[e].cognome);

    }

    for (int j = 0; j < i; j++)
    {
        printf("Elemento %d -> %s %s", j, lista_nomi[j].nome, lista_nomi[j].cognome);
    }

    free(lista_nomi);

    return 0;
}

>Solution :

You need to supply one more argument that specifies the size pf the character arrays. Also remove the address of operator. For example

    printf("\tInserisci Nome -> ");
    scanf_s("%s", lista_nomi[e].nome, ( rsize_t )30 );
    printf("\tInserisci Cognome -> ");
    scanf_s("%s", lista_nomi[e].cognome, ( rsize_t )30);

Also you should add the new line character '\n' in this call of prontf

printf("Elemento %d -> %s %s\n", j, lista_nomi[j].nome, lista_nomi[j].cognome);

Pay attention to that your program continue to work even when memory for the array was not allocated.

if (lista_nomi == NULL)
{
    printf("Errore nell' inizializzazione dell'array lista_nome");
}

that can results in undefined behavior.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading