"C and Pointers" study notes (1)

Source: Internet
Author: User

Recently the C language has been completed, the layout of the big homework: Student Management System 5 version is also finished. But also bought a copy of the "C and the hands", mainly to feel their hands are still not fully familiar with. So we have to do a good study. Gossip does not say much, directly to the first chapter. A look is quick to get started, thought is very simple, but that program took me a half-day to understand, according to the author said that did use the C language most of the skills.

Program 1.1: First read a list of labels, which appear in pairs, that is, the input row column range. This list of labels ends with a negative value as the closing flag. The remaining input lines are read and printed by the program, and the strings in the selected range in the input lines are extracted and printed.

The code in the book is as follows:

#include <stdio.h>#include<stdlib.h>#include<string.h>#defineMax_clos 20/* The maximum number of columns that can be processed */#defineMax_input 1000 * * Maximum length per number */intRead_column_numbers (intColumns[],intmax);voidRearrange (Char*output,Char Const*input,intN_columns,int ConstColumns[max_clos]);intMainvoid){   intN_columns;/*column labels for processing*/   intColumns[max_clos];/*number of columns to process*/   CharInput[max_input];/*array of inputs that need to be accommodated*/   CharOutput[max_input];/*An array that accommodates the output rows*/N_columns=read_column_numbers (Columns,max_clos);  while(Gets (input)!=null)/*The program ends if the input line is not present in the read-in*/{printf ("Original Input:%s\n", input);       Rearrange (output,input,n_columns,columns); printf ("rearranged line:%s\n", output); }   returnexit_success;}/*read the column labels that need to be processed and ignore them if they exceed the specified range*/intRead_column_numbers (intColumns[],intmax) {    intnum=0; intch;  while(NUM&LT;MAX&AMP;&AMP;SCANF ("%d", &columns[num]) = =1&&columns[num]>0)    {        /*this loop condition guarantees that the read line number does not exceed the maximum, and that the short-circuit characteristics of && are not read by scanf even if the maximum line number is exceeded, while the scanf guarantees that the integer data is read, and the following conditions are entered as positive */Num+=1; }    /*determine if the data read is in pairs*/    if(num%2) {puts ("Last column number was not paired.");    Exit (Exit_failure); }     while((Ch=getchar ())!=eof&&ch!='\ n');/*used to handle all characters that contain the last negative value*/    returnnum;}/*working with input rows*/voidRearrange (Char*output,Char Const*input,intN_columns,int ConstColumns[max_clos]) {    intCol/*Subscript of the columns array*/    intOutput_col;/*column counters for output rows*/    intLen/*the length of the input line*/    intNchars;/*The length between pairs of processed columns*/Len=strlen (input); Output_col=0;  for(col=0; col<n_columns;col+=2)    {        if(columns[col]>=len| | output_col==max_input-1)/*if the label of the input line is less than the column label to be processed or the output array is full End Task*/             Break; Nchars=columns[col+1]-columns[col]+1; if(output_col+nchars>max_input-1); Nchars=max_input-output_col-1;/*If the data space is insufficient, only the data that can be accommodated will be processed.*/strncpy (Output+output_col,input+columns[col],nchars); Output_col+=Nchars; } Output[output_col]=' /';}

A little thought: I have to say, Kenneth This kind of master thinking is really perfect, if I write indeed many places do not think (how dare I and master than 2333!) ), such as while (Ch=getchar ()) that statement, at first I was baffled, and later understood to clear the negative character (including the negative value itself) because these characters are not read away, and still exist in the stdin. So when the main function calls get this function to read, these unread characters will be interpreted as the first row of data, so that we can not get the results we want. He also dealt with situations that could result in cross-border access to the array. Think of yourself in the writing of the time never cared about these, are out of bounds before the debug to find out. Another detail is that he passed in the input array of the rearrange function, adding a prefix const to prevent possible changes within the function. This is what I need to learn.

But I also have some objection to the while condition of his main function setting, which gets null when the get function reads to the end of the file (EOF), which is really suitable for us to open a file for reading, because the end of the file must have EOF. But if we are in the keyboard input, we only Ctrlz can get EOF, this always makes me feel very uncomfortable, in fact, I am lazy not to press Ctrlz, in addition to the book is not very understanding of its input sample (you can go to see), there is no ctrlz, so, I don't know the meaning of the author very well. But there seems to be no other better way. Consider the need to enter Ctrlz for the moment. I was drunk for so long on this issue.

This program is really classic, contains a lot of things but not difficult, in fact, you can follow the example of the output, and then you will get very interesting results. I feel that I have learned a semester C, or nothing, I hit the time to play, and then read the book program is also seen a half-day ... Maybe it's because you've been watching Java lately. Not a day to relax ah ...

"C and Pointers" study notes (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.