error: invalid use of void expression

F

Fred Nurk

Why am I getting the Subject of this discussion on lines 78, 80, 82, 84,
86, 88, 90 and 92? The problem statement is at http://sites.google.com/
site/xtheunknown0/hunt-a-word

#include <stdio.h>
#include <string.h>

#define MAX 100

void Nline(int r, int c, int n, char grid[][MAX], char *word) {
int i;

for (i = 0; i < n; i++)
word = grid[r--][c];
}

void NEline(int r, int c, int n, char grid[][MAX], char *word) {
int i;

for (i = 0; i < n; i++)
word = grid[r--][c++];
}

void Eline(int r, int c, int n, char grid[][MAX], char *word) {
int i;

for (i = 0; i < n; i++)
word = grid[r][c++];
}

void SEline(int r, int c, int n, char grid[][MAX], char *word) {
int i;

for (i = 0; i < n; i++)
word = grid[r++][c++];
}

void Sline(int r, int c, int n, char grid[][MAX], char *word) {
int i;

for (i = 0; i < n; i++)
word = grid[r++][c];
}

void SWline(int r, int c, int n, char grid[][MAX], char *word) {
int i;

for (i = 0; i < n; i++)
word = grid[r++][c--];
}

void Wline(int r, int c, int n, char grid[][MAX], char *word) {
int i;

for (i = 0; i < n; i++)
word = grid[r][c--];
}

void NWline(int r, int c, int n, char grid[][MAX], char *word) {
int i;

for (i = 0; i < n; i++)
word = grid[r--][c++];
}

int main() {
FILE *fin = fopen("huntin.txt", "r");
FILE *fout = fopen("huntout.txt", "w");
char grid[MAX][MAX], word[MAX], temp[MAX];
int height, width, i, j, k, nwords, wlen;

fscanf(fin, "%d %d", &height, &width);
for (i = 0; i < height; i++)
fscanf(fin, "%s", grid);
fscanf(fin, "%d", &nwords);

for (i = 0; i < nwords; i++) {
fscanf(fin, "%s", word);
wlen = strlen(word);
for (j = 0; j < height; j++)
for (k = 0; k < width; k++)
if (j-wlen+1>=0 && !strcmp(Nline(j, k,
wlen, grid, temp), word))
fprintf(fout, "%d %d N\n", j+1, k
+1);
else if (j-wlen+1>=0 && k+wlen-1<=width
&& !strcmp(NEline(j, k, wlen, grid, temp), word))
fprintf(fout, "%d %d NE\n", j+1, k
+1);
else if (k+wlen-1<=width && !strcmp(Eline
(j, k, wlen, grid, temp), word))
fprintf(fout, "%d %d E\n", j+1, k
+1);
else if (j+wlen<=height && k+wlen-1<=width
&& !strcmp(SEline(j, k, wlen, grid, temp), word))
fprintf(fout, "%d %d SE\n", j+1, k
+1);
else if (j+wlen<=height && !strcmp(Sline
(j, k, wlen, grid, temp), word))
fprintf(fout, "%d %d S\n", j+1, k
+1);
else if (k-wlen+1>=0 && j+wlen<=height
&& !strcmp(SWline(j, k, wlen, grid, temp), word))
fprintf(fout, "%d %d SW\n", j+1, k
+1);
else if (k-wlen+1>=0 && !strcmp(Wline(j,
k, wlen, grid, temp), word))
fprintf(fout, "%d %d W\n", j+1, k
+1);
else if (k-wlen+1>=0 && j-wlen+1>=0 && !
strcmp(NEline(j, k, wlen, grid, temp), word))
fprintf(fout, "%d %d NW\n", j+1, k
+1);
}
return 0;
}
 
K

Keith Thompson

Fred Nurk said:
Why am I getting the Subject of this discussion on lines 78, 80, 82, 84,
86, 88, 90 and 92? The problem statement is at http://sites.google.com/
site/xtheunknown0/hunt-a-word

