zoukankan      html  css  js  c++  java
  • C语言指针一种容易错误使用的方法

    /*------------------------------------------------------------------------------
    HELLO.C

    Copyright 1995-1999 Keil Software, Inc.
    ------------------------------------------------------------------------------*/

    #include <REG52.H>                /* special function register declarations   */
                                      /* for the intended 8051 derivative         */

    #include <stdio.h>                /* prototype declarations for I/O functions */
    #include <stdlib.h>
    #include <string.h>
    #include <ABSACC.H>

    #define ERR_CASE 1 //test pointer

    #ifdef MONITOR51                         /* Debugging with Monitor-51 needs   */
    char code reserve [3] _at_ 0x23;         /* space for serial interrupt if     */
    #endif                                   /* Stop Exection with Serial Intr.   */
                                             /* is enabled                        */
    #if ERR_CASE
    void test_func(char *str)
    {
     char *p;
     printf("%s/n",str);
     p=malloc(100);
     memset (p, '/0', sizeof (p));
     strncpy(p,"test string",100);
     str=p;
    }
    #else
    void test_func(char **str)
    {
    #if 0
     char *p;
     p=malloc(100);
     memset (p, '/0', sizeof (p));
     strncpy(p,"test string",100);
     *str=p;
    #else
     *str=malloc(100);
     memset (*str, '/0', sizeof (*str));
     strncpy(*str,"test string",100);
    #endif
    }
    #endif
    /*---------------------------------------------
    The main C function.  Program execution starts
    here after stack initialization.
    ------------------------------------------------*/
    void main (void) {
     char *p_str="init string";
    /*------------------------------------------------
    Setup the serial port for 1200 baud at 16MHz.
    ------------------------------------------------*/
    #ifndef MONITOR51
        SCON  = 0x50;          /* SCON: mode 1, 8-bit UART, enable rcvr      */
        TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
        TH1   = 221;                /* TH1:  reload value for 1200 baud @ 16MHz   */
        TR1   = 1;                  /* TR1:  timer 1 run                          */
        TI    = 1;                  /* TI:   set TI to send first char of UART    */
    #endif

    /*------------------------------------------------
    Note that an embedded program never exits (because
    there is no operating system to return to).  It
    must loop and execute forever.
    ------------------------------------------------*/
      init_mempool (&XBYTE [0x2000], 0x1000);
    /* initialize memory pool at xdata 0x2000
       for 4096 bytes */
    #if ERR_CASE
     test_func(p_str);//把指向的内容传进去,只能读取传进的内容,不能改写;并不影响指针,退出时指针并不变化.
    #else
     test_func(&p_str);//把指针传进去,对指针进行操作
    #endif
     if(NULL!=p_str)
      printf ("%s/n",p_str);   /* Print "Hello World" */
      while (1) {
        P1 ^= 0x01;           /* Toggle P1.0 each time we print */

      }
    }

  • 相关阅读:
    算术运算符
    短路运算
    基本运算符
    类型转换
    数据类型讲解
    关键字
    河北省重大技术需求征集八稿第六天
    河北省重大技术需求征集八稿第五天
    河北省重大技术需求征集八稿第四天
    河北省重大技术需求征集八稿第三天
  • 原文地址:https://www.cnblogs.com/fedorayang/p/5094235.html
Copyright © 2011-2022 走看看