0% found this document useful (0 votes)
144 views10 pages

Extra in C HTTPWWW.C For

This document provides information about pointers in C including: - A pointer must be the same type as the variable it points to - Declaring a pointer does not create the variable, it creates the pointer - Pointers must be initialized before use by assigning the address of a variable - To access the value of a variable through a pointer, use an asterisk - Pointer arithmetic and operators like ++ can be used to modify or increment pointers

Uploaded by

SarAona HemBram
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views10 pages

Extra in C HTTPWWW.C For

This document provides information about pointers in C including: - A pointer must be the same type as the variable it points to - Declaring a pointer does not create the variable, it creates the pointer - Pointers must be initialized before use by assigning the address of a variable - To access the value of a variable through a pointer, use an asterisk - Pointer arithmetic and operators like ++ can be used to modify or increment pointers

Uploaded by

SarAona HemBram
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Pointer Cheat Sheet

This page is based on information that appeared on the tear-out Cheat Sheet from C For Dummies, Volume II.

Pointer Pointers

A pointer must always be of the same type as the variable it's pointing at. Declaring a pointer variable does not create the type of variable it points at. It creates a pointer variable. Though pointers are declared with an asterisk they are not always used with an asterisk. Pointers must be initialized before they can be used. Initialize a pointer by assigning it to a variable (of the same type). To assign a pointer to a variable, use an ampersand with the variable's name.
m_address = &memory;

To assign a pointer to an array, do not use the ampersand:


s_address = string;

To assign a pointer to an array element, use the ampersand:


element = &string[2];

Without an asterisk, an initialized pointer holds a memory address. With an asterisk, an initialized pointer holds a value (the value of the variable the pointer points at).

Typical Pointer Setup and Use


First, create a pointer of the proper type:
float *f;

Second assign it to a variable's memory location:


f = &boat;

Finally, use the pointer:

printf("%.0f",*f);

Without its asterisk, the pointer is a memory location. With its asterisk, the pointer equals the value at the memory location. Always use the same type of pointer as the variables it examines: floats for floats, ints for ints, and so on. Remember: Always initialize a pointer before you use it! Set the pointer equal to the address of some variable in memory.

Pointers, Parenthesis and Math


Pointer Thing
p *p *p++ *(p++) (*p)++ *++p *(++p) ++*p ++(*p) p*++ p++*

Memory Address Yup Nope Incremented after value is read Incremented after value is read Unchanged

Memory Contents Nope Yup Unchanged Unchanged Incremented after it's used

Incremented before value is read Unchanged Incremented before value is read Unchanged Unchanged Unchanged Not a pointer Not a pointer Incremented before it's used Incremented before it's used Not a pointer Not a pointer

The ++ operator is used above, though any math operation can be substituted. As a tip: Use parenthesis to isolate part of the pointer problem and the answer will always work out the way you intended.

Pointers and array brackets


Array Notation Pointer Equivalent
array[0] array[1] array[2] *a *(a+1) *(a+2)

array[3] array[x]

*(a+3) *(a+x)

Ugly ** notation
Doodad
array+1 *(array+1)

What It Is An address

How The Compiler Sees It A pointer

Contents of address, what lives there A string A character Same as above

*(*(array+1)) Contents of a character array **(array+1)

Same as above

Compilers for Unix-based systems... Compilers for Windows... A great on-line C language reference

Unix/FreeBSD/Linux/Mac OS X

As was stated in the book, the gcc compiler comes with your operating system and it will work in the special directories you've set up. You're all set. You are the future. Tip: If you're using the command line, then most command shells support a history function; you can use the up arrow key on the keyboard to recall a previously-typed command. Use this to help you quickly re-edit and re-compile.

Windows

The following information supplements what is found in Appendix A in the books. Please refer there first and then here next.

Suggested Compilers IDEs (Integrated Development Environment) Text Editors Getting GCC to work in any folder (update)

Tip: With most Command Prompt windows you can use the up arrow key on the keyboard to recall a previously-typed command. Use this to help you quickly re-edit and re-compile your stuff.

Suggested Compilers
If you know of any other compilers that are GCC-compatible or based on GCC, please do let me know. I'd be happy to list them here. Send e-mail to dan [at) c-for-dummies.com. Otherwise, here are my recommendations: MinGW: Minimalist GNU For Windows. A port of the GNU gcc compiler that I used for writing the books. To download a copy of MinGW, click this link, which is current as of May, 2011. When you click that link, look for and download the Automated MinGW installer. Run that program to install the rest of MinGW. Remember: MinGW is a command prompt program. It is not a complete environment. Refer here to learn how to get the gcc command to work in your folders when compiling at the command prompt in Windows. Microsoft Visual C++ Express. This program is part of the Visual Studio series from Microsoft. Visit this web site, choose your language, and the program is downloaded. I've not played with it much, but it's been recommended as a good started compiler,

