Language = C. Please give an answer with an explanation for both questions. Q2 a
ID: 3708189 • Letter: L
Question
Language = C. Please give an answer with an explanation for both questions.
Q2 a) age == *p b)age == **q c) age == *q d) p ==*q e)none
Assuming that binary file my bin stores tests results where each test result is a doubie. The file is opened double results(5] a) fseek(fp., -3, SEEK END) using the file descriptor fp. Which sequence of code will read last three test results into an array results which is declared as fread(results, sizeof(double), 3, fp): b) fseek(p, 3'sizeof double), SEEK END) fread(results, sizeof(double), 3. fp) b) fseek(fp, 0, SEEK END); rc ftell (fp rc rc-3 sizeof (double); fseek(fp. -rc, SEEK END) fread(results, sizeof double), 3, fp); d) fseek(fp, 0, SEEK END) fseek(fp, -3 sizeof(double), SEEK CUR) fread results, sizeofídouble), 3.fp) e) fseek(fp, 3 sizeof(double), SEEK SET) fread(results, sizeof double), 3, fp) Given the following, which of the following expressions is NOT true? int age int p. "q p BageExplanation / Answer
The answer to the first question is, option d,
This can be understood by analysing how fseek works,
Syntax: fseek(file pointer, offset, from);
Where, file pointer is the file stream's object, offset is a long int type, which tells the "number of byte" to be traveled forward or backward in a file. And from tells the starting position.
In a binary file this can be chosen from a variety of options, but in TXT files it is compulsorily to be SEEK_SET.
Also, binary files are useful bcoz we can jump to any structure in a binary file, which provides random access as if it was an array.
Keeping this in mind, we eliminate options a,b,e bcoz:
Option a: tells fseek to move 3 bytes from the end, but our data type is double, so this will not yield desired results.
Option b: the offset is added to the end of file, thus placing the pointer at a very unwanted location.
Option e: SEEK_SET places the pointer to the beginning of file, thus we will read data from the third record from the beginning of file.
This leaves us with option c and d.
Option c: performing the maths we find that this statement will help us read record from record 3 from the starting.
Option d: with the first fseek statement, we place the pointer at the end of the file, similarly, at the end of last record.
With the second statement, we place the pointer 3 records back (-3*sizeof(double)) from the current position (SEEK_CUR) which indeed is the end of file. Thus with this statement we go back 3 records from the end of file, which is our desired position to read the last 3 records.
2. Let's consider our program,
int age;
int *p, **q; /*p is a pointer to a variable, that is, holds the address of a variable, and q is a pointer to a pointer, thus it holds the address of another pointer which, may, hold the address of some variable*/
p=&age; // store the address of variable age in pointer p
q=&p; //stores address of pointer p in variable q
Knowing this, let's try to justify the options.
Option a: age==*p, we know that the dereferencer (*) is used to fetch what value the variable, whose address is stored in the pointer, has. Thus, this is correct.
Option b: the same concept as option a, dereference the value stored in pointer q, which points to address location of p, which in turn stores the address of age, thus this statement essentially fetches the value stored in variable age, and is thus true.
Option c: *q gives the value stored in the address location stored in a, which is, the address of p.
Thus *q simply returns the value stored in the address it is pointing, which is pointer p, which indeed store the value, address of variable age. Thus, this is false.
Option d: when used without a dereferencer, the variable p returns the address on which it is stored, which, as explained is equal to the value stored in *q. Thus this is also true.
Therefore the answer is c.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.