2012-03-30 58 views
1

问候语, 我遇到了“Assignment_1.c:10:18:错误:存储大小'的错误未知”我不是使用指针指针专家,我想有一个动态大小的单词动态大小的数组。任何想法?'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); 
    } 
+1

's->'这不会工作... – 2012-03-30 21:51:11

+1

没有人只是“指针指针专家”。你要么知道C,要么你不知道。考虑到你使用'void main',我可能会建议你从简单的事情开始。你为什么混合's和's->'? – 2012-03-30 21:51:35

+0

修复's->'thingy,然后尝试'sizeof(句子)'。祝你好运! – alexis 2012-03-30 21:57:52

回答

7

除非您尝试创建不透明类型,否则请勿对结构使用typedef。这是错误的。对C开发人员来说,struct是一个很好的暗示。 Linus对此有很好的描述:

It's a mistake to use typedef for structures and pointers. When you see a

vps_t a;

in the source, what does it mean?

In contrast, if it says

struct virtual_container *a;

you can actually tell what "a" is.

Lots of people think that typedefs "help readability". Not so. They are useful only for:

(a) totally opaque objects (where the typedef is actively used to hide what the object is).

Example: "pte_t" etc. opaque objects that you can only access using 
the proper accessor functions. 

NOTE! Opaqueness and "accessor functions" are not good in themselves. 
The reason we have them for things like pte_t etc. is that there 
really is absolutely _zero_ portably accessible information there. 

(b) Clear integer types, where the abstraction helps avoid confusion whether it is "int" or "long".

u8/u16/u32 are perfectly fine typedefs, although they fit into 
category (d) better than here. 

NOTE! Again - there needs to be a _reason_ for this. If something is 
"unsigned long", then there's no reason to do 

typedef unsigned long myflags_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) when you use sparse to literally create a new type for type-checking.

...

不要在一行中声明一堆变量。你只会这样做而迷惑别人。

当然,您不能使用.运算符引用会员字段,您必须使用->。话虽这么说,你的代码应该是这个样子:

#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; 
} 

还要考虑其'字的结构的第一个成员,或者您浪费内存由于对平台失准,其中指针的对齐比整数更大。

-1

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

您创建一个未命名的结构和该结构的别名。别名是sentence

现在,您的代码中已经有了一种新类型。新类型名称是sentence。如果您提供标签,则会有两个新类型名称:sentencestruct tag

typedef struct tag { 
    whatever; 
} sentence; 

还要注意的typedef是不是真的需要

struct tag { 
    whatever; 
}; 

上面的片段定义了一种新型的名为struct tag

0

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

#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); 
} 
+2

'sizeof(1024)'?再试一次。而且,他显然希望分配的大小取决于变量'size'。 – 2012-03-30 22:01:37

+0

谢谢@MooingDuck!希望这足够。虽然,上面的答案是更好的:) – ajmartin 2012-03-30 22:10:09