110 likes | 232 Vues
CS 450 MPX Project. A Quick C Review. Pointers and addresses in C. Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will need
E N D
CS 450MPX Project A Quick C Review
Pointers and addresses in C • Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will need • Wikipedia: In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. • A pointer is a variable that holds an address to another memory location, not an actual value. • To declare a variable as a pointer, use the *: • int *myIntPointer; • The myIntPointer variable will hold an address to a memory location that contains a value of type int
Pointers in C • To access the value held in a pointer, you need to dereference it • int *myIntPointer; //declare pointer • *myIntPointer = 30; //assign value to the referenced address • *myIntPointer++; //access that value • If you code myIntPointer = 30; you are not changing the value referenced by myIntPointer, you are changing the address myIntPointer references • To access the address of a variable, use &: • int myInt = 8;// new int variable, not a pointer • myIntPointer = &myInt; //copy address of myInt into myIntPointer
Common Pointer Errors int *a = 8; a = 19; //should say *a = 19. int b = 20; a = b; //should be *a = b to set the value referenced by ‘a’ equal to the value of ‘b’ OR a = &b to set the address referenced by ‘a’ = the address of ‘b’.
Strings In C • Strings in C are stored as character arrays and must be “null terminated”. This means that the last character in the string should be ‘\0’ • Declare a string as a character array and give it a maximum size. Use this method if you do not know what you wish to put in the String at compile time: • char[20] myString; • Declare a string as a character pointer. Use this method if you know the exact string you wish to store in this character array at compile time and do not plan on storing a longer string in it later: • char* myString = “hello world\0”;
Copying and Comparing Strings • Copy the value of one string to another: • Do NOT use myString1 = myString2 or *myString1 = *myString2 • Use strcpy: • strcpy(myString1, myString2); • This will copy the value held in myString2 into myString1 • Compare two strings • Do NOT use myString1 == myString2, etc. • Use strcmp(myString1, myString2); • If equal, returns 0, otherwise returns something else.
C Strings • Potential Problems: • You declare a character array of size X, then store a string of size > X in that array. • You declare a character pointer and later try to store a string in it (since you declared it as a * with no initial value, no memory is allocated for the string): char *myCharPointer; …… code …. *myCharPointer = “hello world\0”; • Not null terminating your strings- printf, strcpy, strcmp find the end of a string by looking for \0.
Compiler Directives • Use “#” to give specific instructions to the compiler • #include: essentially directs the compiler to “copy and paste” the contents of a file into another file • #include “MyHeaderFile.H” • #include <dos.h> • Use “…” for header files you create, <…> for system header files • NEVERinclude “.C” files • #define: symbolic constants • #define MYCONSTANT 40 • Typically in all upper case • In this example, every time you type “MYCONSTANT” in your code, the compiler will replace it with “40” • Allows you to define at compile time constants that you will use throughout your code • Use for array sizes, giving numeric values to categories, and register addresses
Header Files • C programs are comprised of two types of files • Source Files (.C) • Header Files (.H) • Source files contain all of your source code • Header files contain prototypes for all of your functions, definitions of structures, symbolic constants, and global variable declarations. • For each source (.C) file, there should be a header file (.H) • You should link the source files together, and include header files in the source files
Proper Use of Header Files • If the function prototypes contained in the header files are included more than once, errors will be generated by the compiler. • To avoid this problem, wrap the header files in an if…end if statement: #ifndef MYHFILE_ #define MYHFILE_ ….. Header file contents ….. #endif
Online Resources • http://www.cprogramming.com • In depth tutorials and reviews of just about every C topic you will need in MPX, including basic syntax, file I/O, C-strings, pointers, function pointers, structures, linked lists, and bitwise operators. • http://home.netcom.com/~tjensen/ptr/pointers.htm • Pointers and arrays