Thread: Pointers

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    11

    Pointers

    Hi,

    I'm trying to understand how pointers work. It seems I can deference and sometimes I cannot.

    Code:
    main(int argc, char *argv[]) {
        FILE *f = fopen("/home/9three/Desktop/test.txt", "a");
    
        if (f == NULL) {
            printf("Unable to open file for writting.");
            return 1;
        }
        fprintf(*f, "Testing..\n");
    
        return 0;
    }
    If you notice I'm passing a pointer to fprintf. This doesn't compile and the error is "expecting FILE type". But "f" was declared as FILE type to begin with.

    So "f" is a pointer and variable at the same time? If I pass *f then I'm passing a pointer. But if I pass "f" then I'm just passing a variable with a specific type? eehhh.. what?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    T *p; // T is type int ,double ,float,FILE ..etc
    then p is pointer to T, and *p is type T.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think there is a confusion here.

    If you look here:

    fprintf - C++ Reference

    fprintf expects a FILE* not a FILE. f in your case is a pointer and *f is the FILE the pointer is pointing at. So you need to pass f to fprintf not *f.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Damn, beaten by Bayint
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 9three View Post
    Hi,

    I'm trying to understand how pointers work. It seems I can deference and sometimes I cannot.

    Code:
    main(int argc, char *argv[]) {
        FILE *f = fopen("/home/9three/Desktop/test.txt", "a");
    
        if (f == NULL) {
            printf("Unable to open file for writting.");
            return 1;
        }
        fprintf(*f, "Testing..\n");
    
        return 0;
    }
    If you notice I'm passing a pointer to fprintf. This doesn't compile and the error is "expecting FILE type". But "f" was declared as FILE type to begin with.

    So "f" is a pointer and variable at the same time? If I pass *f then I'm passing a pointer. But if I pass "f" then I'm just passing a variable with a specific type? eehhh.. what?
    Pointers are a type of variable, yes.

    The fopenf() function expects a pointer. When you hand it *f you are handing it the value at the address held in the pointer, which it doesn't know what to do with.
    Code:
    file *f;
    
    fopen (f,"blablabla.txt")...
    C has this thing about variables being used the way they're declared...

    int *x ... is a pointer to an integer.

    then

    *x is the data at that address.

    Someplace along the way they decided that would be easier...
    I rather tend to disagree. I've always found it thoroughly confusing!

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    Quote Originally Posted by CommonTater View Post
    Pointers are a type of variable, yes.
    int *x ... is a pointer to an integer.

    then

    *x is the data at that address.
    When you say *x is the data at that address, do you mean this:

    Code:
    int *x = 12345;
    
    //x contains 12345 ? or does *x contains 12345 ?
    //or does *x contain the address only?
    
    
    //wtf?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by 9three View Post
    When you say *x is the data at that address, do you mean this:

    Code:
    int *x = 12345;
    
    //x contains 12345 ? or does *x contains 12345 ?
    //or does *x contain the address only?
    
    
    //wtf?
    He means exactly what he says. x is the address, *x is the data at that address (i.e. the address stored in x).

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 9three View Post
    When you say *x is the data at that address, do you mean this:

    Code:
    int *x = 12345;
    
    //x contains 12345 ? or does *x contains 12345 ?
    //or does *x contain the address only?
    
    
    //wtf?
    x contains the address of 12345... not the number itself.

    *x is shorthand for "the data at the address in x"

    Code:
    int *x = malloc(sizeof(int));
    *x = 12345;
    
    printf("address = %d  value = %d", x, *x);
    
    free(x);
    Try it... you'll see...

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by 9three View Post
    When you say *x is the data at that address, do you mean this:

    Code:
    int *x = 12345;
    
    //x contains 12345 ? or does *x contains 12345 ?
    //or does *x contain the address only?
    
    
    //wtf?
    You need to understand the difference between defining a pointer, and de-referencing a pointer.
    Defining a pointer looks like this:

    Code:
    int *x;
    De-referencing a pointer looks like this:

    Code:
    int zero_value = 0;
    int some_value = 13;
    int* x = zero_value; //this is a defining + initialising statement 
    *x = some_value; //this is a de-referencing statement
    Since the main use for a pointer is to store memory addresses, defining a pointer and initializing it by using the assignment operator '=', ends up giving it (i.e. the pointer x) the memory address of the integer variable called "zero_value", whereas de-referencing it, and assigning it a value, is accessing what's being already pointed at (namely, the memory address of zero_value) and then replacing the existing value (i.e. 0) at that address with the value of the variable "some_value" which is 13, thus making the name of the "zero_value" variable now no longer accurate.
    Last edited by Programmer_P; 01-23-2011 at 09:44 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Programmer_P View Post
    You need to understand the difference between defining a pointer, and de-referencing a pointer.
    Defining a pointer looks like this:

    Code:
    int *x;
    De-referencing a pointer looks like this:

    Code:
    int zero_value = 0;
    int some_value = 13;
    int* x = zero_value; //this is a defining + initialising statement 
    *x = some_value; //this is a de-referencing statement
    Since the main use for a pointer is to store memory addresses, defining a pointer and initializing it by using the assignment operator '=', ends up giving it (i.e. the pointer x) the memory address of the integer variable called "zero_value", whereas de-referencing it, and assigning it a value, is accessing what's being already pointed at (namely, the memory address of zero_value) and then replacing the existing value (i.e. 0) at that address with the value of the variable "some_value" which is 13, thus making the name of the "zero_value" variable now no longer accurate.
    I think you just confused him even further, should have made the initialization to NULL not 0. (they are the same thing, but for a beginner.....)
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by claudiu View Post
    I think you just confused him even further, should have made the initialization to NULL not 0. (they are the same thing, but for a beginner.....)
    Unless C is different than C++ with pointers, the statement:

    Code:
    int* x = zero_value;
    does not assign x the value of zero_value. It assigns to x the memory address of zero_value, which would not be 0.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    Unless C is different than C++ with pointers, the statement:

    Code:
    int* x = zero_value;
    does not assign x the value of zero_value. It assigns to x the memory address of zero_value, which would not be 0.
    This is false, in both C and C++. To assign the memory address, you would need to use &zero_value.

  13. #13
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Just a pointer (no pun intended!) you can use the assert() macro for quickly testing things. Just include <assert.h>

    Code:
    #include <assert.h>
    #include <stdio.h>
    
    int main(){
      FILE *ptr = fopen("~/test.txt", "a+");
      assert(ptr != NULL);
      fprintf(ptr, "Testing a line.\n");
    
      return 0;
    }
    Faster testing means faster coding. Though you may want to check for errors in a more meaningful way later.. if you're just testing stuff asserts rock.. plus they rock for complex pointer stuff.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    This is false, in both C and C++. To assign the memory address, you would need to use &zero_value.
    Oh right, duh...
    I knew that, I just forgot to put it in my code in this thread.
    I'll be stepping out of this discussion now...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    Hi,

    I'm not new to programming so we can skip the beginner stuff. I come from the Java world!

    @CommonTater

    Your code does not compile. It was suppose to be %p, no bigge!

    After running your code it does clear it up a bit. But then it leads to another question...

    What about this:

    Code:
    main(int argc, char *argv[]) {
        printf("%s", argv[1]);
        printf("%c", *argv[1]);
    }
    In the first code block, in order for me to access the data I need to use *x. But in the second code block, if I want the entire data I need to use argv[1] (no dereferencing). Dereferencing gives me the first character of the argument passed with the file.

    Can someone explain that? Why such inconsistency ?


    btw, does anyone have any resource on getting the address? Such as *argv[1] + 3, it should give me another address.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays with pointers
    By Niels_M in forum C Programming
    Replies: 18
    Last Post: 07-31-2010, 03:31 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM