samedi 27 juin 2015

Appending two strings without str functions

I'm having an issue trying to figure out how to append the char pointer c to the existing Struct String. I want to be able to take in input as such (considering a predefined Struct with the value of stuff being "Hello") append(test,"world") When I try to use strcat and strcpy I get an error because the structure String is not a valid type to be used with this function.

How do I append without the use of the str functions?

I currently have code that declares a structure and sets stuff to be the value of the contents of the structure in this case hello I enter my function and check if the data the person is passing is not null. I create a new String Struct called append and realloc memory to the new size of the previous "stuff" plus the value of *c. Should I use a for loop to get the contents of *c at the point [i] into the end of append?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>

typedef struct strstf {
    char   *stuff;   
    size_t  length;   
} String;




 String *append(String *b, const char *c){
        String* append;

         if(!(c = NULL)){
            /*creates memory the size of the original string buffer and the added string*/
                 b->stuff realloc(strlen(c) + strlen(b->stuff) + 1);
                 strcpy(append, b->stuff);
                 strcat(append, c);
                 return append;  
            }
            if (append->stuff == NULL) {
                 free(append);  
                 return NULL;
            }
        return append;
    }

Aucun commentaire:

Enregistrer un commentaire