C语言static 加在字符数组中得到正确的字符串,static不加则不行,由于不明白原理,题目描述不是很准确? C语言问题

c\u8bed\u8a00 \u6570\u7ec4\u7684\u5b9a\u4e49 \u52a0\u4e0astatic \u548c\u4e0d\u52a0 \u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f

c\u8bed\u8a00\u4e2dstatic\u5173\u952e\u5b57\u6709\u4e24\u4e2a\u4f5c\u7528\uff0c\u4e00\u662f\u6587\u4ef6\u4f5c\u7528\u57df\uff0c\u4e8c\u662f\u51fd\u6570\u4f5c\u7528\u57df\u3002

\u4e00\u3001\u6587\u4ef6\u4f5c\u7528\u57df

\u6587\u4ef6\u4f5c\u7528\u57df\u5173\u952e\u5b57static\u7684\u4f5c\u7528\u662f\uff0c\u4ee5static\u7533\u660e\u7684\u5168\u5c40\u53d8\u91cf\u3001\u51fd\u6570\u4e0d\u5f97\u88ab\u5176\u4ed6\u6587\u4ef6\u6240\u5f15\u7528\uff0c\u4f8b\u5982\uff1a

//\u8fd9\u662fmystr.c\u6587\u4ef6\u7684\u5185\u5bb9
#include

static int num = 10;

int mynum = 100;

static int str_len(char *str)
{
return strlen(str);
}

int mystr_len(char *str)
{
return str_len(str) + num;
}

\u5f53\u4f60mystr.c\u6587\u4ef6\u4e2d\u7684str_len\u51fd\u6570\u52a0\u4e0a\u4e86static\u5173\u952e\u5b57\uff0c\u4f60\u4fbf\u4e0d\u80fd\u5728\u5176\u4ed6\u7684\u5730\u65b9\u590d\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u8b6c\u5982\uff0c\u4f60\u4e0d\u80fd\u8fd9\u6837\u5199\uff1a

//\u8fd9\u662fmain.c \u7684\u5185\u5bb9

#include

int main()
{
int len;
len = str_len("hello, world\n");
printf("%d %d\n", num, len )
return 0;
}

gcc main.c mystr_len.c

\u5c06\u4e0d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff0c\u56e0\u4e3amystr.c\u4e2d\u7684num\u53d8\u91cf\u548cstr_len\u51fd\u6570\u90fd\u7528\u4e86static\u5173\u952e\u5b57\uff0c\u5bfc\u81f4\u4ed6\u4eec\u53ea\u80fd\u5728mystr.c\u4e2d\u88ab\u4f7f\u7528\uff0c\u5982\u6587\u4ef6\u4e2d\u7684mystr_len\u51fd\u6570\u53ef\u4ee5\u5f15\u7528num\u53d8\u91cf\u548cstr_len\u51fd\u6570\u3002


//\u8fd9\u662fmain.c \u7684\u5185\u5bb9

#include

int main()
{
int len;
len = mystr_len("hello, world\n");
printf("%d %d\n", my_num, len )
return 0;
}

\u5f53\u4e0d\u7528static\u5173\u952e\u5b57\u65f6\uff0c\u7b49\u540c\u4e8eextern\uff0c\u5373
int mystr_len(char *str)
{
return str_len(str) + num;
}

\u548c

extern int mystr_len(char *str)
{
return str_len(str) + num;
}

\u662f\u4e00\u6837\u7684\u3002


\u4e8c\u3001\u51fd\u6570\u4f5c\u7528\u57df

static\u53e6\u5916\u4e00\u4e2a\u7528\u9014\u662f\u51fd\u6570\u5185\u90e8\u9759\u6001\u53d8\u91cf\uff0c\u6700\u5e38\u7528\u7684\u60c5\u51b5\u662f

int *test()
{
int num = 100;
int *ptr = #
return ptr;
}

int main()
{
printf("%d\n", *test);
return 0;
}

\u8be5\u51fd\u6570\u8fd4\u56de\u6574\u6570num\u7684\u6307\u9488\uff0c\u5728main\u51fd\u6570\u4e2d\u6253\u5370*test\u5c06\u4f1a\u51fa\u73b0\u6bb5\u9519\u8bef\uff0c\u56e0\u4e3anum\u505a\u4e3atest\u51fd\u6570\u5185\u90e8\uff0c\u53ea\u80fd\u5728test\u5185\u90e8\u88ab\u8bbf\u95ee\u3002\u4ee5\u4e0b\u7a0b\u5e8f\u662f\u6b63\u786e\u7684\u3002

int *test()
{
static int num = 100;
int *ptr = #
return ptr;
}

int main()
{
printf("%d\n", *test);
return 0;
}

\u8be5\u7a0b\u5e8f\u4e2dnum\u53d8\u91cf\u52a0\u4e86\u5173\u952e\u5b57static\uff0c\u51fd\u6570\u8fd0\u884c\u7ed3\u675f\u540e\uff0c\u4f9d\u7136\u53ef\u4ee5\u5728\u5176\u4ed6\u5730\u65b9\u88ab\u5f15\u7528\uff0c\u53ea\u662f\u4e0d\u80fd\u76f4\u63a5\u901a\u8fc7\u53d8\u91cf\u540d\u8bbf\u95ee\uff0c\u800c\u8981\u95f4\u63a5\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\uff0c\u539f\u56e0\u662f static\u53d8\u91cf\u5b58\u50a8\u5728\u5168\u5c40\u6570\u636e\u6bb5\u4e2d\u800c\u4e0d\u662f\u51fd\u6570\u6808\u4e2d\u3002\u8bfb\u8005\u53ef\u4ee5\u5c06\u5b83\u770b\u4f5c\u7279\u6b8a\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u53ea\u662f\u5176\u4ed6\u5730\u65b9\u53ea\u80fd\u901a\u8fc7\u6307\u9488\u6765\u8bbf\u95ee\uff0c\u800c\u4e0d\u80fd\u76f4\u63a5\u901a\u8fc7\u53d8\u91cf\u540d\u8bbf\u95ee\u3002

