栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数组运算-搜索

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数组运算-搜索

#include 

int amount[] = {1,5,10,25,50};
char *name[] = {"penny","nickel","dime","quarter","half-dollar"};

int search(int key,int a[],int len)
{
	int ret = -1;
	int i;
	for( i=0; i-1 )
	{
		printf("%sn",name[r]);
	}
    return 0;
}

上述代码(因为两种割裂的数组)对计算机cache不友好;

#include 

int amount[] = {1,5,10,25,50};
char *name[] = {"penny","nickel","dime","quarter","half-dollar"};

struct {
	int amount;
	char *name;
} coins[] = {
    {1,"penny"},
    {5,"nickel"},
    {10,"dime"},
    {25,"quarter"},
    {50,"half-dollar"},
};

int main()
{
	int k = 25;
	int i;
	for (i=0; i 

使用gcc编译代码是报出

error: 'for' loop initial declarations are only allowed in C99 mode

note: use option -std=c99 or -std=gnu99 to compile your code

这是因为在gcc中直接在for循环中初始化了增量:
    for(int i=0; i 这语法在gcc中是错误的,必须先先定义i变量:
int i;
for(i=0;i

这是因为gcc基于c89标准,换成C99标准就可以在for循环内定义i变量了:

gcc src.c -std=c99 -o src

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738931.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号