error: invalid use of void expression

Discussion in 'C Programming' started by Fred Nurk, Jun 27, 2010.

  1. Fred Nurk

    Fred Nurk Guest

    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;
    }
    Fred Nurk, Jun 27, 2010
    #1
    1. Advertising

  2. Fred Nurk <> writes:
    > 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?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 27, 2010
    #2
    1. Advertising

  3. 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?



    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 27, 2010
    #3
  4. Fred Nurk

    Fred Nurk Guest

    Keith Thompson wrote:
    > <snip>
    > What type does Nline() return? What does strcmp() expect as its first
    > argument?


    Good point. :)
    Fred Nurk, Jun 27, 2010
    #4
  5. On Sun, 27 Jun 2010 12:11:49 +0200, Vincenzo Mercuri
    <> wrote:

    >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?

    --
    Remove del for email
    Barry Schwarz, Jun 27, 2010
    #5
  6. Barry Schwarz wrote:
    > On Sun, 27 Jun 2010 12:11:49 +0200, Vincenzo Mercuri
    > <> wrote:
    >
    >> 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
    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.

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 27, 2010
    #6
  7. On Sun, 27 Jun 2010 19:52:57 +0200, Vincenzo Mercuri
    <> wrote:

    >Barry Schwarz wrote:
    >> On Sun, 27 Jun 2010 12:11:49 +0200, Vincenzo Mercuri
    >> <> wrote:
    >>
    >>> 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.

    >row (ie the columns) would also be safer, since we might exceed MAX here
    >as well.


    --
    Remove del for email
    Barry Schwarz, Jun 28, 2010
    #7
  8. Barry Schwarz wrote:

    >>
    >>> 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.













    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 28, 2010
    #8
  9. Vincenzo Mercuri wrote:

    > Just writing
    >
    > char grid[5][5] = {""};
    >
    > would be fine.


    char grid[5][5]={{'\0'}};

    is even better

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 28, 2010
    #9
  10. Richard Heathfield wrote:
    > Vincenzo Mercuri wrote:
    >> Vincenzo Mercuri wrote:
    >>
    >>> Just writing
    >>>
    >>> char grid[5][5] = {""};
    >>>
    >>> would be fine.

    >>
    >> char grid[5][5]={{'\0'}};
    >>
    >> is even better

    >
    > My candidate would be:
    >
    > char grid[GRID_MAXX][GRID_MAXY] = {0};
    >


    taken from a book of Schildt's? (laughs)
    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 28, 2010
    #10
  11. On Mon, 28 Jun 2010 04:07:52 +0200, Vincenzo Mercuri
    <> wrote:

    >Barry Schwarz wrote:
    >
    >>>
    >>>> 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).


    --
    Remove del for email
    Barry Schwarz, Jun 29, 2010
    #11
  12. On Mon, 28 Jun 2010 04:44:38 +0200, Vincenzo Mercuri
    <> wrote:

    >Vincenzo Mercuri wrote:
    >
    >> Just writing
    >>
    >> char grid[5][5] = {""};
    >>
    >> would be fine.

    >
    >char grid[5][5]={{'\0'}};
    >
    >is even better


    Since the result is exactly the same, this is just a matter of style.

    --
    Remove del for email
    Barry Schwarz, Jun 29, 2010
    #12
  13. Barry Schwarz wrote:
    > On Mon, 28 Jun 2010 04:07:52 +0200, Vincenzo Mercuri
    > <> wrote:
    >
    >> Barry Schwarz wrote:
    >>
    >>>>
    >>>>> 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

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 29, 2010
    #13
  14. Barry Schwarz wrote:
    > On Mon, 28 Jun 2010 04:44:38 +0200, Vincenzo Mercuri
    > <> wrote:
    >
    >> Vincenzo Mercuri wrote:
    >>
    >>> Just writing
    >>>
    >>> char grid[5][5] = {""};
    >>>
    >>> would be fine.

    >>
    >> char grid[5][5]={{'\0'}};
    >>
    >> is even better

    >
    > Since the result is exactly the same, this is just a matter of style.
    >


    Yes.

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 29, 2010
    #14
  15. Richard Heathfield <> writes:
    > Barry Schwarz wrote:
    >
    > <snip>
    >
    >> Since the result is exactly the same, this is just a matter of style.

    >
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 29, 2010
    #15
  16. On 29/06/2010 17:37, Keith Thompson wrote:
    > Richard Heathfield<> writes:
    >> Barry Schwarz wrote:
    >>
    >> <snip>
    >>
    >>> Since the result is exactly the same, this is just a matter of style.

    >>
    >> 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.
    >


    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

    --
    Vincenzo Mercuri
    Vincenzo Mercuri, Jun 29, 2010
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    500
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    777
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    815
    S.Tobias
    Jul 22, 2005
  4. philwozza
    Replies:
    3
    Views:
    3,498
    philwozza
    May 13, 2006
  5. Replies:
    1
    Views:
    394
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page