static\u662f\u4e00\u4e2a\u9759\u6001\u53d8\u91cf.
\u5982\u679c\u52a0\u4e86static\uff0c\u5c31\u4f1a\u5bf9\u5176\u5b83\u6e90\u6587\u4ef6\u9690\u85cf\u3002\u4f8b\u5982\u5728a\u548cmsg\u7684\u5b9a\u4e49\u524d\u52a0\u4e0astatic\uff0cmain.c\u5c31\u770b\u4e0d\u5230\u5b83\u4eec\u4e86\u3002\u5229\u7528\u8fd9\u4e00\u7279\u6027\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u5b9a\u4e49\u540c\u540d\u51fd\u6570\u548c\u540c\u540d\u53d8\u91cf\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u547d\u540d\u51b2\u7a81\u3002Static\u53ef\u4ee5\u7528\u4f5c\u51fd\u6570\u548c\u53d8\u91cf\u7684\u524d\u7f00\uff0c\u5bf9\u4e8e\u51fd\u6570\u6765\u8bb2\uff0cstatic\u7684\u4f5c\u7528\u4ec5\u9650\u4e8e\u9690\u85cf\uff0c\u800c\u5bf9\u4e8e\u53d8\u91cf\uff0cst\u5b58\u50a8\u5728\u9759\u6001\u6570\u636e\u533a\u7684\u53d8\u91cf\u4f1a\u5728\u7a0b\u5e8f\u521a\u5f00\u59cb\u8fd0\u884c\u65f6\u5c31\u5b8c\u6210\u521d\u59cb\u5316\uff0c\u4e5f\u662f\u552f\u4e00\u7684\u4e00\u6b21\u521d\u59cb\u5316\u3002\u5171\u6709\u4e24\u79cd\u53d8\u91cf\u5b58\u50a8\u5728\u9759\u6001\u5b58\u50a8\u533a\uff1a\u5168\u5c40\u53d8\u91cf\u548cstatic\u53d8\u91cf\uff0catic\u8fd8\u6709\u4e0b\u9762\u4e24\u4e2a\u4f5c\u7528\u3002
\uff082\uff09static\u7684\u7b2c\u4e8c\u4e2a\u4f5c\u7528\u662f\u4fdd\u6301\u53d8\u91cf\u5185\u5bb9\u7684\u6301\u4e45\u3002\u53ea\u4e0d\u8fc7\u548c\u5168\u5c40\u53d8\u91cf\u6bd4\u8d77\u6765\uff0cstatic\u53ef\u4ee5\u63a7\u5236\u53d8\u91cf\u7684\u53ef\u89c1\u8303\u56f4\uff0c\u8bf4\u5230\u5e95static\u8fd8\u662f\u7528\u6765\u9690\u85cf\u7684\u3002
\uff083\uff09static\u7684\u7b2c\u4e09\u4e2a\u4f5c\u7528\u662f\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a0\u3002\u5176\u5b9e\u5168\u5c40\u53d8\u91cf\u4e5f\u5177\u5907\u8fd9\u4e00\u5c5e\u6027\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u4e5f\u5b58\u50a8\u5728\u9759\u6001\u6570\u636e\u533a\u3002\u5728\u9759\u6001\u6570\u636e\u533a\uff0c\u5185\u5b58\u4e2d\u6240\u6709\u7684\u5b57\u8282\u9ed8\u8ba4\u503c\u90fd\u662f0x00\uff0c\u67d0\u4e9b\u65f6\u5019\u8fd9\u4e00\u7279\u70b9\u53ef\u4ee5\u51cf\u5c11\u7a0b\u5e8f\u5458\u7684\u5de5\u4f5c\u91cf\u3002
\u6700\u540e\u5bf9static\u7684\u4e09\u6761\u4f5c\u7528\u505a\u4e00\u53e5\u8bdd\u603b\u7ed3\u3002\u9996\u5148static\u7684\u6700\u4e3b\u8981\u529f\u80fd\u662f\u9690\u85cf\uff0c\u5176\u6b21\u56e0\u4e3astatic\u53d8\u91cf\u5b58\u653e\u5728\u9759\u6001\u5b58\u50a8\u533a\uff0c\u6240\u4ee5\u5b83\u5177\u5907\u6301\u4e45\u6027\u548c\u9ed8\u8ba4\u503c0

因为非静态局部变量是分配在栈中的,当函数调用返回后,这种局部变量的内存空间就释放了,所以变量的值就变成不存在了或无效了。

而静态局部变量是分配在数据储存区的,其内存在函数调用返回后并不会释放,所以函数调用返回后依然可以访问到这种变量的值。这种变量的内存要到程序退出时才释放。

返回的地址必须再函数结束后还有效,而char s[] = "abcdefghij";在函数结束后,对象就不存在了。实际上你用static也不对,函数返回指针内容是不推荐的,一般建议把内存用malloc分配好通过参数传进去,如
char * p = (char*) malloc(100);
fun(p);

void func(char * p) {
strcpy(p,"abcde");
}

看你的代码很规范,不像初学的样子,自己找找函数里的static的用法不就好了

静态存储的变量的生存期是直到程序结束的

没有static修饰的局部变量,生存期在函数退出后就结束了



扩展阅读:public static void main ... c语言status怎么用 ... c语言static int ... c语言extern的用法 ... c语言static的用法 ... static-x ... c语言static经典例子 ... c语言 static关键字 ... static dword c语言 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网