hello guys how properly push,pop,display a char array into another char array, when come to int i can do but in char array its to hard to analyze.. pls guy need some help. thnx in advance

Recommended Answers

All 3 Replies

It depends on how you're storing the items of the stack. Personally, I would use an array of structures:

struct item {
    char a[ITEM_SIZE];
};

struct item stack[STACK_SIZE];
size_t top = 0;

With that setup, pushing and popping are a simple matter of copying the contained array:

void push(const char a[], size_t size)
{
    if (top >= STACK_SIZE)
        return;

    if (size > ITEM_SIZE)
        size = ITEM_SIZE;

    memcpy(stack[top++].a, a, size);
}

void pop(char dst[], size_t size)
{
    if (top == 0)
        return;

    if (size > ITEM_SIZE)
        size = ITEM_SIZE;

    memcpy(dst, stack[--top].a, size);
}

thnx narue, but can you show me how to exactly apply this code with simple input and output?

char a[ITEM_SIZE];

if (fgets(a, sizeof a, stdin) != NULL)
    push(a, sizeof a);

pop(a, sizeof a);
puts(a);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.