1 / 15

Thursday, February 28, 2013

Thursday, February 28, 2013. Pointers, Strings. NOTES. Project 2 will go out next Monday Questions?. Pointers. Declaration: <pointer type> *<pointer name> Type: char, float, int , etc Specifies type of variable whose addresses this pointer can store Examples int * intptr = &4;

dessa
Download Presentation

Thursday, February 28, 2013

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. Thursday, February 28, 2013 Pointers, Strings

  2. NOTES • Project 2 will go out next Monday • Questions?

  3. Pointers • Declaration: • <pointer type> *<pointer name> • Type: char, float, int, etc • Specifies type of variable whose addresses this pointer can store • Examples • int *intptr = &4; • Puts address of ‘4’ into intptr • char c = ‘c’; char *cptr = &c

  4. Pointers • Can change variable by accessing it through address • “dereferencing” pointer • Usually, only one return value for function • By passing in pointers (addresses), variables can be modified by dereferencing • constint *q vsint * const q

  5. Dereferencing Pointers int a = 4, b = 5; int *p1 = &a; int *p2 = &b; modify(p1, p2); void modify(int *ptr1, int *ptr2) { *ptr1 = 6; *ptr2 = 3; } Run modifyptr.c

  6. Pointers in Memory • Pointers are variables which store the address of another variable: • Ex: int a = 3; int *p; p = &a; a p

  7. Pointers and Arrays • Name of an array is a constant pointer whose value is the address of the array’s first element • a = &a[0] • Use either pointer arithmetic or indexing to refer to or modify the elements in an array • Arrays are always passed by reference to/from functions

  8. Arrays • Name of array is pointer to first element in array • Ex: int a[] = {1, 2, 3, 4, 5} • Here, a = &a[0]; • What would these return? • *(a+2) => 3 • *(a+3) => 4 • Go over pointers.c

  9. Strings • Strings represented by arrays of characters • Pointer to null-terminated array of characters • char string1[3] = “hi”; • Same as: char s1[3] = {‘h’, ‘i’, ‘\0’}; • Null-terminated • Note size is one more than number of letters in string

  10. Input / Output • %s for strings • No ‘&’ in scanf: • When dealing with int’s: int a; scanf(“%d”, &a); • With strings: char s1[3]; scanf(“%s”, s1); • This is because s1 is already a pointer • Scanning in string longer than the length of array will override adjacent memory space!

  11. #include <ctype.h> The following two functions return non-zero if c follows the condition described; return zero otherwise: • intisupper(int c); • if c is an uppercase letter • intislower(int c); • if c is a lowercase letter ************************************************************* • inttoupper(int c); • converts c to uppercase • inttolower(int c); • converts c to lowercase

  12. #include <string.h> • char *strcpy(const char *dest, const char *src) • Copies the contents of the second into the array referenced by the first, and puts a null character at the end • intstrcmp(const char *s1, const char *s2) • Returns negative number if first string comes before the second alphabetically, 0 if the strings are identical through null, positive if second comes first. • char *strcat(char *s1, const char *s2) • Concatenates s2 onto the end of s1, and adds a null character at the end • intstrlen(const char *str) • Returns the length of strexcluding ‘\0’

  13. Mistakes with Strings • Forgetting about the null character, or forgetting to include array space for it • Setting strings equal to each other instead of using strcpy • Single quotes vs. double quotes • Using the ampersand with scanf

  14. Note the difference: char s1[] = “winter is coming soon”; char *s2 = “winter is coming soon”; • s1 is a constant pointer which will always point to a specific memory location; the first element of the character array. • s2 is a pointer which points to a string constant. You can’t modify individual elements of the array of memory that it points to! • Go over string.c

  15. Coding Assignment • Create a file called data.txt with the following numbers: • 99 32 46 53 42 33 47 58 21 101 • Read in this file using file I/O and store the numbers in an array • Loop through the array (USING POINTERS ONLY) • Add 5 to those elements with an even index • Subtract 5 to those elements with an odd index • Print out the final array (again if using loops, only pointers) • So no a[0], etc!!

More Related