Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Author: Jiri Zahradnik
- * Date 13. Nov 2014
- * Purpose: pointer logic demonstration
- */
- #include <stdio.h>
- int main(){
- int *i; /* here we have a pointer */
- /* pointer is just an arrow which points, through dereferencing (*) and referencing(&)
- we can get its adress(reference) or value stored at that adress(dereference)
- */
- i = malloc(sizeof(int));/* to store anything, we must allocate memory for value
- malloc returns pointer(adress) of allocated memory
- */
- /* now we want to store a value in that memory, we use dereference(symbol: *), it means this:
- Program! I want to write THIS value in THIS memory pointed by pointer i
- */
- *i = 5;
- /* what we do NOT want to do, is modifying adress, so this:
- &i = 5;
- will change adress and not only we lose access to that memory block,
- we cause crash of that program
- */
- /* now we want to print adress of i */
- printf("%0xp\n", &i);
- /* now we want to print value of i */
- printf("%d\n", i);
- /* and now we want to free memory */
- free(i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement