编译错误 [英] compiling error

查看:85
本文介绍了编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨新闻组


这里给出的程序被GCC拒绝,错误我不能理解。它说

rnd00.c:在函数中?? ?? maina ??:

rnd00.c:26:错误:预期表达式之前的?? ?? a ??令牌


如何编译​​?我也尝试过buf [10]但是它给出了细分

错误。谢谢高级。


/ * scanf来自/ dev / random的10个随机整数* /

#include< stdio.h>


void open_file(FILE * f)

{

f = fopen(" / dev / random"," r");

}


void read_values(FILE * f,int buf [],int count)

{

do

{

--count;

fread((void *)& buf [count],sizeof(int),1 ,f);

}

而(数0);

}


int main ()

{

int buf [10];

int count = 10;

FILE * f;


open_file(f);

read_values(f,buf [],count);

do

{

--count;

printf("%d",buf [count]);

}

while(count 0);

返回0;

}

-

Kapteyn的明星

Hi newsgroup

The program here given is refused by GCC with a error i cannot
understand. It says
rnd00.c: In function a??maina??:
rnd00.c:26: error: expected expression before a??]a?? token

How to make it compile? I also tried buf[10] but that gives "segmentation
fault". Thanks in advanced.

/* scanf 10 random integers from /dev/random */
#include <stdio.h>

void open_file(FILE *f)
{
f= fopen("/dev/random", "r");
}

void read_values(FILE *f, int buf[], int count)
{
do
{
--count;
fread((void*)&buf[count], sizeof(int), 1, f);
}
while(count 0);
}

int main()
{
int buf[10];
int count= 10;
FILE *f;

open_file(f);
read_values(f, buf[], count);
do
{
--count;
printf("%d ", buf[count]);
}
while(count 0);
return 0;
}
--
Kapteyn''s Star

推荐答案

Kapteyn'的明星说:
Kapteyn''s Star said:

嗨新闻组


这里给出的程序被GCC拒绝了,我不能理解错误。它说

rnd00.c:在函数中?main?:

rnd00.c:26:错误:预期表达式之前?]?令牌


如何编译​​?我也尝试过buf [10]但是它给出了细分

错误。谢谢高级。


/ * scanf来自/ dev / random的10个随机整数* /

#include< stdio.h>


void open_file(FILE * f)

{

f = fopen(" / dev / random"," r");
Hi newsgroup

The program here given is refused by GCC with a error i cannot
understand. It says
rnd00.c: In function ?main?:
rnd00.c:26: error: expected expression before ?]? token

How to make it compile? I also tried buf[10] but that gives "segmentation
fault". Thanks in advanced.

/* scanf 10 random integers from /dev/random */
#include <stdio.h>

void open_file(FILE *f)
{
f= fopen("/dev/random", "r");



这不会做你想的。如果你想要一个函数来改变一个对象的价值,你必须将该对象的地址传递给该函数。

或者,你可以得到函数返回

对象的新值,并在调用者中获取,如图所示:


FILE * open_file()

{

return fopen(" / dev / random"," r");

}


用法: fp = open_file();


如果你想以参数方式进行,你需要这样做:


void open_file(FILE ** f)

{

* f = fopen(" / dev / random"," r");

}


用法:open_file(& fp);


请记住fopen如果无法打开则返回空指针

文件。

This won''t do what you think. If you want a function to change the value of
an object, you must pass to that function the address of the object.
Alternatively, you can get the function to return the new value of the
object, and pick it up in the caller, as shown:

FILE *open_file()
{
return fopen("/dev/random", "r");
}

usage: fp = open_file();

If you want to do it the parameter way, you need to do this:

void open_file(FILE **f)
{
*f = fopen("/dev/random", "r");
}

Usage: open_file(&fp);

Remember that fopen will return a null pointer if it fails to open the
file.


}


void read_values(FILE * f,int buf [],int count)

