Jump to content

C pointers

- - - - -

  • Please log in to reply
No replies to this topic

#1
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
/*
* Pointers are variables
* pointers need to be assigned like variables
* pointers contain memory addresses
* often to another variable
* x = 5
* ptr = 103 (NOT INTEGER, BUT MEMORY ADDRESS)
* 103 is the memory address of x in this case
* incoming syntax
* NOW, HOW TO GET A VARIABLE ADDRESS
* &x shows the memory address
* takes the address of another variable
* if you want the contents of what the pointer points to
* we put a star at the beginning, *ptr for example
* *ptr = 5 whereas ptr = 103
* the star is also used to declare
* int *ptr;
*/

main();

{

       //X is an integer

       int  x;

       // *ptr is a pointer to an integer

       int  *ptr;

       

       /*

        * set pointer to the memory address of x.

        * pointer initialisation

        */

        *ptr = x; /* LEGAL, but ptr is not initialised */

        &ptr = x; /* ILLEGAL - attempting to move ptr in memory */

        ptr = &x; /* LEGAL - initialising pointer! */

        ptr = *x; /* ILLEGAL x cannot point to anything! */

}

       /*

        * set the contents of ptr to x, NOT WHAT IT POITNS TOO  ptr = 5; 

        */


/*
* Pointers to structures
* struct employee *emp_p;
* emp_p.age = 34;
* emp_p is not a structure but is a pointer to a structure
* *emp_p.age = 34 would not work because . takes precident over *
* (*emp_p).age = 34; we need neater way
* emp_p->age = 34; this is to find one of the fields of a pointer of a structure
*/

These are just short notes about pointers in C that I took during some tutorials. :), hope you all can understand them and some use to anyone. :D




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users