Pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bhavana1
    New Member
    • Dec 2017
    • 1

    Pointers

    how to get started with pointers of c language and how can i get a good grip on them ?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Just read more than one book on C.

    Pointers aren't as bad as they seem.

    This is easy:

    Code:
    int x;
    This creates an int variable named x;

    Then:

    Code:
    int x,y;
    This creates two int variables named x and y.

    Now the good part:

    Code:
    int x,y,*c;
    This creates three variables. x and y are int. And *c is also an int.

    c is a pointer to an int. To see the int you must use *c to dereference c. Use the * dereference operator. c will refer to an int by containing the address of the int:

    Code:
    int x = 10;
    printf("%d", x);  you will see 10
    Then:
    Code:
    int x = 10;
    int *c;           there is no address of an int in c
    
    c = &x            put the address of x into c
    
    printf("%d", *c);  you will see 10
    This last printf can print any int as long as the address of that int has been put into c. So now you can write one function that will print any int rather than a separate function for each int. The pointer lets you get around the name of the int.

    But beware: At all times c must contain the address of the correct int before you use c. You are responsible to see this happens.

    Write a few simple programs and try this out.

    Post again for more info.

    Comment

    • alexhuber
      New Member
      • Feb 2018
      • 7

      #3
      Pointers are all about & and * so the only way is to practice it like all other topics.
      A pointer is easy but seems difficult at the start.
      Use book by Denis Ritchie(creator of c language).

      Comment

      Working...