[Courses] [C] Lesson12: Exercise answers
Laura Bowser
lbowser at andrew.cmu.edu
Thu Nov 21 10:49:25 EST 2002
> [1] Write a program that uses pointers to set each element of an
> array to zero.
I couldn't resist, and I'm going to "cheat" here, this solution technically
meets all the requirements, but may not be the easiest to understand - I put
in comments where I felt appropriate, but feel free to ask me about anything
in this program.
#define N 30
void main(void) {
int *array; /* create the array- here's the pointer */
array = (int *) calloc(N, sizeof(int)); /* set every element to zero */
/* calloc allocates space for N things, of sizeof(int) size and sets it to an
appropriate initial value. In this case 0 because I'm dealing with ints, if
I was dealing with pointers, then it would become NULL. */
}
Laura
--
Public key available at
http://www.elwing.org/~elwing/lbowser.gpg
More information about the Courses
mailing list