字符串实现,似乎不能使用箭头运算符 [英] String implementation and can't seem to use the arrow operator

查看:49
本文介绍了字符串实现,似乎不能使用箭头运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该为字符串实现编写一个模块,我对如何以及从哪里开始有一个粗略的想法.但是每次我尝试在 C 中使用箭头运算符时,我都会遇到错误.

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 - 应该分配内存并使用输入初始化字符串.COPY - copy 将一个字符串复制到另一个CONCAT - 串联PRINT - 最后打印出来

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:

头文件:

    #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 */

实施文件:

    #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) {

    }

头文件看起来非常好,因为它没有太多功能.但是编译器给出了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.

推荐答案

s1 似乎是指向指针的指针,而不是指向结构类型的指针.

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

您可以更改 typedef 以删除指针类型,或仅使用 string 类型(而不是 string *),因为它本身否则已经是一个指针.

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.

这篇关于字符串实现,似乎不能使用箭头运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