本文介绍了vc.user_comments [i] = string; / * *崩溃/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这种结构:


typedef struct {

char ** user_comments;

/ * ... * /

} vorbis_comment;


/ * prototype * /

char * read_vorbis_string

(文件* sc);


/ *问题* /

vc.user_comments [i] = read_vorbis_string(sc);


''read_vorbis_string''返回一个指向calloced字符串的指针。这个函数

肯定是有用的。

但问题是,'vc.user_comments [i] = string;''不是正确的方式

将''字符串''的地址分配给指针数组''vc.user_comments''。

什么是?

Have this kind of struct:

typedef struct {
char **user_comments;
/* ... */
} vorbis_comment;

/* prototype */
char * read_vorbis_string
( FILE *sc);

/* problem */
vc.user_comments[i] = read_vorbis_string (sc);

''read_vorbis_string'' returns a pointer to a calloced string. This function
works for sure.
But the problem is, that ''vc.user_comments[i] = string;'' is not the right way
to assing the address of the ''string'' to the pointer array ''vc.user_comments''.
What is?

推荐答案




你已经忘记了vorbis_comment的实施。请发布这个

部分,声明并初始化vc。


karl m



You''ve left out the INSTANTIATION of vorbis_comment. Please post this
section, where you declare and initialize vc.

karl m




方式



way


''vc.user_comments''。



''vc.user_comments''.



你已经忘记了vorbis_comment的实施。请发布此
部分,声明并初始化vc。

karl m


You''ve left out the INSTANTIATION of vorbis_comment. Please post this
section, where you declare and initialize vc.

karl m




这是你的 ; INSTANTIATION&qu​​ot;:


vorbis_comment read_comments

(const char * scname)

{

/ *在第二个''vorbis'之后,字符串出现了VENDOR字符串的宽度(int)。

*然后是VENDOR字符串本身。

*在VENDOR字符串出现之后(int)评论。

*评论数量来自第一个用户评论的宽度(int)。

*第一个用户评论后出现宽度(int )第二个用户评论。

* /

注册int i;


vorbis_comment vc;

FILE * sc;

char * ident =" vorbis";


sc = fopen(scname," rb");

断言(sc!= NULL);


断言(!wind_till(ident,sc));

断言(!wind_till(ident) ,sc));


vc.vend或= read_vorbis_string(sc);

断言(vc.vendor!= NULL);


断言(fread(& vc.comments,sizeof(int) ),1,sc)== 1);


printf("%d \ nn",vc.comments);


vc.comment_wds =(int *)calloc(vc.comments + 1,sizeof(int));

断言(vc.comment_wds!= NULL);

$ (b = b;(i = 0;我< vc.comments - 1; i ++){

vc.user_comments [i] = read_vorbis_string(sc);


assert(vc.user_comments [i]!= NULL);

printf(" __%s __",vc.user_comments [i]);


vc.comment_wds [i] = strlen(vc.user_comments [i] );

}


vc.comment_wds [vc.comments + 1] = 0;

return vc;

}

/ *文件结尾* /



Here is your "INSTANTIATION":

vorbis_comment read_comments
( const char *scname)
{
/* After second ''vorbis'' string comes the width (int) of VENDOR string.
* Then comes the VENDOR string itself.
* After VENDOR string comes the number (int) of comments.
* After the number of comments comes the width (int) of first user comment.
* After the first user comment comes the width (int) of the second user comment.
*/
register int i;

vorbis_comment vc;
FILE *sc;
char *ident = "vorbis";

sc = fopen (scname, "rb");
assert (sc != NULL);

assert (!wind_till (ident, sc));
assert (!wind_till (ident, sc));

vc.vendor = read_vorbis_string (sc);
assert (vc.vendor != NULL);

assert (fread (&vc.comments, sizeof (int), 1, sc) == 1);

printf ("%d\n", vc.comments);

vc.comment_wds = (int *) calloc (vc.comments + 1, sizeof (int));
assert (vc.comment_wds != NULL);

for (i = 0 ; i < vc.comments - 1 ; i++) {
vc.user_comments[i] = read_vorbis_string (sc);

assert (vc.user_comments[i] != NULL);
printf ("__%s__", vc.user_comments[i]);

vc.comment_wds[i] = strlen (vc.user_comments[i]);
}

vc.comment_wds[vc.comments + 1] = 0;
return vc;
}
/* End Of File */



这篇关于vc.user_comments [i] = string; / * *崩溃/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:51