especially if you are familiar with Microsoft's compilers or are considering updating to the full version of Visual Studio in the future. Tiny C. It's fast. It's small. It's very popular. Especially if you have an older PC that's not very fast, I highly recommend the Tiny C compiler. Visit the Tiny C web site and click the link to download the Windows executable.

IDEs
The book doesn't mention any IDEs, but if you're new it might help you to check out a few of them. Especially when you plan on creating large projects, an IDE is a boon to productivity. Remember that I cannot offer support for an IDE, but here are some I recommend or which have been recommended to me. Code :: Blocks. Runs on Linux, Mac, and Windows. A great way to set up a compiler under Windows Vista without having to mess with the intricacies of MinGW. Link. Dev-C++. Lots of readers recommend the Dev-C++ IDE from Bloodshed. Visit their web site for more information.

Text Editors
I suppose I should also mention various text editors available, especially if you're going to be using the command prompt to create your programs (as I do). The editor I use is VIM, though it's advanced, scary, and intimidating: VIM Another popular editor is GNU Emacs, though it also isn't the most friendly editor in the world. For more information on the Windows version, see:http://www.gnu.org/software/emacs/windows/ Here are a few easy-to-learn and use text editors as recommended by one of my Forum contributors, Brad: Notepad2 Notepad++ Crimson Editor

The Geany editor is recommended by reader Paul: Geany Finally, some additional recommendations, though these may not be as easy to use as the previous three: Context PSPad

Getting the GCC command to work in any folder


The best way to get the gcc command to work in any folder on your hard drive is to place the bin folder (where gcc lives) onto the Windows search path. This can be complex, so pay attention! First you need to know where the bin folder is located, the bin folder's path. This is the hardest part because most folks began using computers with Windows, which doesn't emphasize paths as much as command prompt OSs do. If you've installed MinGW according to its wont, then the path to bin may look like this:
C:\MinGW\bin

(This can be seen in the Address Bar if you open the bin folder in a window.) Or if you installed MinGW in the Program
C:\Program Files\MinGW\bin
Files

folder, the path may be:

Or maybe you're using another compiler and the path is something like:
C:\OtherGCC\bin

Or:
C:\Program Files\OtherGCC\bin

Whatever! Write down or memorize or copy that path. It's important. It must be accurate! Second, you need to modify the path environment variable in Windows so that Windows knows where to look for GCC and its companion commands. The following

sections describe how to do this for Windows 7, Vista, and XP andWindows 98. (I no longer support Windows ME and do not have Windows 2000/NT workstation.)

Windows 7 & Windows Vista & Windows XP

Some older versions of the MinGW compiler have trouble installing on Windows Vista. If you are experiencing problems with the MinGW installer in Windows Vista, click here. 1. Open the Control Panel's System icon; the keyboard shortcut is Win+Break (my favorite keyboard shortcut). Win+Break is also the Win+Pause key, should your keyboard not show the Break key. 2. If you're using Windows XP, skip to step 5. 3. In Windows 7 and in Windows Vista, choose Advanced System Settings on the left side of the System window. 4. In Windows Vista, click the Continue button or type in the Administrator's password to get by the UAC warning. 5. If necessary, click the Advanced tab in the System Properties dialog box. 6. Click the Environment Variables button.

7. Choose the Path variable from the bottom part of the dialog box, shown above. 8. Click the Edit button.

9. Click in the Variable value text box. 10. Press the End key on your keyboard so that you may add text to the end of path. 11. Type a semicolon,
;

12. Type the path to the BIN folder, as in:


;c:\mingw\bin

13. Double-check! There should be no spaces in there. 14. Click OK to close the Edit System Variable box. 15. Click OK to close the Environment variables box.

16. Click OK to close the System Properties window. The changes take affect with the next command prompt window you open. If you were successful, then you can run the gcc command in any folder/directory at the command prompt.
Windows 98

1. From the Start button choose the Run command. 2. In the Run dialog box, type msconfig:

3. Click OK. 4. Inside the System Configuration Utility window, click the Autoexec.bat tab. 5. Click to select the PATH line:

6. Click the Edit button.

7. At the end of the existing path, add a semicolon and then the path to MinGW or your C compiler, whatever that is. For example:
;c:\mingw\bin

8. Double-check! 9. Click OK. This change takes affect after you next restart the computer; do so now if prompted. NOTE: If you don't see a line with PATH= on it anywhere in the scrolling window, then you'll have to create one. Click on the New button, then type:
path=c:\mingw\bin

Or specify whatever the path may be to your compiler. Save this, then restart your computer.

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ (online reference)

You might also like