I am supposed to write a Modul for string implementation and I have a rough idea how and where to start. But I keep getting an error every time I try to use the arrow operator in C.
SET - is supposed to allocate the memory and initialise the string with the input.
COPY - copy copies one string to another
CONCAT - concatenation
PRINT - prints it out in the end
So any help would be appreciated:
Header File:
#ifndef string_h
#define string_h
typedef struct {
int len;
char *s;
} string_t;
typedef string_t *string;
void set(string *s1, char *s);
void copy(string *s1, string s2);
void concat(string *s1, string s2);
void print(string s1);
#endif /* string_h */
Implementation File:
#include "string.h"
#include <string.h>
#include <stdlib.h>
void set(string *s1, char *s) {
s1 = (string*) malloc (sizeof(string));
if(s == NULL) {
s1 -> len = 0;
} else {
s1 -> len = strlen(s);
s1 = (string*) malloc (sizeof(s1 -> len));
s1 -> s = s;
}
}
void copy (string *s1, string s2) {
}
void concat(string *s1, string s2) {
}
void print(string s1) {
}
Header files seems perfectly fine, as there's not much it does. But the Compiler gives out an error for "s1 -> len" every time. So if anyone could help me out here, I'd be grateful, Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…