user3283585
user3283585

Reputation: 29

Pointer comparison causing errors

I'm trying to make a simple function that searches a chosen column for the first instance of a value, and then have it return the row it's in. This would search in a predefined struct called "SPREADSHEET". However, I'm having some major problems.

I'm getting error "incompatable type for argument 1 of memcpy" and a couple of other errors. Here's my code:

// Searches down the specified column for a row which contains text.
// The search starts at row number rowNum;
// The result is the row number (where the first row is numbered 0).
// If the text is not found, the result is -1.
int SS_FindRow(SPREADSHEET *ss, int colNum, char *text, int startNum) {
    if (debug)
        fprintf(stderr, "DEBUG: Call to SS_FindRow(--,%d,%s,%d)\n",
            colNum, text, startNum);
    // TO BE COMPLETED!
    SPREADSHEET read;
    memcpy(read,ss,sizeof(ss));
    int i;
    for(i = startNum; i < MAXROWS; i++) {
        if(strcmp(&read.contents[i][colNum],text)==0)
            return i;
    }
    return -1;
}

I'm really stumped as to what could be causing this. Running the program and the no matter what my input is the if statement will never succeed. Help?

Upvotes: 1

Views: 78

Answers (2)

Thanushan
Thanushan

Reputation: 532

Syntax of memcpy() is void *memcpy(void *str1, const void *str2, size_t n)

read must be a pointer pointing to a memory location, then you can use memcpy()

Or use &read in memcpy().

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182779

int SS_FindRow(SPREADSHEET *ss, int colNum, char *text, int startNum) {
                           ^

memcpy(read,ss,sizeof(ss));
                      ^^

You probably want the sizeof a SPREADSHEET, not a pointer to one. The easiest solution here is to use sizeof(read) instead.

Also, the first parameter to memcpy needs to be where to copy to. So you want &read (the address of read), not read (the value of read).

Upvotes: 2

Related Questions