From 86885dab5ca005ba72c21b80f7f55a9b92e17a6d Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Fri, 28 Jun 2013 21:47:31 -0400 Subject: Update pointer examples and comments --- c.html.markdown | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 15bfa05e..3f9b6c61 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -194,26 +194,41 @@ printf("%d\n", (short) 65537); // => 1 (Max short = 65535) // Pointers /////////////////////////////////////// -// You can retrieve the memory address of your variables, -// then mess with them. +// A pointer is a variable declared to store a memory address. Its declaration will +// also tell you the type of data it points to. You can retrieve the memory address +// of your variables, then mess with them. int x = 0; printf("%p\n", &x); // Use & to retrieve the address of a variable // (%p formats a pointer) // => Prints some address in memory; +// Pointer types end with * in their declaration +int* px; // px is a pointer to an int +px = &x; // Stores the address of x in px +printf("%p\n", px); // => Prints some address in memory + +// To retreive the value at the address a pointer is pointing to, +// put * in front to de-reference it. +printf("%d\n", *px); // => Prints 0, the value of x, which is what px is pointing to the address of + +// You can also change the value the pointer is pointing to. +// We'll have to wrap the de-reference in parenthesis because +// ++ has a higher precedence than *. +(*px)++; // Increment the value px is pointing to by 1 +printf("%d\n", *px); // => Prints 1 +printf("%d\n", x); // => Prints 1 + int x_array[20]; // Arrays are a good way to allocate a contiguous block of memory int xx; for(xx=0; xx<20; xx++){ x_array[xx] = 20 - xx; } // Initialize x_array to 20, 19, 18,... 2, 1 -// Pointer types end with * int* x_ptr = x_array; -// This works because arrays are pointers to their first element. +// This works because an array name is bound to the address of its first element -// Put a * in front to de-reference a pointer and retrieve the value, -// of the same type as the pointer, that the pointer is pointing at. +// Arrays are pointers to their first element printf("%d\n", *(x_ptr)); // => Prints 20 printf("%d\n", x_array[0]); // => Prints 20 -- cgit v1.2.3 From db168d11be3306c88eb90f775fda15fcafe3ce82 Mon Sep 17 00:00:00 2001 From: Kai Mallea Date: Fri, 28 Jun 2013 21:54:28 -0400 Subject: Update comment --- c.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 3f9b6c61..36bd07fd 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -225,6 +225,7 @@ for(xx=0; xx<20; xx++){ x_array[xx] = 20 - xx; } // Initialize x_array to 20, 19, 18,... 2, 1 +// Declare a pointer of type int and initialize it to point to x_array int* x_ptr = x_array; // This works because an array name is bound to the address of its first element -- cgit v1.2.3 From 30e0d68cd9d3ad2fe35bb895feb1388539a91e5a Mon Sep 17 00:00:00 2001 From: Dario Sneidermanis Date: Fri, 28 Jun 2013 23:19:25 -0300 Subject: Fix precision typo with C integer types --- c.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 15bfa05e..413aad5f 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -50,8 +50,8 @@ short x_short = 0; char x_char = 0; char y_char = 'y'; // Char literals are quoted with '' -long x_long = 0; // Still 32 bytes for historical reasons -long long x_long_long = 0; // Guaranteed to be at least 64 bytes +long x_long = 0; // Still 32 bits for historical reasons +long long x_long_long = 0; // Guaranteed to be at least 64 bits // 32-bit floating-point decimal float x_float = 0.0; -- cgit v1.2.3