1 / 11

CS 450 MPX Project

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

blade
Download Presentation

CS 450 MPX Project

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 450MPX Project A Quick C Review

  2. 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

  3. 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

  4. 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’.

  5. 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”;

  6. 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.

  7. 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.

  8. 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

  9. 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

  10. 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

  11. 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

More Related