C Programming Tutorial (K&R version 4) _Part3

C Programming Tutorial (K&R version 4) _Part3 -...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Arrays as Parameters What happens if we want to pass an array as a parameter? Does the program copy the entire array into local storage? The answer is no because it would be a waste of time and memory. Arrays can be passed as parameters, but only as variable ones. This is a simple matter, because the name of the array is a pointer to the array. The Game of Life program above does this. Notice from that program how the declarations for the parameters are made. main () { char array[23]; function (array); ..... } function (arrayformal) char arrayformal[23]; { } Any function which writes to the array, passed as a parameter, will affect the original copy. Array parameters are always variable parameters Node:Questions 19, Previous: Arrays as Parameters , Up: Arrays Questions 1. Given any array, how would you find a pointer to the start of it? 2. How do you pass an array as a parameter? When the parameter is received by a function does C allocate space for a local variable and copy the whole array to the new location? 3. Write a statement which declares an array of type double which measures 4 by 5. What numbers can be written in the indicies of the array? Node:Strings, Next: Putting together a program , Previous: Arrays , Up: Top Strings Communication with arrays. Strings are pieces of text which can be treated as values for variables. In C a string is represented as some characters enclosed by double quotes. "This is a string" A string may contain any character, including special control characters, such as \n , \r , \7 etc... "Beep! \7 Newline \n..." Conventions and Declarations : Strings Arrays and Pointers : Arrays of Strings : Example 21 : Strings from the user : Handling strings : Example 22 : String Input/Output : Example 23 : Questions 20 : Node:Conventions and Declarations, Next: Strings Arrays and Pointers , Previous: Strings , Up: Strings Conventions and Declarations There is an important distinction between a string and a single character in C. The convention is that single characters are enclosed by single quotes e.g. * and have the type char. Strings, on the hand, are enclosed by double quotes e.g. "string..." and have the type "pointer to char" (char *) or array of char. Here are some declarations for strings which are given without immediate explanations. /**********************************************************/ /* */ /* String Declaration */ /* */ /**********************************************************/ #define SIZE 10 char *global_string1; char global_string2[SIZE]; main () { char *auto_string; char arraystr[SIZE]; static char *stat_strng; static char statarraystr[SIZE]; } Node:Strings Arrays and Pointers, Next: Arrays of Strings , Previous: Conventions and Declarations , Up: Strings Strings, Arrays and Pointers A string is really an array of characters. It is stored at some place the memory and is given an end marker which standard library functions can recognize as being the end of the string. The end marker is called the zero (or of the string....
View Full Document

Page1 / 84

C Programming Tutorial (K&R version 4) _Part3 -...

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

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