Re: compiling error



Richard Heathfield writes:

Kapteyn's Star said:

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");

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 I forget constantly that C does pass by value for functions, curse
of having learned Pascal in school :)

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

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

}

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

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

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

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

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>

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

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

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

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
.



Relevant Pages

  • Re: pointer q
    ... int main (void) ... it would compile what you wanted was: ... a type compatible with the effective type of the object, ...
    (comp.lang.c)
  • Re: Writing to text segment
    ... memcpy((void *)&foo, buf, sizeof buf); ... Because it won't compile? ... volatile int i = 1; ...
    (comp.lang.c)
  • Please Help => Simple Program
    ... I am trying to compile a Visual Studio 2005 C++ project using an ... about converting from void* to float* because of the last 5 parameters ... in the "BiquadSections" structure. ... int main ...
    (microsoft.public.dotnet.languages.vc)
  • SUMMARY: void main(int argc, char *argv[]) question
    ... The consensus is that the use of void on main is wrong (The book ... return and int. ... >In trying to compile some old test Unix C programs... ... Why do those old UNIX programs used void ... ...
    (SunManagers)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)