Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how file pointer works, needs explanation?

+1 vote
asked May 5, 2020 by sofibilal193 (620 points)

2 Answers

–1 vote
answered Jun 16, 2020 by Filip (120 points)

I saw your question recently.

Well, basically pointer is just variable that changes it own value to position to "point" at some value.

For example we have one array made of 5 numbers. Out job is to decrease every number here by 1.

If array[5] = {1, 4, 7, 9, 2} we need our pointer to go from first to last number in array. We are going to do that like this:


array example[5] = {1, 4, 7, 9, 2};        //this is our example array

int i;   //Here we need variable(for example variable "i" because its most frequently used) to be our pointer. He will "move" from first to last by increasing himself.

void loop {   //this is loop that repeats forever

     if(i < 5) {   //Here we make condition for our pointer "i" position,  if our pointer "i" is smaller than max count of numbers in our array(in this example five numbers). Instead of using if, we can use logic's like for, while or any other if you understand it.

          array[ i ] = array[ i ] - 1;   //here we subtract number one from position in our array. For example, if pointer position is 2 then we subtract number 1 from number that is at 2nd place in our array and that is number 4

          i++;   //We add 1 to pointer i because we want to move it for one position up. For example, if last time pointer was at position 3, that is number 7 in our array, now he will go and subtract 1 from next number, number 9 and so on.

     }

    if (i = 5) {   //here we set condition for moment when pointer i will be equal to number 5, in the other words when our pointer is at last number. In our example, it's number 5.

             i = 0;   //here we reset our pointer i to value of 0 because we need him to start again from beginning, from position 0, so we can again increase from minimal to maximum position

     }

}

With this example, you have code that will turn array that include numbers {1, 4, 7, 9, 2} to array that include {0, 3, 6, 8, 1}.

I hope this is useful and that it will help you our. If you are using instagram, i would be really grateful if you respond there at profile named @filikapec

Best Regards,

Filip Filipović.

commented Jun 17, 2020 by Peter Minarik (84,720 points)
The variable 'i' in your example is not a pointer, but an index.
A pointer is a variable that does not store the value, rather the memory address of a value. Basic operations do not change the value the pointer points to, rather change the memory address itself (again, not what's stored there).
0 votes
answered Jun 17, 2020 by Peter Minarik (84,720 points)
edited Jun 18, 2020 by Peter Minarik

Look at the example in http://www.cplusplus.com/reference/cstdio/fopen/.

That's you basic open/close for files in C.

For reading and writing data, look at methods in the stdio.h on the reference page (see above). Focus on those, starting with the "f" (file) prefix, such as fread, fwrite, fgets, fputs, etc.

A file points is just like any other pointer. You can google it yourself or have a look at a tutorial I've found: https://www.programiz.com/c-programming/c-pointers

A small explanation for pointers is this:

A pointer is a variable that does not store the value, rather the memory address of a value. Basic operations do not change the value the pointer points to, rather change the memory address itself (again, not what's stored there). One can dereference a pointer to access its value (e.g. *pArray is accessing the value stored at the address pArray).

int array[] = { 1, 2, 3, 4 };
int * pArray = &array[0]; // Now our pArray (pointer to array) points to the address of the 0th element in the array (0-based indexing)
*pArray = 10; // changing the value stored at the address of pArray.
pArray++; // Now it points to the 1st element
*pArray= 20;
*(++pArray) = 30; // Point the the next element (2nd) and set it's value to 30
*(++pArray) = 40; // Point to the next element (3rd) and set it's value to 40;
// now our array is { 10, 20, 30, 40 }
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...