lecture_18_revised

lecture_18_revised - /* cptr set to the base address of the...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
1 · lecture_18_addition · 2010-10-20 20:54 · David Nicol Example - read string of text from the keyboard and count the number of times the character 'a' is found #include <stdio.h> #define MAX_STRING_LEN 50 int main() { char text[MAX_STRING_LEN]; const char testchar = 'a'; int i, count=0; printf( "Enter string:" ); gets(text); /* get line of text from user */ for(i=0; i<MAX_STRING_LEN; i++) { if (text[i] == testchar ) count++; else if(text[i] == '\0') break; } printf( "String %s has %d occurences of character %c\n" , text, count, testchar); return 0; } - Relationship between arrays and pointers char word[10]; /* array of ten characters */ char *cptr; /* variable that holds the address of a character */ cptr = word;
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: /* cptr set to the base address of the array. Same as cptr = &word[0]; */ cptr and word both refer to the first element in the array. The following are equivalent if cptr is not changed after initial assignment word[3] cptr[3] *(cptr+3) But if you were to change cptr, e.g., cptr++, then cptr[3] would not be the same as word[3] IMPORTANT cptr is a variable and its value can be changed word is an array identifier and cannot be changed char oneword[10]; char anotherword[25]; char *cptr; /* the next two statements are legal */ cptr = oneword; cptr = anotherword; /* the next two statements are not legal */ oneword = cptr; oneword = anotherword;...
View Full Document

This note was uploaded on 08/29/2011 for the course ECE 265 taught by Professor Kosbar during the Fall '09 term at Missouri S&T.

Page1 / 10

lecture_18_revised - /* cptr set to the base address of the...

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

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