c语言的字符串数组问题

was595 2015-02-07 05:08:24
搞了很久JAVA,发现已经把C语言忘光了,卡在一个问题上

#include <stdio.h>
int main(int argc, char *argv[]) {
static const char *classPathName = "com/android/browser/BrowserUtil";
static const char *hotkeyclassName = "com/iiu/setting/hotkey/MouseUtil";
static const char *className[2] = {classPathName, hotkeyclassName};

int i = 0;
for(i=0; i < 2; i++)
{
printf("%s\n", className[i]);
}
return 0;
}

上面代码编译出错了,我想在className数组里添加字符串指针,要怎么改才对?
...全文
110 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
was595 2015-02-08
  • 打赏
  • 举报
回复
引用 5 楼 czarten 的回复:
static 修饰符使得变量被放在全局数据区,在编译时就初始化,所以必须赋常量,而classPathName和hotkeyclassName都是变量,编译时不知道其值。 改成这样,就是在运行时赋值了,就没有问题:

#include <stdio.h>
int main(int argc, char *argv[]) {
    static const char *classPathName = "com/android/browser/BrowserUtil";
    static const char *hotkeyclassName = "com/iiu/setting/hotkey/MouseUtil";
    static const char *className[2];
    className[0]=classPathName;
    className[1]=hotkeyclassName;
    int i = 0;
    for(i=0; i < 2; i++)
    {
        printf("%s\n", className[i]);
    }
    return 0;
}
而且我觉得,你这个不管是static还是const都加得有点莫名,看不懂加在这里意义是什么。 static修饰局部变量,使其在函数退出时不销毁,main一般不用多次调用吧 const如果你是想让classPathName不能被再次赋值,应该改成 static char * const classPathName
这个嘛,我只是把某个项目里要修改的代码段抠出来单独编译测试,谢谢你的解释
你怎么了熊吉 2015-02-08
  • 打赏
  • 举报
回复
static 修饰符使得变量被放在全局数据区,在编译时就初始化,所以必须赋常量,而classPathName和hotkeyclassName都是变量,编译时不知道其值。 改成这样,就是在运行时赋值了,就没有问题:

#include <stdio.h>
int main(int argc, char *argv[]) {
    static const char *classPathName = "com/android/browser/BrowserUtil";
    static const char *hotkeyclassName = "com/iiu/setting/hotkey/MouseUtil";
    static const char *className[2];
    className[0]=classPathName;
    className[1]=hotkeyclassName;
    int i = 0;
    for(i=0; i < 2; i++)
    {
        printf("%s\n", className[i]);
    }
    return 0;
}
而且我觉得,你这个不管是static还是const都加得有点莫名,看不懂加在这里意义是什么。 static修饰局部变量,使其在函数退出时不销毁,main一般不用多次调用吧 const如果你是想让classPathName不能被再次赋值,应该改成 static char * const classPathName
bear234 2015-02-07
  • 打赏
  • 举报
回复
引用 3 楼 was595 的回复:
[quote=引用 1 楼 cutmelon 的回复:] 代码没问题啊,难道你用的vc6?不行就这么写
static const char *className[2]={"com/android/browser/BrowserUtil","com/iiu/setting/hotkey/MouseUtil"};
或者下面才是你想要的效果
	struct ClassName
	{
		const char *classPathName;
		const char *hotkeyclassName;
	};
	ClassName cn{"com/android/browser/BrowserUtil","com/iiu/setting/hotkey/MouseUtil"};
去掉className的static就好了。。。完蛋全忘了[/quote] 你用const修饰这个指针数组 难道意思不是指针指向的内容不能改变?这和你去掉static有什么关系?
was595 2015-02-07
  • 打赏
  • 举报
回复
引用 1 楼 cutmelon 的回复:
代码没问题啊,难道你用的vc6?不行就这么写
static const char *className[2]={"com/android/browser/BrowserUtil","com/iiu/setting/hotkey/MouseUtil"};
或者下面才是你想要的效果
	struct ClassName
	{
		const char *classPathName;
		const char *hotkeyclassName;
	};
	ClassName cn{"com/android/browser/BrowserUtil","com/iiu/setting/hotkey/MouseUtil"};
去掉className的static就好了。。。完蛋全忘了
was595 2015-02-07
  • 打赏
  • 举报
回复
引用 1 楼 cutmelon 的回复:
代码没问题啊,难道你用的vc6?不行就这么写
static const char *className[2]={"com/android/browser/BrowserUtil","com/iiu/setting/hotkey/MouseUtil"};
或者下面才是你想要的效果
	struct ClassName
	{
		const char *classPathName;
		const char *hotkeyclassName;
	};
	ClassName cn{"com/android/browser/BrowserUtil","com/iiu/setting/hotkey/MouseUtil"};
你说的这个方法,都成功,只是不明白我的写法里,用gcc编译也出错
cutmelon 2015-02-07
  • 打赏
  • 举报
回复
代码没问题啊,难道你用的vc6?不行就这么写
static const char *className[2]={"com/android/browser/BrowserUtil","com/iiu/setting/hotkey/MouseUtil"};
或者下面才是你想要的效果
	struct ClassName
	{
		const char *classPathName;
		const char *hotkeyclassName;
	};
	ClassName cn{"com/android/browser/BrowserUtil","com/iiu/setting/hotkey/MouseUtil"};

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