Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c - String implementation and can't seem to use the arrow operator

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

s1 seems to be a pointer to pointer, not a pointer to structure type.

You can just change the typedef to remove the pointer type, or just use string type (not string *), as it itself is a pointer already otherwise.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...