CHAP5

代码 1
m = &count;


代码 2
q = *m;



代码 3
#include <stdio.h>

int main(void)
{
  int x = 99;
  int *p1, *p2;    //定义指针变量

  p1 = &x;
  p2 = p1;

  /* print the value of x twice */
  printf("Values at p1 and p2: %d %d\n", *p1, *p2); 

  /* print the address of x twice */
  printf("Addresses pointed to by p1 and p2: %p %p", p1, p2); //打印出指针

  return 0;
}



代码 4
#include <stdio.h>

int main(void)
{
  double x = 100.1, y;
  int  *p;

  /* The next statement causes p (which is an integer pointer) to point to a double. */
  p = (int *) &x;

  /* The next statement does not operate as expected. */
  y = *p; /* attempt to assign y the value x through p */

  /* The following statement won't output 100.1. */
  printf("The (incorrect) value of x is: %f", y);

  return 0;
}


代码 5
p1++;


代码 6
p1--;


代码 7
p1 = p1 + 12;


代码 8
if(p < q) 
    printf("p points to lower memory than q\n");


代码 9    入栈 出栈
#include <stdio.h>
#include <stdlib.h>

#define SIZE 10

void push(int i);
int pop(void);

int  *tos, *p1, stack[SIZE];

int main(void)
{
  int value;

  tos = stack; /* tos points to the top of stack */
  p1 = stack; /* initialize p1 */

  do {
    printf("Enter value: ");
    scanf("%d", &value);

    if(value != 0) push(value);
    else printf("value on top is %d\n", pop());

  } while(value != -1);

  return 0;
}

void push(int i)
{
  p1++;
  if(p1 == (tos+SIZE)) {
    printf("Stack Overflow.\n");
    exit(1);
  }
  *p1 = i;
}

int pop(void)
{
  if(p1 == tos) {
    printf("Stack Underflow.\n");
    exit(1);
  }
  p1--;
  return *(p1+1);
}


代码 10
return *p1+1;


代码 11
char str[80], *p1;
p1 = str;


代码 12
str[4]


代码 14
/* Index s as an array. */
void putstr(char *s)
{
  register int t;

  for(t=0; s[t]; ++t) putchar(s[t]);
}

/* Access s as a pointer. */
void putstr(char *s)
{
  while(*s) putchar(*s++);
}


代码 15
int *x[10];


代码 16
x[2] = &var;


代码 17
*x[2]


代码 18
void display_array(int *q[])
{
  int t;

  for(t=0; t<10; t++)
    printf("%d ", *q[t]);
}


代码 19
void syntax_error(int num)
{
  static char *err[] = {
    "Cannot Open File\n",
    "Read Error\n",
    "Write Error\n",
    "Media Failure\n"
  };

  printf("%s", err[num]);
}


代码 20
float **newbalance;


代码 21
#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;
}


代码 22
char *p = 0;

代码 23
p = NULL;

代码 24
int *p = 0;
*p = 10; /* wrong! */

代码 25
#include <stdio.h>
#include <string.h>

int search(char *p[], char *name);

char *names[] = {
  "Herb",
  "Rex",
  "Dennis",
  "John",
  NULL}; /* null pointer constant ends the list */

int main(void)
{
  if(search(names, "Dennis") != -1)
    printf("Dennis is in list.\n");

  if(search(names, "Bill") == -1)
    printf("Bill not found.\n");

  return 0;
}

/* 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 */
}


代码 26
char *p = "hello world";


代码 27
#include <stdio.h>
#include <string.h>

char *p = "hello world";

int main(void)
{
  register int t;


  /* print the string forward and backwards */
  printf(p,"\n");
  for(t=strlen(p)-1; t>-1; t--) 
    printf("%c\n", p[t]);

  return 0;
}


代码 28
#include <stdio.h>
#include <string.h>

void check(char *a, char *b,
int (*cmp)(const char *, const char *));

int main(void)
{
  char s1[80], s2[80];
  int (*p)(const char *, const char *); /* function pointer */

  p = strcmp; /* assign address of strcmp to p */

  printf("Enter two strings.\n");
  gets(s1);
  gets(s2);

  check(s1, s2, p); /* pass address of strcmp via p */

  return 0;
}

void check(char *a, char *b,
int (*cmp)(const char *, const char *))
{
  printf("Testing for equality.\n");
  if(!(*cmp)(a, b)) printf("Equal");
  else printf("Not Equal");
}

代码 29
(*cmp)(a, b)

代码 30
cmp(a, b); 

代码 31
check(s1, s2, strcmp);

代码 32
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

void check(char *a, char *b,
int (*cmp)(const char *, const char *));
int compvalues(const char *a, const char *b);

int main(void)
{
  char s1[80], s2[80];

  printf("Enter two values or two strings.\n");
  gets(s1);
  gets(s2);

  if(isdigit(*s1)) {
    printf("Testing values for equality.\n");
    check(s1, s2, compvalues);
  }
  else {
    printf("Testing strings for equality.\n");
    check(s1, s2, strcmp);
  }

  return 0;
}

void check(char *a, char *b,
int (*cmp)(const char *, const char *))
{
  if(!(*cmp)(a, b)) printf("Equal");
  else printf("Not Equal");
}

int compvalues(const char *a, const char *b)
{
  if(atoi(a)==atoi(b)) return 0;
  else return 1;
}


代码 33
char *p;
p = malloc(1000); /* get 1000 bytes */

代码 34
int *p;
p = malloc(50*sizeof(int));

代码 35
p = malloc(100);
if(!p) {
  printf("Out of memory.\n");
  exit(1);
}

代码 36
/* Allocate space for a string dynamically, request user
   input, and then print the string backwards. */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
  char *s;
  register int t;

  s = malloc(80);

  if(!s) {
    printf("Memory request failed.\n");
    exit(1);
  }

  gets(s);
  for(t=strlen(s)-1; t>=0; t--) putchar(s[t]);
  free(s);

  return 0;
}

代码 37
#include <stdio.h>
#include <stdlib.h>

int pwr(int a, int b);

int main(void)
{
  /* Declare a pointer to an array that has 10
     ints in each row. */
  int (*p)[10]; 

  register int i, j;

  /* allocate memory to hold a 4 x 10 array */
  p = malloc(40*sizeof(int));

  if(!p) {
    printf("Memory request failed.\n");
    exit(1);
  }

  for(j=1; j<11; j++)
    for(i=1; i<5; i++) p[i-1][j-1] = pwr(j, i);

  for(j=1; j<11; j++) {
    for(i=1; i<5; i++) printf("%10d ", p[i-1][j-1]);
    printf("\n");
  }

  return 0;
}

/* Raise an integer to the specified power. */
pwr(int a, int b)
{
  register int  t=1;

  for(; b; b--) t = t*a;
  return t;
}

代码 38
p = (int (*)[10]) malloc(40*sizeof(int));

代码 39
/* This program is wrong. */
int main(void)
{
  int x, *p;

  x = 10;
  *p = x; /* error, p not initialized */

  return 0;
}

代码 40
/* This program is wrong. */
#include <stdio.h>

int main(void)
{
  int x, *p;

  x = 10;
  p = x;

  printf("%d", *p);

  return 0;
}

代码 41
p = x;

代码 42
p = &x;

代码 43
char s[80], y[80];
char *p1, *p2;

p1 = s;
p2 = y;
if(p1 < p2) . . .

代码 44
int first[10], second[10];
int *p, t;

p = first;
for(t=0; t<20; ++t)  *p++ = t;

代码 45
/* This program has a bug. */
#include <string.h>
#include <stdio.h>

int main(void)
{
  char *p1;
  char s[80];

  p1 = s;
  do {
    gets(s);  /* read a string */

    /* print the decimal equivalent of each
       character */
    while(*p1) printf(" %d", *p1++);

  } while(strcmp(s, "done"));

  return 0;
}

代码 46
/* This program is now correct. */
#include <string.h>
#include <stdio.h> 

int main(void)
{
  char *p1;
  char s[80];

  do {
    p1 = s; /* reset p1 to beginning of s */
    gets(s);  /* read a string */

    /* print the decimal equivalent of each
       character */
    while(*p1) printf(" %d", *p1++);

  } while(strcmp(s, "done"));

  return 0;
}

转载于:https://my.oschina.net/lazyou/blog/130048

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值