美文网首页操场上玩电脑系列博客
C语言万花03 - static variables, loca

C语言万花03 - static variables, loca

作者: 绿月 | 来源:发表于2019-08-03 13:21 被阅读0次

今天要玩耍的是static variable还有 local pointers
先来看一个例子
header1.h

void set_i(int i);

int get_i();
int print_mychar();
int set_mychar(char *a);
char *get_mychar();
char *get_thischar();
char *get_thatchar();

header1.c

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

int i = 3;
char my_char[4];

void set_i(int n) {
    i = n;
}

int get_i() {
    return i;
}

int set_mychar(char *a) {
    strcpy(my_char, a);
    return 0;
}

int print_mychar() {
    puts(my_char);
    return 0;
}

char *get_mychar() {
    return my_char;
}

char *get_thischar() {
    return "654";
}

char *get_thatchar() {
    static char b[3] = "321";
    return b;
}

test.c

#include <header1.h>
#include <header2.h>
#include <stdio.h>

int main(int argc, char **argv) {
    set_i(10);
    printf("i = %d\n", get_i());
    char a[4]="123";
    set_mychar(a);
    print_mychar();
    puts(get_mychar());
    puts(get_thischar());
    puts(get_thatchar());
    return 0;
}

Makefile

CLFAGS=-I. -c -g -Wall

.SUFFIXES: .c .o

.c.o:
    cc $(CLFAGS) -o $@ $<

OBJ=    test.o \
        header1.o

all: test

test: $(OBJ)
    cc -g $(OBJ) -o $@

clean:
    rm -f test $(OBJ)

make
./test

./test 
i = 10
123
123
654
321

这现在是跑出来了,但是如果改两个地方就编译不出来。
改过的header1.h

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

int i = 3;
char my_char[i];

void set_i(int n) {
    i = n;
}

int get_i() {
    return i;
}

int set_mychar(char *a) {
    strcpy(my_char, a);
    return 0;
}

int print_mychar() {
    puts(my_char);
    return 0;
}

char *get_mychar() {
    return my_char;
}

char *get_thischar() {
    return "654";
}

char *get_thatchar() {
    char b[3] = "321";
    return b;
}

char my_char[4] -> char my_char[i]
static char b[3] = "321"; -> char b[3] = "321";
make

make
cc -I. -c -g -Wall -o header1.o header1.c
header1.c:6:6: error: variably modified ‘my_char’ at file scope
 char my_char[i];
      ^~~~~~~
header1.c: In function ‘get_thatchar’:
header1.c:36:12: warning: function returns address of local variable [-Wreturn-local-addr]
     return b;
            ^
Makefile:6: recipe for target 'header1.o' failed
make: *** [header1.o] Error 1

local variable的pointer无法被return,但是static variable可以。在function以外initialize的variable应该都是static的。
还有就是static的string的大小必须是一个constant,variable不行。

相关文章

  • C语言万花03 - static variables, loca

    今天要玩耍的是static variable还有 local pointers先来看一个例子header1.h h...

  • c语言static

    C程序一直由下列部分组成:1)正文段——CPU执行的机器指令部分;一个程序只有一个副本;只读,防止程序由于意外事故...

  • C语言变量类型

    C语言变量类型 auto static external static external register aut...

  • BE —— C++

    Storage classes Variables with static storage (such as gl...

  • Static关键字的作用

    在C++、C、Java等高级语言中均有关键字static C语言中 static的含义不是存储方式,而是指对函数的...

  • 代写CS3012 Translate Assembly Lang

    将C语言的FIFO queue用ASM重构。Part A: Global Variables and Separa...

  • 代写CS3012 Translate Assembly Lang

    将C语言的FIFO queue用ASM重构。Part A: Global Variables and Separa...

  • static用法总结

    1 C语言中 在C语言中,static有三个明显作用: 函数体内static变量,不会随函数结束而消亡。再次调用该...

  • iOS面试题及答案

    C语言相关面试题 1.static有什么用途? 答案:在C语言中,static主要定义全局静态变量,定义局部静态变...

  • iOS面试题大全(附带答案)

    C语言相关面试题 1.static有什么用途? 答案:在C语言中,static主要定义全局静态变量,定义局部静态变...

网友评论

    本文标题:C语言万花03 - static variables, loca

    本文链接:https://www.haomeiwen.com/subject/enrjdctx.html