quiettttt 2022-08-24 08:45 采纳率: 97.9%
浏览 46
已结题

这条代码加static和不加static有什么区别啊?


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 256

int main(void)
{
    char **pt;
    int i, n, length;
    static char temp[LEN];

    printf("How many words do you wish to enter? ");
    scanf("%d", &n);
    if ((pt = (char **)malloc(n * sizeof(char *))) != NULL)
    {
        printf("Enter %d words now:\n", n);
        for (i = 0; i < n; i++)
        {
            scanf("%255s", temp);
            length = strlen(temp) + 1;
            pt[i] = (char *)malloc(length * sizeof(char));
            //↑使用malloc分配足够的存储空间来存储单词;
            if (NULL == pt[i])
            {
                printf("Memory allocation failed!\n");
                exit(EXIT_FAILURE);
            }
            strcpy(pt[i], temp);
            //↑从临时数组中把单词拷贝到动态分配的存储空间中;
        }
        printf("Here are your words:\n");
        for (i = 0; i < n; i++)
        {
            puts(pt[i]);
            free(pt[i]);
            pt[i] = NULL;
        }
        free(pt);
        pt = NULL;
        //↑指针仍然指向malloc分配的存储空间;
        //↑因此令指针指向NULL后防止内存滥用;
    }
    else
    {
        printf("Memory allocation failed!\n");
        exit(EXIT_FAILURE);
    }

    return 0;
}

这条代码加static和不加static有什么区别啊?我感觉也没什么区别啊

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2022-08-24 09:10
    关注

    参考这篇文章:https://wenku.baidu.com/view/b2182af2baf3f90f76c66137ee06eff9aef8496e.html
    上面的代码里,确实没什么区别,除了加static后初始化。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月1日
  • 已采纳回答 8月24日
  • 创建了问题 8月24日

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教