C 存储大小为‘;s’;国际标准化组织’;不知道+;指针到指针

C 存储大小为‘;s’;国际标准化组织’;不知道+;指针到指针,c,pointers,struct,malloc,C,Pointers,Struct,Malloc,问候,, 我遇到了“赋值_1.c:10:18:error:s的存储大小未知”的错误。我不擅长使用指针指向指针,我想要一个动态大小的单词数组。有什么想法吗 #include <stdio.h> #include <stdlib.h> #define MAX 100 int size = MAX; typedef struct{ int numberOfWords,averageWordLength,id; char ** words; }

问候,, 我遇到了“赋值_1.c:10:18:error:s的存储大小未知”的错误。我不擅长使用指针指向指针,我想要一个动态大小的单词数组。有什么想法吗

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int size = MAX;
typedef struct{
    int numberOfWords,averageWordLength,id;
    char ** words;      
    }sentence;
void main(){  
    struct sentence s; 
    s.numberOfWords=3;
    s.averageWordLength=5;
    s.id=1;
    s->words= malloc(size * sizeof(s));
    //printf("%s",s.words);
    }
#包括
#包括
#定义最大值100
int size=MAX;
类型定义结构{
int numberOfWords,平均字长,id;
字符**字;
}判决;
void main(){
结构句;
s、 numberOfWords=3;
s、 平均字长=5;
s、 id=1;
s->words=malloc(大小*sizeof(s));
//printf(“%s”,s.words);
}

除非试图创建不透明类型,否则不要对结构使用typedef。这是错误的<代码>结构对C开发人员来说是一个很好的提示。莱纳斯对此有很好的描述:

对结构和指针使用typedef是错误的。当你 见

副总裁

在来源中,它意味着什么

相反,如果它说

结构虚拟容器*a

你实际上可以分辨出“a”是什么

很多人认为typedef“有助于可读性”。不是这样。他们 仅适用于以下情况:

(a) 完全不透明的对象(typedef主动用于 隐藏 对象是什么)

(b) 清除整数类型,其中抽象有助于避免 混乱 无论是“int”还是“long”

typedef无符号长myflags\u t

 but if there is a clear reason for why it under certain circumstances
 might be an "unsigned int" and under other configurations might be
 "unsigned long", then by all means go ahead and use a typedef.
(c) 当您使用“稀疏”创建新类型时 类型检查

不要在一行中声明一组变量。这样做只会让别人感到困惑

当然,不能使用
运算符引用成员字段,必须使用
->
。也就是说,您的代码应该如下所示:

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

#define MAX 100

struct sentence {
    int numberOfWords;
    int averageWordLength;
    int id;
    char **words;
};

int main()
{
    struct sentence s;
    s.numberOfWords = 3;
    s.averageWordLength = 5;
    s.id = 1;
    s.words = malloc(MAX * sizeof(s));
    /* printf("%s",s.words); */
    return EXIT_SUCCESS;
}
#包括
#包括
#定义最大值100
结构句{
整数字;
int平均字长;
int-id;
字符**字;
};
int main()
{
结构句;
s、 numberOfWords=3;
s、 平均字长=5;
s、 id=1;
s、 words=malloc(MAX*sizeof);
/*printf(“%s”,s.words)*/
返回退出成功;
}

也可以考虑将单词作为结构的第一个成员,或者由于指针的位置大于整数的平台上的错位而浪费内存。

< P>不要使用Type来构造Struts,除非您试图创建不透明类型。这是错误的<代码>结构对C开发人员来说是一个很好的提示。莱纳斯对此有很好的描述:

对结构和指针使用typedef是错误的。当你 见

副总裁

在来源中,它意味着什么

相反,如果它说

结构虚拟容器*a

你实际上可以分辨出“a”是什么

很多人认为typedef“有助于可读性”。不是这样。他们 仅适用于以下情况:

(a) 完全不透明的对象(typedef主动用于 隐藏 对象是什么)

(b) 清除整数类型,其中抽象有助于避免 混乱 无论是“int”还是“long”