{

do

{

--count;

fread((void *)& buf [count],sizeof(int),1,f);
}

void read_values(FILE *f, int buf[], int count)
{
do
{
--count;
fread((void*)&buf[count], sizeof(int), 1, f);



更简单:

fread(& buf [count],sizeof buf [count],1,f);


甚至

fread(buf + count,sizeof buf [count],1,f);


< snip> ;

Simpler:
fread(&buf[count], sizeof buf[count], 1, f);

or even
fread(buf + count, sizeof buf[count], 1, f);

<snip>


int main()

{

int buf [10];

int count = 10;

FILE * f;


open_file(f);

read_values(f,buf [ ],数);
int main()
{
int buf[10];
int count= 10;
FILE *f;

open_file(f);
read_values(f, buf[], count);



这应该是:read_values(f,buf,count);


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

This should be: read_values(f, buf, count);

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Kapteyn'的明星< re ********************** *************** @ g0m8ai2l。 9com>

写道:

< snip>
Kapteyn''s Star <re*************************************@g0m8ai2l. 9com>
writes:
<snip>

void open_file(FILE * f)

{

f = fopen(" / dev / random" ,r);

}
void open_file(FILE *f)
{
f= fopen("/dev/random", "r");
}



除了其他评论之外,还应该是rb。


-

Ben。

In addition to the other comments, that should be "rb".

--
Ben.


Richard Heathfield写道:
Richard Heathfield writes:

Kapteyn'的明星说:
Kapteyn''s Star said:

>嗨新闻组

这里给出的程序被GCC拒绝我无法理解的错误。它说
rnd00.c:在函数中?main?:
rnd00.c:26:错误:预期表达式之前?]?令牌

如何编译?我也试过buf [10],但这给了
分段错误。先谢谢了。

/ * scanf来自/ dev / random * / #include< stdio.h的随机整数>

void open_file(FILE * f)
{
f = fopen(" / dev / random"," r");
>Hi newsgroup

The program here given is refused by GCC with a error i cannot
understand. It says
rnd00.c: In function ?main?:
rnd00.c:26: error: expected expression before ?]? token

How to make it compile? I also tried buf[10] but that gives
"segmentation fault". Thanks in advanced.

/* scanf 10 random integers from /dev/random */ #include <stdio.h>

void open_file(FILE *f)
{
f= fopen("/dev/random", "r");



这不会做你想的。如果你想要一个函数来改变一个对象的值

,你必须将该对象的地址传递给该函数。

或者,你可以得到函数返回

对象的新值,并在调用者中获取,如图所示:


FILE * open_file()

{

return fopen(" / dev / random"," r");

}


用法: fp = open_file();


如果你想以参数方式进行,你需要这样做:


void open_file(FILE ** f)

{

* f = fopen(" / dev / random"," r");

}


用法:open_file(& fp);


This won''t do what you think. If you want a function to change the value
of an object, you must pass to that function the address of the object.
Alternatively, you can get the function to return the new value of the
object, and pick it up in the caller, as shown:

FILE *open_file()
{
return fopen("/dev/random", "r");
}

usage: fp = open_file();

If you want to do it the parameter way, you need to do this:

void open_file(FILE **f)
{
*f = fopen("/dev/random", "r");
}

Usage: open_file(&fp);



oops我不断忘记C确实传递了函数值,诅咒

在学校学过Pascal :)

oops I forget constantly that C does pass by value for functions, curse
of having learned Pascal in school :)


请记住,如果无法打开

文件,fopen将返回空指针。
Remember that fopen will return a null pointer if it fails to open the
file.



好​​的。我无视检查fopen()因为/ dev / random存在于此处。但是我看到你的观点,它可能在旧内核下没有。

Okay. I ommited checking fopen() because /dev/random exists here. But i
see your point, it might be absent under older kernels.


>} <无效read_values(FILE * f,int buf [],int count){

{
--count;
fread((void *) )& buf [count],sizeof(int),1,f);
>}

void read_values(FILE *f, int buf[], int count) {
do
{
--count;
fread((void*)&buf[count], sizeof(int), 1, f);



更简单:

fread(& buf [count],sizeof buf [count],1,f);


Simpler:
fread(&buf[count], sizeof buf[count], 1, f);



我认为在不同的

类型指针之间进行转换时需要进行类型转换...

I thought that a typecast is needed when converting between different
typed pointers...


甚至

fread(buf + count,sizeof buf [count],1,f);
or even
fread(buf + count, sizeof buf[count], 1, f);



这很整洁!我仍然没有得到指针算术的悬念。 :(

但是没有括号的sizeof只是感觉很奇怪。我认为它不会是b $ b编译但是确实如此!在c中学到很多......

This is neat! I still not getting the hang of pointer arithmetics. :(
But sizeof without parenthesis just feels weird. I thought it wont
compile but it does! So much to learn in c...


< snip>
<snip>

> int main()
{
int buf [10] ;
int count = 10;
FILE * f;

open_file(f);
read_values(f,buf [],count);
>int main()
{
int buf[10];
int count= 10;
FILE *f;

open_file(f);
read_values(f, buf[], count);



这应该是:read_values(f,buf,count);


This should be: read_values(f, buf, count);



传递给函数的数组不应该有在原型中需要索引但

索引?注意。


Thanx给你和Ben ...程序现在可用了!


-

Kapteyn的明星

Okay arrays when passed to function should not have the index but the
index is needed when in the prototype? Noted.

Thanx to you and Ben... program works now!

--
Kapteyn''s Star


这篇关于编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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