vc.user_comments [i] = string; / * *崩溃/ [英] vc.user_comments[i] = string; /*crashing*/

查看:91
本文介绍了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?

推荐答案

" Tatu Portin" <斧**** @ mbnet.fi>在消息中写道

news:xV ************** @ read3.inet.fi ...
"Tatu Portin" <ax****@mbnet.fi> wrote in message
news:xV**************@read3.inet.fi...
有这种结构:

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


karl malbrain写道:
karl malbrain wrote:
" Tatu Portin" <斧**** @ mbnet.fi>在消息中写道
新闻:xV ************** @ read3.inet.fi ...
"Tatu Portin" <ax****@mbnet.fi> wrote in message
news:xV**************@read3.inet.fi...
有这种结构:

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;''不是正确的
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''。



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


问题解决了。


我没有为指针数组调用内存。我只是为指针数组指向的字符串调用内存




/ * ... * /


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


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


/ * ADDED * /

vc.user_comments =(char *)calloc(vc.comments + 1, sizeof(char *));

断言(vc.user_comments!= NULL);

/ *加入结束* /


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

断言(vc.comment_wds!= NULL);

/ * ... * /
Problem solved.

I hadn''t calloced memory for the array of pointers. I was only callocing memory
for the strings which the array of pointers should be pointing to.

/* ... */

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

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

/* ADDED */
vc.user_comments = (char *) calloc (vc.comments + 1, sizeof (char *));
assert (vc.user_comments != NULL);
/* End of ADDED */

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


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

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