typedef无符号长myflags\u t

 but if there is a clear reason for why it under certain circumstances
 might be an "unsigned int" and under other configurations might be
 "unsigned long", then by all means go ahead and use a typedef.
(c) 当您使用“稀疏”创建新类型时 类型检查

不要在一行中声明一组变量。这样做只会让别人感到困惑

当然,不能使用
运算符引用成员字段,必须使用
->
。也就是说,您的代码应该如下所示:

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

#define MAX 100

struct sentence {
    int numberOfWords;
    int averageWordLength;
    int id;
    char **words;
};

int main()
{
    struct sentence s;
    s.numberOfWords = 3;
    s.averageWordLength = 5;
    s.id = 1;
    s.words = malloc(MAX * sizeof(s));
    /* printf("%s",s.words); */
    return EXIT_SUCCESS;
}
#包括
#包括
#定义最大值100
结构句{
整数字;
int平均字长;
int-id;
字符**字;
};
int main()
{
结构句;
s、 numberOfWords=3;
s、 平均字长=5;
s、 id=1;
s、 words=malloc(MAX*sizeof);
/*printf(“%s”,s.words)*/
返回退出成功;
}

也考虑将“单词作为结构的第一个成员”,或者在指针的对齐大于整数的平台上浪费了内存,

typedef struct {
    int numberOfWords, averageWordLength, id;
    char **words;
    } sentence;
您可以为该结构创建未命名的结构和别名。别名是
句子

现在代码中有了一个新类型。新的类型名称是
语句
。如果您提供了一个标记,那么将有两个新的类型名称:
句子
结构标记

typedef struct tag {
    whatever;
} sentence;
还要注意的是,实际上并不需要typedef

struct tag {
    whatever;
};
上面的代码段在中定义了一个名为
struct-tag

的新类型

typedef struct {
    int numberOfWords, averageWordLength, id;
    char **words;
    } sentence;
typedef struct tag {
    whatever;
} sentence;
您可以为该结构创建未命名的结构和别名。别名是
句子

现在代码中有了一个新类型。新的类型名称是
语句
。如果您提供了一个标记,那么将有两个新的类型名称:
句子
结构标记

typedef struct tag {
    whatever;
} sentence;
还要注意的是,实际上并不需要typedef

struct tag {
    whatever;
};

当结构的对象是指针类型时,上面的代码段定义了一个名为
struct tag

的新类型Use'->'。这应该起作用:

typedef struct tag {
    whatever;
} sentence;
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int size = MAX;
typedef struct{
  int numberOfWords,averageWordLength,id;
  char *words;
}sentence;

void main(){
  sentence s;
  s.numberOfWords=3;
  s.averageWordLength=5;
  s.id=1;
  s.words= malloc(size * sizeof(s));
  //printf("%s",s.words);
}
#包括
#包括
#定义最大值100
int size=MAX;
类型定义结构{
int numberOfWords,平均字长,id;
字符*单词;
}判决;
void main(){
句子s;
s、 numberOfWords=3;
s、 平均字长=5;
s、 id=1;
s、 文字=malloc(尺寸*尺寸);
//printf(“%s”,s.words);
}

当结构的对象是指针类型时,使用“->”。这应该起作用:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int size = MAX;
typedef struct{
  int numberOfWords,averageWordLength,id;
  char *words;
}sentence;

void main(){
  sentence s;
  s.numberOfWords=3;
  s.averageWordLength=5;
  s.id=1;
  s.words= malloc(size * sizeof(s));
  //printf("%s",s.words);
}
#包括
#包括
#定义最大值100
int size=MAX;
类型定义结构{
int numberOfWords,平均字长,id;
字符*单词;
}判决;
void main(){
句子s;
s、 numberOfWords=3;
s、 平均字长=5;
s、 id=1;
s、 文字=malloc(尺寸*尺寸);
//printf(“%s”,s.words);
}

s->
这是行不通的……没有人只是“指向指针的指针专家”。你要么知道C,要么不知道。考虑到您使用的是
void main
,我建议您从更简单的开始