Function in a text buffer

This is a discussion on Function in a text buffer within the C Programming forums, part of the General Programming Boards category; hi! i'm new in this programming stuff and now i need to make a text buffer with struct: Code: typedef ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    Function in a text buffer

    hi! i'm new in this programming stuff and now i need to make a text buffer with struct:
    Code:
    typedef struct textbuffer *TB;
    struct textbuffer{
    char* text; //it's gonna be a string
    TB next;
    };
    now the problem is i need to write a function:
    Code:
     
    TB newTB(char text[]);
    when newTB is called, a list will be created because the input will have more than one line of text and we need to initialise it using char text[]. and every time it find '\n' the next text will be stored in the next node, and terminated with '\0'

    can anyone give me the c prog for this one? i'm really slow at understanding the adt!! what is adt anyway? thanks!

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by wellsper View Post
    can anyone give me the c prog for this one?
    No, but I can help you getting it done. First of all, you need to allocate memory for the string to store to and I suggest you read this thread carefully and also follow the link I've posted in my post there. As soon as you think you have an idea how to write the code yourself, post your idea and we'll help you further.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    so...

    wow tks a lot for the instant reply! i appreciate it so much!! so can i write it like this?
    Code:
    TB newTB(char text[])
    {
            char *text = malloc( sizeof (*text) *strlen(text) + 1)
            return TB *newnode = (TB*) malloc(sizeof(TB));       
    }
    but why the type of the func is TB? how's the func should be work? sorry but i'm really noob at this subject. tks for ur passion.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Not exactly. text is the function argument, so no need to allocate space for it. What you need is to create a new struct TB and allocate memory for the text field of the structure. Finally, copy the text given as argument into the structure and return it:
    Code:
    TB newnode;
    newnode.text = malloc(somelength * sizeof(*(newnode.text)));
    strcpy(newnode.text, text);
    return newnode;

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    1
    Hey sorry ..i got a question
    The code that Koni put ..
    I dont understand how that will allow each line to be stored into different nodes
    because each lines end with '\n' and the whole this is terminated by '\0'.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    It doesn't, I don't think he/she realised that 'TB' is a pointer to a structure.

    You just need to allocate space for the structure before you allocate space for the string. Also note that when you access members of a pointer to a structure, you use '->' instead of '.'.

    Something like:
    Code:
    TB newnode;
    newnode = malloc(sizeof(struct textbuffer));
    //allocate space for string and such here, then return newnode, as per KONI's code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 09:09 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21