Thread: Pointers in C

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    Pointers in C

    I have problems with pointers wich I am trying to clear .Are my comments correct

    Code:
     #include <stdio.h>
    
    int main()
    {   
      int * one=NULL;
      printf("%d\n",one);
      int a=5;
      one=&a;
      printf("%p\n",one);//printing the the value in pointer one
      printf("%p\n",&a); 
      printf("%d\n",*one); //printing the value where the value(address) of the pointer is pointing 
      printf("%p\n",&one);
       if (one)
       printf("GOOD\n");
       getch();
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    What are you trying to do and what's the problem? It looks like you create an int variable, then an int pointer to that variable, and then you print out the variable and the pointer (not in that order).

  3. #3
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Pointers are variables.
    Cprogramming.com FAQ > A tutorial on pointers

    Also getch(); is not defined in stdio.h and on Linux getch() only works within special curses applications that disable line buffering. For something more portable:
    Cprogramming.com FAQ > Stop my Windows Console from disappearing everytime I run my program?
    Cprogramming.com FAQ > How do I get my program to wait for a keypress?

    Or this little gem, which is evil outside of keeping a local console open because it does not check for EOF:
    while (getchar()!='\n');

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your comments sound correct. A pointer is just a variable that stores an address. To clarify, here is what each printf is doing:
    Code:
      printf("%d\n",one);
    The value in one is an address, not an integer, so this isn't correct. You properly print the value pointed to by one in your 4th printf.

    Code:
      printf("%p\n",one);//printing the the value in pointer one
    one contains an address, so you are printing the address contained in one (which you initialized to NULL, so it will likely print 0x000000)

    Code:
      printf("%p\n",&a);
    a is an integer, so you are printing the address of the variable a

    Code:
      printf("%d\n",*one); //printing the value where the value(address) of the pointer is pointing
    You are printing the value contained at the address stored in one. Since you initialized one to NULL, this will probably seg fault (can't read from NULL).

    Code:
      printf("%p\n",&one);
    You are printing the address of the variable one (this should be close to &a since they are adjacent on the stack).
    Last edited by anduril462; 12-10-2010 at 07:16 PM. Reason: forgot a printf and fixed some typos

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