⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap6.lst

📁 Borland C++ Builder The Complete Reference 例程源代码
💻 LST
字号:
listing 1
p = #

listing 2
q = *p;

listing 3
#include <stdio.h> 
 
int main(void) 
{ 
  int num, q; 
  int *p; 
 
  num = 100; /* num is assigned 100 */ 
  p = &num;  /* p receives num's address */ 
  q = *p;    /* q is assigned num's value 
                  indirectly through p */ 
 
  printf("%d", q); /* prints 100 */ 
 
  return 0; 
}

listing 4
#include <stdio.h> 
 
int main(void) 
{ 
  double x, y; 
  int  *p; 
 
  x = 100.123; 
 
  p = &x; 
  y = *p; 
  printf("%f", y);  /* this will be wrong */ 
 
  return 0; 
}

listing 5
#include <stdio.h> 
 
int main(void) 
{ 
  int x; 
  int *p1, *p2; 
 
  p1 = &x; 
  p2 = p1; 
 
  /* This will display the addresses held by 
     p1 and p2. They will be the same. 
  */ 
  printf("%p  %p", p1, p2); 
 
  return 0; 
}

listing 6
p1++;

listing 7
p1--;

listing 8
p1 = p1 + 9;

listing 9
if(p<q) printf("p points to lower memory than q\n");

listing 10
#include <stdio.h> 
#include <stdlib.h> 
 
#define STCKSIZE 50 
 
void push(int i); 
int pop(void); 
 
int *p1, *tos, stack[STCKSIZE]; 
 
int main(void) 
{ 
  int value; 
 
  p1 =  stack; /* assign p1 the start of stack */ 
  tos = p1;  /* let tos hold top of stack */ 
 
  do { 
    printf("Enter a number (-1 to quit, 0 to pop): "); 
    scanf("%d", &value); 
    if(value!=0) push(value); 
    else printf("this is it %d\n", pop()); 
  } while(value!=-1); 
  return 0; 
} 
 
void push(int i) 
{ 
  p1++; 
  if(p1==(tos + STCKSIZE)) { 
    printf("stack overflow"); 
    exit(1); 
  } 
  *p1 = i; 
} 
 
int pop(void) 
{ 
  if(p1==tos) { 
    printf("stack underflow"); 
    exit(1); 
  } 
  p1--; 
  return *(p1+1); 
}

listing 11
return *p1 + 1;

listing 12
char *p; 
p = (char *) malloc(25); 

listing 13
int *p; 
p = (int *) malloc(50*sizeof(int));

listing 14
int *p; 
if((p = (int *) malloc(100))==NULL) { 
  printf("Out of memory.\n"); 
  exit(1); 
}

listing 15
#include <stdio.h> 
 
void sp_to_dash(const char *str); 
 
int main(void) 
{ 
  sp_to_dash("this is a test"); 
 
  return 0; 
} 
 
void sp_to_dash(const char *str) 
{ 
  while(*str) { 
    if(*str == ' ') printf("%c", '-'); 
    else printf("%c", *str); 
    str++; 
  } 
}

listing 16
/* This is wrong. */ 
void sp_to_dash(const char *str) 
{ 
  while(*str) { 
    if(*str == ' ') *str = '-'; /* can't do this */ 
    printf("%c", *str); 
    str++; 
  } 
}

listing 17
char str[80], *p1; 
p1 = str;

listing 18
str[4]

listing 20
/* Use array. */ 
int puts(const char *s) 
{ 
  register int t; 
 
  for(t=0; s[t]; ++t) putchar(s[t]); 
  return 1; 
} 
 
/* Use pointer. */ 
int puts(const char *s) 
{ 
  while(*s) putchar(*s++); 
  return 1; 
}

listing 21
/* Use pointers. */ 
int strcmp(const char *s1, const char *s2) 
{ 
  while(*s1) 
    if(*s1-*s2) 
      return *s1-*s2; 
    else { 
      s1++; 
      s2++; 
    } 
  return 0; /* equal */ 
}

listing 22
while (*s1)

listing 23
/* This program is incorrect. */ 
 
#include <stdio.h> 
#include <string.h> 
 
