C语言extern字符数组,无法在循环中输出?该怎么解决
C语言extern字符数组,无法在循环中输出??
首先在头声明字符数组引用
在main方法中调用用printf输出有数据,用txtBuf[0],txtBuf[1],txtBuf[2]....这种方式也能输出
放入while中后编译时就出错了
该怎么解决,在线等!!!
------解决思路----------------------
你这个变量的定义写在了函数定义的后面了
------解决思路----------------------
头文件重复包含了。
------解决思路----------------------
这个int mian(void)不是主函数吧。我刚用主函数测试了下,没任何问题,不然你自己用一个简单的测试程序试试,肯定是你复杂项目里有啥顺序问题错了。
------解决思路----------------------
输出
Hello Worlds!
Hello Worlds!
------解决思路----------------------
=== Build: Debug in extern_dcl (compiler: GNU GCC Compiler) ===
------解决思路----------------------
obj\Debug\main.o
------解决思路----------------------
In function `main':
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
obj\Debug\main.o:E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
8
------解决思路----------------------
more undefined references to `txtBuf' follow
------解决思路----------------------
------解决思路----------------------
=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===
------解决思路----------------------
extern char txtBuf[256];///仅仅是外部引用声明,只是多了一个数组大小信息。
通常写作
extern char txtBuf[];
首先在头声明字符数组引用
extern char txtBuf[256];
在main方法中调用用printf输出有数据,用txtBuf[0],txtBuf[1],txtBuf[2]....这种方式也能输出
放入while中后编译时就出错了
int i=0;
while(i<5)
{
printf("==%c\n",txtBuf[i]);
i++;
}
该怎么解决,在线等!!!
------解决思路----------------------
你这个变量的定义写在了函数定义的后面了
------解决思路----------------------
头文件重复包含了。
------解决思路----------------------
这个int mian(void)不是主函数吧。我刚用主函数测试了下,没任何问题,不然你自己用一个简单的测试程序试试,肯定是你复杂项目里有啥顺序问题错了。
------解决思路----------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern char txtBuf[256];
int main(){
strcpy(txtBuf,"Hello Worlds!\n");
printf("%s\n",txtBuf);
int i=0;
while(txtBuf[i]){
printf("%c",txtBuf[i++]);
}
printf("\n");
return 0;
}
char txtBuf[256];
输出
Hello Worlds!
Hello Worlds!
#include <stdlib.h>
#include <string.h>
extern char txtBuf[256];
int main(){
strcpy(txtBuf,"Hello Worlds!\n");
printf("%s\n",txtBuf);
int i=0;
while(txtBuf[i]){
printf("%c",txtBuf[i++]);
}
printf("\n");
return 0;
}
//注释掉这一行,则编译通不过
//char txtBuf[256];
------解决思路----------------------
=== Build: Debug in extern_dcl (compiler: GNU GCC Compiler) ===
------解决思路----------------------
obj\Debug\main.o
------解决思路----------------------
In function `main':
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
7
------解决思路----------------------
undefined reference to `txtBuf'
------解决思路----------------------
obj\Debug\main.o:E:\Devlops\CB\extern_dcl\main.c
------解决思路----------------------
8
------解决思路----------------------
more undefined references to `txtBuf' follow
------解决思路----------------------
------解决思路----------------------
=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===
------解决思路----------------------
extern char txtBuf[256];///仅仅是外部引用声明,只是多了一个数组大小信息。
通常写作
extern char txtBuf[];