Thread: stringBuff

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    stringBuff

    Hello,

    i try to write a stringBuffer in C. For this task i want to use linked lists. But i'm not sure how to create stringbuffers, furthermore i don't know how they have to look like.

    So my idea was the following:
    1)
    First i defined a linked list and named it stringbuffer.
    for instance

    Code:
    struct stringbuffer{
    char a;
    struct stringbuffer *next;
    }
    2)
    Now i think that my actual stringbuffer can be pointer, which point on such a struct stringbuffer, i.e. something like

    struct stringbuffer *firstStringbuffer;

    Is this right? Can i do it like this?


    It would be nice, if you can help me.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's a string buffer?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by HungryMan View Post
    Hello,

    i try to write a stringBuffer in C. For this task i want to use linked lists. But i'm not sure how to create stringbuffers, furthermore i don't know how they have to look like.
    Normally in C a "string buffer" would be considered to be a char array.

    Code:
    char *buffer = malloc(4096);
    Doing it with a linked list of chars is just plain crazy -- is there a reason you want to do this?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    6
    Quote Originally Posted by MK27 View Post
    Doing it with a linked list of chars is just plain crazy -- is there a reason you want to do this?
    Yes, a friend has to do it this way for homework and i want to help him.

    But i think there is also a good reason for this.
    Because you want to store arbitrary many and different strings.

    So it can be better not to use arrays, since they are stored in "blocks". I think this is the reason....

    But i don't know what stringbuffer's actually are in a C programm? How can we use linked lists to define a stringbuffer.
    We can create a pointer, which points on a struct stringbuffer.
    But how do we "store" and "delete" stringbuffers, when we define stringbuffer's this way?

    I have no odea sorry,
    Last edited by HungryMan; 05-08-2011 at 03:50 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by HungryMan View Post
    So it can be better not to use arrays, since they are stored in "blocks". I think this is the reason....,
    Why do you think this is better? (I can see if it's a homework assignment and you have to make a linked list.) Instead of worrying about blocks being the wrong size, you can simply use realloc and make them whatever size you want. Also, if we assume a pointer is 4 bytes, that means for every node in your list, you could instead be storing 5 characters in the same amount of space.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    6
    yeah, its a homework!!! I don't know why they have to do this. Perhaps just to learn linked lists.
    Last edited by HungryMan; 05-08-2011 at 04:11 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Start here then: Cprogramming.com Tutorial: Linked Lists


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by HungryMan View Post
    Yes, a friend has to do it this way for homework and i want to help him.

    But i think there is also a good reason for this.
    Because you want to store arbitrary many and different strings.
    Just a thought... if you know less about C than your friend, are you really helping him?

    In any case a linked list of individual characters is a patently bad idea. As has already been pointed out you will end up using between 3 and 5 times as much memory and you won't have a useable string in the entire mess.

    In C a string is an array of characters, stored contiguously, ending in a null (0) character. All of C's library functions (strcpy(), strlen(), etc.) count on this to work... By storing the data in an unexpected way you totally trash any hope of using any of the standard library functions on your data.

    You may want to look into using a standard char array as your input buffer then a linked list to store each string ... perhaps placing a line of text in each struct. At least then you would preserve the ability to use string functions on a line by line basis.

    Code:
    typedef struct tLines
      { char *Line;
         void *Next }
       Lines;
    
    int main (void)
      { char Buffer[128];  // line buffer
        Lines *first = NULL, *current= NULL;
    
        While (fgets(Buffer,127,stdin))
            {  // add new line;
               current->Next = malloc(sizeof(lines));
               current = current->Next;
               current->Next = NULL;
               current->Line = strdup(Buffer);  
               // diagnostic
               printf("%s",current->Line); }
    
    
       return 0; }
    This is just an example of form, it won't compile, don't even bother trying... but I think you can get the idea from it.
    Last edited by CommonTater; 05-08-2011 at 08:04 AM.

Popular pages Recent additions subscribe to a feed