int main(void) 
{ 
  char *p1, s[80]; 
 
  p1 = s;  /* assign p1 the starting address of s */ 
  do { 
    printf("\nEnter string: "); 
    gets(s);  /* read a string */ 
 
    /* print the decimal equivalent of each 
       character */ 
    while(*p1) printf(" %d", *p1++); 
 
  } while(strcmp(s, "done")); 
  return 0; 
}

listing 24
/* This program is correct. */ 
 
#include <stdio.h> 
#include <string.h> 
 
int main(void) 
{ 
  char *p1, s[80]; 
 
  do { 
    p1 = s; /* assign p1 the starting address of s */ 
    printf("\nEnter string: "); 
    gets(s);  /* read a string */ 
 
    /* print the decimal equivalent of each 
       character */ 
    while(*p1) printf(" %d", *p1++); 
 
  } while(strcmp(s, "done")); 
  return 0; 
}

listing 25
int *x[10];

listing 26
x[2] = &var;

listing 27
*x[2]

listing 28
void display_array(int *q[]) 
{ 
  int t; 
 
  for(t=0; t<10; t++) 
    printf("%d ", *q[t]); 
}

listing 29
void serror(int num) 
{ 
  static char *err[] = { 
    "Cannot Open File\n", 
    "Read Error\n", 
    "Write Error\n", 
    "Media Failure\n" 
  }; 
 
  printf("%s", err[num]); 
}

listing 30
float **newbalance;

listing 31
#include <stdio.h> 
 
int main(void) 
{ 
  int x, *p, **q; 
 
  x = 10; 
  p = &x; 
  q = &p; 
 
  printf("%d", **q); /* print the value of x */ 
 
  return 0; 
}

listing 32
 /* Look up a name. */ 
int search(char *p[], char *name) 
{ 
  register int t; 
  for(t=0; p[t]; ++t) 
    if(!strcmp(p[t], name)) return t; 
 
  return -1; /* not found */ 
}

listing 33
char *p = "hello world\n";

listing 34
#include <stdio.h> 
#include <string.h> 
 
char *p = "hello world\n"; 
 
int main(void) 
{ 
  register int t; 
 
  /* print the string forward and backwards */ 
  printf(p); 
  for(t=strlen(p)-1; t>-1; t--) printf("%c", p[t]); 
  return 0; 
}

listing 35
void enter(void), del(void), review(void), quit(void); 
int menu(void); 
 
void (*options[])(void) = { 
  enter, 
  del, 
  review, 
  quit 
} ;

listing 36
#include <stdlib.h> 
#include <stdio.h> 
#include <conio.h> 
#include <string.h> 
 
void enter(void), del(void), review(void), quit(void); 
int menu(void); 
 
void (*options[])(void) = { 
  enter, 
  del, 
  review, 
  quit 
} ; 
 
int main(void) 
{ 
  int i; 
 
  i = menu(); /* get user's choice */ 
 
  (*options[i])();  /* execute it */ 
  return 0; 
 
} 
 
int menu(void) 
{ 
  char ch; 
 
  do { 
 
    printf("1. Enter\n"); 
    printf("2. Delete\n"); 
    printf("3. Review\n"); 
    printf("4. Quit\n"); 
    printf("Select a number: "); 
    ch = getche(); 
    printf("\n"); 
  } while(!strchr("1234", ch)); 
  return ch-49; /* convert to an integer equivalent */ 
} 
 
void enter(void) 
{ 
  printf("\nIn enter."); 
} 
 
void del(void) 
{ 
  printf("\nIn del."); 
} 
 
void review(void) 
{ 
  printf("\nIn review."); 
} 
 
void quit(void) 
{ 
  printf("\nIn quit."); 
  exit(0); 
}

listing 37
/* This program is wrong. */ 
 
int main(void) 
{ 
  int x, *p; 
 
  x = 10; 
  *p = x; 
 
  return 0; 
}

listing 38
#include <stdio.h> 
 
/* This program is wrong. */ 
int main(void) 
{ 
  int x, *p; 
 
  x = 10; 
  p = x; 
  printf("%d", *p); 
 
  return 0; 
}

listing 39
p = x;

listing 40
p = &x;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -