





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Intro Prog&Prob Solving; Subject: Engineering Computer Science; University: University of California - Davis; Term: Spring 2002;
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






This program reads an input line, and retains the newline; normally you would have a function to do this (there are some in the Standard I/O Library, for instance) but it does show how to use character arrays and pointers. This ver- sion uses arrays.
#include <stdio.h>
#define BUFSIZE 1024 /* maximum input line length */
/*
/*
/*
This is exactly the same program, but uses a pointer to put elements into the buffer:
#include <stdio.h>
#define BUFSIZE 1024 /* maximum input line length */
/*
do{ /* load up to BUFSIZE-1 chars into the buffer */ l = &line[0]; while(l < &line[BUFSIZE-1] && (c = getchar()) != EOF && c != '\n') l++ = c; / end the string with a newline and return success */ if (c == '\n') l++ = c; / tack on the string terminator */ l = '\0'; / print it out */ if (l != line) printf("%3d %s", l - line, line); } while (c != EOF);
/*
/*
/*
/*
/*
How do you pass arrays, and how do you reference them? This little goodie shows that arrays and pointers can be passed indiscriminately, so long as the prototypes and definitions are consistent.
#include <stdio.h>
#define MAXLINE 1000 /* maximum allowed line length */
/*
/*
/*
/*
void str3copy(char new[], char old[]) { int i; /* counter in a for loop */
/*
/*
This program , split over three pages, shows how to open, close, and read from a file using the Standard I/O Library; it also shows how to use static variables to keep information across function calls.
#include <stdio.h>
#define MAXFILENAMESIZE 1024 /* max length of file name / #define IN_WORD 1 / currently inside a word / #define NOTIN_WORD 0 / currently not in a word */
/*
/*
/*
/*