lecture20-mar8 - Announcements Lecture 20 Assignment 4...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Announcements Lecture 20 • Assignment 4 – Q&A; programs due before Thursday midnight • Spring Break is next week – no assignments • Topics for today (Read Ch. 16 , and then 14-15 for this week) – Finish pointers – Stacks and the system stack (not in book) – structs Problems (cont) /* the purpose is to output the hex equivalent of a series of strings of chars that have been input by the user (until QUIT) is entered */ #include <string.h> #include <stdio.h> int main( ) { char *p1=0; char s[80]; p1 = s; gets(s); /* read a string of chars & puts a null at end */ while(!strcmp(s, “QUIT”)) /* sentinel check */ { /* output the hex equivalent of each character */ while(! (*p1= ‘\0’)) { printf (“%x\n”, *p1); p1++; } gets(s); /* read the next input string */ } return 0; } p1 = s; This program has logic defects - can you find them? == (strcmp(s,”QUIT”)!=0) Arrays of Pointers int *pa[20]; int myvar = 0; pa[2] = &myvar; *pa[2] = 100; char *errors [ 6 ] = { "Input exceeds field width", "Out of range", "Printer not turned on", "Paper out", "Disk full", "Disk write error" }; void error(int num) { printf(“%s”, errors[num]); } You can have an array of pointers An array of strings is an array of pointers too Example of pointer arrays int i; char *grandChildren[ ]= {"Abby", "Ella”, "Julianne"}; for (i= 0; i < 3; i++} { printf (“Child %d is %s\n",i+1, grandChildren[i]); } Child 1 is Abby Child 2 is Ella Child 3 is Julianne Will print: As a 2D Array of chars • Store the names of planets of our solar system as a 2D array of chars, with strings as the rows char planets [8] [8] = {“Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”}; • Why 8 X 8 ?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

lecture20-mar8 - Announcements Lecture 20 Assignment 4...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online