-2

I've written code as below and have a problem. I want to add task names to my list but when I enter different names and try to display them, it shows me the lastest name I entered for each element in list. How can I correct? My codes has been written in in Turkish language but I am sure that you understand. (Also I translated in comment lines)

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

struct element   // element
{
    char *taskName;  // task name
    struct element *next;  // next
};

typedef struct element Element;
typedef Element *Elementptr;

Elementptr new_element(char *taskName)  // new element
{
    Elementptr element = (Element *)malloc(sizeof(Element));
    element->taskName = taskName;
    element->next = NULL;
    return element;
}

struct stack  // stack
{
    Elementptr top;  // top
};

typedef struct stack stack;
typedef stack *stackptr;

stackptr new_stack()  // new stack
{
    stackptr stack = (stack *)malloc(sizeof(stack));
    stack->top = NULL;
    return stack;
}

Elementptr top(stackptr c)  // top
{
    return c->top;
}

int stack_empty(stackptr c)  // empty stack
{
    if (c->top == NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void stack_add(stackptr c, Elementptr new)  // add stack
{
    new->next = c->top;
    c->top = new;
}

Elementptr stack_delete(stackptr c)  // delete stack
{
    Elementptr e = c->top;
    if (!stack_empty(c))
    {
        c->top = c->top->next;
    }
    return e;
}

void stack_write(stackptr c)  // write stack
{
    printf("\n\n*** Stack'te Bulunan Gorevler ***\n");
    Elementptr p;
    int sayac = 0;
    while (!stack_empty(c))
    {
        p = stack_delete(c);
        sayac++;
        printf("\nStack d. gorevi: s\n", sayac, p->taskName);
    }
}

int main()
{
    int devam;  // continue
    char gorevIsmi[32];  // task name
    bool dogruMu = true;  // isTrue
    stackptr test = new_stack();

    printf("-- Gorev addmeyi sonlandirmak icin \"157\" giriniz --\n");   // to stop program enter 157
    do 
    {       
        printf("\nenter task name: ");  // enter task name
        scanf("s", gorevIsmi);
        Elementptr new = new_element(gorevIsmi);
        stack_add(test, new);

        printf("\nDevam durumu giriniz (Rakam): ");
        scanf("d", &devam);
        if (devam == 157)
        {
            dogruMu = false;
        }

    } while (dogruMu == true);

    stack_write(test);
}
ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
KGT
  • 3
  • 3

1 Answers1

1

In yeni_eleman, you're storing gorevAdi into your structure without first copying it. When called from main, you are passing it a local array which is therefore shared across multiple instances of your struct (and becomes undefined upon return from main).

I suggest copying gorevAdi in yeni_eleman. Replace the assignment to eleman->gorevAdi with:

eleman->gorevAdi = strdup(gorevAdi);

This will allocate storage for the string with malloc, copying gorevAdi into it.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41