Implementing a stack of strings in C


 
Thread Tools Search this Thread
Top Forums Programming Implementing a stack of strings in C
# 1  
Old 09-28-2012
Implementing a stack of strings in C

I'm trying to create a simplified version of dc to learn from. The first thing I notice is that the main stack stores strings. How would I implement a string stack in C? How is something like this:

Code:
struct StringStack
{
   const unsigned int pysicalSize;     // physical size of the stack
   const char ** const basePtr;        // cannot change bottommost pointer or pointee
   char **stackPtr;                    // top of stack
   unsigned int logicalSize;           // number of elements in the array
};

Thoughts?

---------- Post updated at 06:33 PM ---------- Previous update was at 04:59 PM ----------

Actually I don't think I need the stackPtr because I can just use the basePtr + the logicalSize.
# 2  
Old 10-04-2012
But where is your actual array that will store the strings as they are entered...
# 3  
Old 10-04-2012
That would be basePtr, one would think.

You can't make it a const as you'll need to change it at least once, when you allocate memory to it. Smilie
# 4  
Old 10-04-2012
Come to think of it, I wrote a simple string list which can easily be modified for what you need. Just one function missing...

Code:
// strlist.h

#ifndef __STRLIST_H__
#define __STRLIST_H__

typedef struct strlist
{
        int size;
        int pos;
        char *list[1]; // Actually ends up being larger than one element because of malloc() size
} strlist;

#ifdef __cplusplus
extern "C" {
#endif

strlist *strlist_create(void);
strlist *strlist_resize(strlist *s);
void strlist_free(strlist *s);
strlist *strlist_append(strlist *s, const char *str);

char *strlist_pop(strlist *s);

#ifdef __cplusplus
}
#endif

#endif/*__STRLIST_H__*/

Code:
//strlist.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "strlist.h"

#define STRLIST_MINSIZE 16
#define STRLIST_FREE(X) (((X)->size)-((X)->pos))

strlist *strlist_create(void)
{
        strlist *s=malloc(sizeof(strlist)+(sizeof(char *)*STRLIST_MINSIZE));
        s->pos=0;
        s->size=STRLIST_MINSIZE;
        return(s);
}

strlist *strlist_resize(strlist *s)
{
        strlist *n=realloc(s, sizeof(strlist) +
                (sizeof(char *)*(s->size<<1)));

        if(n == NULL)
                return(NULL);

        n->size<<=1;

        return(n);
}

void strlist_free(strlist *s)
{
        int n;
        for(n=0; n<s->pos; n++)
                free(s->list[n]);
        free(s);
}

strlist *strlist_append(strlist *s, const char *str)
{
        if(STRLIST_FREE(s) <= 0)
        {
                strlist *n=strlist_resize(s);
                if(n == NULL)
                        return(NULL);
                s=n;
        }

        s->list[s->pos++]=strdup(str);
        return(s);
}

char *strlist_pop(strlist *s)
{
        if(s->pos <= 0) return(NULL);
        return(s->list[--s->pos]);
}

If it has to enlarge the size of the stack, then the pointer may change, so pay attention to the return value of the functions when they return strlist *'s.

[edit] Oh yeah. And if you pop() a string, you have to free() the pointer it returns.

Last edited by Corona688; 10-05-2012 at 12:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Kernel Stack vs User Mode Stack

Hi, I am new to the linux kernel development area. I want to know what is the difference between kernel mode stack and user mode stack? Does each process has a user mode stack and a kernel mode stack?? Or Each process has a user mode stack and there is only one kernel mode stack that is shared by... (4 Replies)
Discussion started by: saurabhkoar
4 Replies

2. Shell Programming and Scripting

Need help in implementing expect

Hello All, I am trying a shell script for automatically login to test servers and pulling the output of top command from all using expect. ----snippet of code --- #!/usr/bin/expect -f #!/bin/bash server1=10.251.222.51 server=("$server1") i=1 for exp_server in ${server}; do expect -c... (3 Replies)
Discussion started by: Renjesh
3 Replies

3. Shell Programming and Scripting

Need help in implementing logic

i have following input file... 00290002STDR000000000000000000000000000EOD END TRANSACTION ^@^@^@^@^@^@^@^@^@^@^@^@^ 00299998STDR070000000007000000000000000STANDING DEBITS ^@^@^@^@^@^@^@^@^@^@^@^@^... (1 Reply)
Discussion started by: sagarrd
1 Replies

4. Shell Programming and Scripting

Help with implementing logging

I'm trying to add logging to an existing script which echos a number of lines to the screen. I've added a switch to the script that is going to suppress much of this output and put it in a file instead. The way I envisioned it was like this: $log would be set to either "" or the log files... (8 Replies)
Discussion started by: cheetobandito
8 Replies

5. Shell Programming and Scripting

Implementing Password

I am trying to implement a login screen to the following code how would i go about doing so. I have try to place the password in a variable using if statements which would usually work but as i have the system in a while loop i think i need to find another method. #!/bin/bash #Filename:... (4 Replies)
Discussion started by: warlock129
4 Replies

6. Programming

Implementing the TCP stack

Hello, I am trying to implement TCP protocol in C. I have read the RFC for TCP and have knowledge about it. But I am stuck in coding. Are ther any reference links, code snippets available for reference? (1 Reply)
Discussion started by: Harsh
1 Replies

7. Programming

what is stack winding and stack unwinding

helo can u tell me what do you mean by stack winding and stack unwinding Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

8. Programming

Implementing a shell in C

Hi, I am implementing a shell in C, with the following problem... Suppose the shell is invoked from the command line as >> myshell < test.in > test.out 2>&1 I have to execute the commands in test.in and redirect them to test.out How does one detect in the main function that the shell... (1 Reply)
Discussion started by: jacques83
1 Replies
Login or Register to Ask a Question