Question:

(Easy for a C guru): Explain some of this C code to me so I can understand it better...?

by Guest55678  |  earlier

0 LIKES UnLike

(This is code under the GNU GPL, FYI)

Explain this line of code:

if (fsize > checktree->file->size) cmpresult = 1;

I'm struggling with all this pointer/structure stuff, and when I see lines like this, I have NO IDEA how to interpret it and understand what it's doing. What specifically does "checktree->file->size" actually do?

From the same file I am looking at, there is this line as well:

file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file) {

What exactly is the purpose of each component of this line, and what is the purpose of what I assume are "pointers to pointers" denoted by two asterisks?

 Tags:

   Report

3 ANSWERS


  1. Well, checktree->file->size is a chain of pointer dereferences.  We're going to the pointer called checktree, dereferencing it (visiting the memory location that checktree points to), from there dereferencing a pointer called "file" that is, probably, a member of the "checktree" object or struct, and then dereferencing a member called "size" that belongs to "file."  

    Remember that in C, -> is equivalent to a dereference and a member access, so you can rewrite this as:

    (*(*checktree).file).size

    Pointers to pointers (**) are exactly what they seem to be.  One use of them is when you expect a function to make a change to a pointer value.  For example, if I had a function that was going to reverse a string in place, I might define the function:

    int reverse(char **arg);

    And then I'd have a char* str and I'd pass in:

    char *str;

    reverse(&str);

    Handing in the address of the char * (giving me a char**) lets me change the value of the thing I'm sending in.  Since C only has the ability to pass by value, this is a trick we use to pass parameters by reference.

    So I would expect:

    file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file) {

    To return a pointer to root that I could act on, given parameters checktree and file.


  2. With that   arrow notation '->'  , use this rule :

    a->b is read as  " b of  a "  ( reverse )

    For example ,

    fsize > checktree->file->size is read as :

    size of file of checktree of fsize .

    With tta checkmatch () line :

    file_t **checkmatch(filetree_t **root, filetree_t *checktree, file_t *file)

    This is a function that takes 3 parameters  one is a pointer to a pointer and the other two are pointers. This function returns a a pointer to pointer to a struct of type file_t .

    This is a general rule for reading names of functions :

    Look for the first name before  the parentheses (). This will be the name of the function .

    Look at the RIGHT  of that name in the   parentheses , these are the parameters  of the function.

    Look at the LEFT of the function's name and you will find the return value of the function , which is only one value in C language .

    Example :

    int  createFile(  int k , int* p )

    Since this is a function ( because you have a name preceded by  a data type and followed by  parentheses ) :

    1. The  function's name is createFile

    2.RIGHT side  :It takes two parameters , one is integer and the other is a pointer to an integer

    3.LEFT  side :This function returns an integer  


  3. the solution is:

    The C Programming Language 2nd Ed.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.