Thread: Text Modul | dynamic struct |

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    2

    Text Modul | dynamic struct |

    Hello,
    I will try to explain my question. I am pretty new in C. And i want to program a text modul.
    Using dynamic struct and dynamic arrays. it is not a homework its nor more like preperation for a test.


    text stuctur: length without \0
    dynamic array from type char and \0 should be save


    Text prototype:
    create(): parameter size, create a new text with a dynamic array, size define size.
    it should have more place bcs of \0.
    init(): parameter c string, with new size, text needs to be create()
    data needs to copied by strlen() and strcpy(), it has to be returned
    kill(): ofc delete dynamic array, set size on 0 and the pointer on Null
    add(): two text struct on value: both text gets to one text.
    Firstly the sum of both lengths is the new size. create() makes a new text
    then copy the data. Finally return.
    print(): text struct on value, data needs to get print.

    The Code:

    Code:
    struct text {
     int length;
     char *zeichen;};
     struct text create(int size);
     struct text init(char string[]);
     void kill(struct text *rS);
     struct text add(struct text a, struct text b);
     void print();
    
    struct
     text create(int size) {
     struct text s = { NULL, size }; s.zeichen = malloc((size + 1) * sizeof(char));
     if (s.zeichen != NULL) {
     s.length = size;}
     return s;}
    
     void kill(struct text *rS) {
     if (rS->zeichen != NULL)
     free(rS->zeichen); rS->zeichen = NULL; rS->length = 0;}
    
     struct text init(char zeichen[]) { // copy text and a new length
     struct text newtext = create(strlen(zeichen)); strcpy(newtext, zeichen); //copy the new  text
     return newtext;}
    
     struct text add(struct text a, struct text b) {
     struct text longtext = create(a.length + b.length);  //size of both text
     strcat(longtext, a);           // text a need to get add to b
     return b;}
    
     void print(struct text q) {
     printf("%s", q.zeichen);}
    Last edited by Salem; 11-03-2021 at 10:45 AM. Reason: Removed the list

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,430
    Well it's pretty awful to read, being so compressed and all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2021
    Posts
    2
    Never mind I got it now. Ty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array struct
    By trep in forum C Programming
    Replies: 10
    Last Post: 05-23-2012, 10:20 AM
  2. dynamic array of struct within dynamic array of struct
    By explodecomputer in forum C Programming
    Replies: 3
    Last Post: 08-03-2010, 03:25 AM
  3. dynamic array in STRUCT
    By cfdprogrammer in forum C Programming
    Replies: 15
    Last Post: 08-04-2009, 09:57 AM
  4. how to know dynamic size of struct
    By unikgila in forum C Programming
    Replies: 16
    Last Post: 10-20-2008, 08:56 PM
  5. Dynamic struct alignment?
    By matthew180 in forum C Programming
    Replies: 7
    Last Post: 06-15-2007, 08:34 AM

Tags for this Thread