#include <stdio.h>
#include <string.h>

#define MAX 100

void Nline(int r, int c, int n, char grid[][MAX], char *word) { [snip]
[snip]
if (j-wlen+1>=0 && !strcmp(Nline(j, k,
wlen, grid, temp), word))
[snip]

What type does Nline() return? What does strcmp() expect as its
first argument?
 
B

Barry Schwarz

Fred Nurk wrote:

int main() {

int main(/void/) {

FILE *fin = fopen("huntin.txt", "r");
FILE *fout = fopen("huntout.txt", "w");

these streams should be /f/closed before exiting from main.
Moreover, "huntin.txt" should exist before program runs.
char grid[MAX][MAX],...
fscanf(fin, "%s", grid);


this is really what you want?


Other than the fact that it might overrun the i-th element of grid,
what do you think is wrong?
 
V

Vincenzo Mercuri

Barry said:
Fred Nurk wrote:

int main() {

int main(/void/) {

FILE *fin = fopen("huntin.txt", "r");
FILE *fout = fopen("huntout.txt", "w");

these streams should be /f/closed before exiting from main.
Moreover, "huntin.txt" should exist before program runs.
char grid[MAX][MAX],...
fscanf(fin, "%s", grid);


this is really what you want?


Other than the fact that it might overrun the i-th element of grid,
what do you think is wrong?


I should correct myself, I meant to add a check for the case height>MAX
char grid[MAX][MAX], word[MAX], temp[MAX];
int height, width, i, j, k, nwords, wlen;

fscanf(fin, "%d %d", &height, &width);
for (i = 0; i < height; i++)
fscanf(fin, "%s", grid);


and initialize grid. grid is a pointer to the i-th row of characters
and it's fine. Taking the control over the number of characters in each
row (ie the columns) would also be safer, since we might exceed MAX here
as well.
 
B

Barry Schwarz

Barry said:
Fred Nurk wrote:

<snip>
int main() {

int main(/void/) {

<snip>
FILE *fin = fopen("huntin.txt", "r");
FILE *fout = fopen("huntout.txt", "w");

these streams should be /f/closed before exiting from main.
Moreover, "huntin.txt" should exist before program runs.

char grid[MAX][MAX],...

fscanf(fin, "%s", grid);

this is really what you want?


Other than the fact that it might overrun the i-th element of grid,
what do you think is wrong?


I should correct myself, I meant to add a check for the case height>MAX
char grid[MAX][MAX], word[MAX], temp[MAX];
int height, width, i, j, k, nwords, wlen;

fscanf(fin, "%d %d", &height, &width);
for (i = 0; i < height; i++)
fscanf(fin, "%s", grid);


and initialize grid. grid is a pointer to the i-th row of characters


What would you initialize grid to?
and it's fine. Taking the control over the number of characters in each

The **object** grid is not a pointer. It is an array. It is the
actual i-th row.

In most cases, the **expression** grid will be converted to a
pointer as you describe but that is not the same thing.
 
V

Vincenzo Mercuri

Barry said:
char grid[MAX][MAX], word[MAX], temp[MAX];
int height, width, i, j, k, nwords, wlen;

fscanf(fin, "%d %d",&height,&width);
for (i = 0; i< height; i++)
fscanf(fin, "%s", grid);


and initialize grid. grid is a pointer to the i-th row of characters


What would you initialize grid to?


Playing safe:

suppose we have height = width = 5 and MAX == 5.
And suppose we pass (mistakenly or not)
the following strings to grid:

[ 'b' ][ 'i' ][ 'r' ][ 'd' ][ '\0']

[ 'o' ][ 'k' ][ 'a' ][ 'y' ][ '\0']

[ 'a' ][ 'n' ][ 't' ][ '\0'][ ??? ]

[ 'b' ][ 'u' ][ 'g' ][ 's' ][ '\0']

[ 'p' ][ 'a' ][ 'r' ][ 'k' ][ '\0']

If for some reason (mistakenly or not),
we want to print the entire sequence of
characters of grid[2] (the array)
we will get a meaningless value for grid[2][4].

Just writing

char grid[5][5] = {""};

would be fine.

That said, i don't mean, in this case, that initialization
is a must. From my point of view it is just
a more meaningful choice.
The **object** grid is not a pointer. It is an array. It is the
actual i-th row.

In most cases, the **expression** grid will be converted to a
pointer as you describe but that is not the same thing.


Yes. I agree. I must be more precise and
maybe admit that I relied too much on the K. King's
book's terminology. (see Chapter 12, Sec. 12.4,
4th-to-last line of pag.268, C Programming:
A Modern Approach, 2nd Edition).

Nevertheless, even a good book can't replace
the Standard's reliability.

When I say that grid is a pointer to the i-th
row of characters I mean that the expression
grid has the same numerical value of &grid[0].

By that, i meant that i didn't have to object to
passing grid to fscanf.
 
B

Barry Schwarz

Barry said:
char grid[MAX][MAX], word[MAX], temp[MAX];
int height, width, i, j, k, nwords, wlen;

fscanf(fin, "%d %d",&height,&width);
for (i = 0; i< height; i++)
fscanf(fin, "%s", grid);

and initialize grid. grid is a pointer to the i-th row of characters


What would you initialize grid to?


Playing safe:

suppose we have height = width = 5 and MAX == 5.
And suppose we pass (mistakenly or not)
the following strings to grid:

[ 'b' ][ 'i' ][ 'r' ][ 'd' ][ '\0']

[ 'o' ][ 'k' ][ 'a' ][ 'y' ][ '\0']

[ 'a' ][ 'n' ][ 't' ][ '\0'][ ??? ]

[ 'b' ][ 'u' ][ 'g' ][ 's' ][ '\0']

[ 'p' ][ 'a' ][ 'r' ][ 'k' ][ '\0']

If for some reason (mistakenly or not),
we want to print the entire sequence of
characters of grid[2] (the array)
we will get a meaningless value for grid[2][4].


Since we are talking about initialization, if you initialize grid with
the values indicated, then the value of grid[2][4] is guaranteed to be
0 (or '\0' if you prefer).
 
V

Vincenzo Mercuri

Barry said:
Barry said:
char grid[MAX][MAX], word[MAX], temp[MAX];
int height, width, i, j, k, nwords, wlen;

fscanf(fin, "%d %d",&height,&width);
for (i = 0; i< height; i++)
fscanf(fin, "%s", grid);

and initialize grid. grid is a pointer to the i-th row of characters

What would you initialize grid to?


Playing safe:

suppose we have height = width = 5 and MAX == 5.
And suppose we pass (mistakenly or not)
the following strings to grid:

[ 'b' ][ 'i' ][ 'r' ][ 'd' ][ '\0']

[ 'o' ][ 'k' ][ 'a' ][ 'y' ][ '\0']

[ 'a' ][ 'n' ][ 't' ][ '\0'][ ??? ]

[ 'b' ][ 'u' ][ 'g' ][ 's' ][ '\0']

[ 'p' ][ 'a' ][ 'r' ][ 'k' ][ '\0']

If for some reason (mistakenly or not),
we want to print the entire sequence of
characters of grid[2] (the array)
we will get a meaningless value for grid[2][4].


Since we are talking about initialization, if you initialize grid with
the values indicated, then the value of grid[2][4] is guaranteed to be
0 (or '\0' if you prefer).

Of course the table refers to after inserting the
strings, with grid previously uninitialized

Regards
 
K

Keith Thompson

Richard Heathfield said:
Barry Schwarz wrote:



I disagree. It's not *just* a matter of style. It's a matter of style!

I picture you saying that with a dramatic gesture. I hope you did.
 
V

Vincenzo Mercuri

I picture you saying that with a dramatic gesture. I hope you did.

Although I don't get to understand or interpret other people's
gestures and irony, for some reason I believe I should thank Richard for
his line. Thanks Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top