0

i have a problem with adding an string type inside of a node list.

I am using the command strncpy(), but i am not sure how this works with the pointer.

This is the code block i have now.

My specific problem is in the function "Insertar" because it expects a type char *variable but i an inserting just a char.

#ifndef _LINK_LIST_H_
#define _LINK_LIST_H_

#include <stdio.h>


typedef struct
{
    int dia;
    int mes;
    int ano;
}fecha;

struct NODEL
{
    float valor;
    char variable [10];
    int resol;
    fecha f;
    struct NODEL* nextNode;
};

typedef struct NODEL LISTNODE;
typedef LISTNODE * LISTNODEPTR;

void insertar(LISTNODEPTR * lista, float newvalor, char *variable, int newresol, 

#endif


void insertar(LISTNODEPTR * lista, float newvalor, char *variable, int newresol, int newdia, int newmes, int newano)
{
    LISTNODEPTR newPtr, prevPtr, currPtr;
    newPtr = malloc(sizeof(LISTNODE));
    if(newPtr != NULL)
    {
        newPtr->valor = newvalor;
        strncpy(newPtr->variable,newPtr->variable,10);
        newPtr->resol = newresol;
        newPtr->f.dia = newdia;
        newPtr->f.mes = newmes;
        newPtr->f.ano = newano;
        newPtr->nextNode = NULL;

        prevPtr = NULL;
        currPtr = *lista;

        while(currPtr != NULL && newvalor > currPtr->valor)
        {
            prevPtr = currPtr;
            currPtr = currPtr->nextNode;
        }


        if(prevPtr == NULL)
        {
            newPtr->nextNode = *lista;
            *lista = newPtr;
        }
        else
        {
            prevPtr->nextNode = newPtr;
            newPtr->nextNode = currPtr;
        }
    }
    else
    {
        printf("Elemento no insertado, no hay memoria disponible\n");
    }
}


int main(void)
{
    LISTNODEPTR data_base;

    float valorADC;
    char varIngresada;
    int resolucion;
    int diaIng;
    int mesIng;
    int anoIng;

    printf("Valor de ADC: ");
    scanf("%f", &valorADC);
    fflush(stdin);

    printf("Tipo variable 'Presion'/'Temperatura'/'Aceleracion': ");
    scanf("%s", &varIngresada);
    fflush(stdin);

    printf("Resolución: ");
    scanf("%d", &resolucion);
    fflush(stdin);

    printf("Fecha (dia): ");
    scanf("%d", &diaIng);
    fflush(stdin);

    printf("Fecha (mes): ");
    scanf("%d", &mesIng);
    fflush(stdin);

    printf("Fecha (año): ");
    scanf("%d", &anoIng);
    fflush(stdin);

    insertar(&data_base, valorADC, varIngresada, resolucion, diaIng, mesIng, anoIng);
    imprimir(data_base);
}
frlan
  • 6,950
  • 3
  • 31
  • 72
  • 1) `char variable [10];` (Requires a size of **12** or more)--> `char variable [16];`, `char varIngresada;` --> `char varIngresada[16];`.. `scanf("%s", &varIngresada);` --> `scanf("%15s", varIngresada);` – BLUEPIXY Oct 08 '17 at 01:46
  • `fflush(stdin);` is NOT doing what you think it is. See [**fflush(stdin); is undefined behaviour, don't do it.**](https://stackoverflow.com/a/38325926/2173917) – David C. Rankin Oct 08 '17 at 01:56
  • `strncpy(newPtr->variable,newPtr->variable,10);` change this to `strncpy(newPtr->variable,variable,10);` – H.S. Oct 08 '17 at 06:32
  • Initialize database pointer to NULL in main --> `LISTNODEPTR data_base = NULL` becaue in insertar() you are assigining `currPtr = *lista;` and then checking `currPtr != NULL`. – H.S. Oct 08 '17 at 06:43

0 Answers0