Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

pset4 - resize - negation of my offset value for fseek is causing a runtime error

0 votes
asked Jun 26, 2017 in Pset4 by funipendulous (160 points)
edited Jun 26, 2017 by funipendulous
While working on the "resize" portion of pset4, I'm trying to enter a negative value in the offset of my fseek command, in order to move the pointer backwards.  The code compiles fine but upon execution, I see:

runtime error: negation of 9 cannot be represented in type 'unsigned long'

I have tried casting this value as every data type I can think of via something like:

        signed long offset = - (origWidth * sizeof(RGBTRIPLE) );
        fseek(inptr, offset, SEEK_CUR);

and I have also tried specifying various data types right in the fseek line and I always am presented with this same error.  I even tried subtracting this from zero, thinking that would in some way be more advantageous than using negation, to no avail.  When I hard-code a -9 for offset, it works as expected but I cannot get this value to work dynamically.

Thank you in advance for your thoughts,

Mike

1 Answer

0 votes
answered Jun 26, 2017 by funipendulous (160 points)
I was able to get past this by setting the sizeof(X) to a variable first:

        int sizeOfTriple = sizeof(RGBTRIPLE);
        signed long offset =  - (origWidth * sizeOfTriple);
        fseek(inptr, offset, SEEK_CUR);

Per GNU:      
"The result of the sizeof operator is of a type called size_t, which is defined in the header file <stddef.h>. size_t is an unsigned integer type, perhaps identical to unsigned int or unsigned long int; it varies from system to system."
Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

197 questions

248 answers

217 comments

4,844 users

4 Online
0 Member And 4 Guest
...