一个有关计算结构体数组大小的问题
2011-12-13 10:40
211 查看
这个程序的做的事情,输入一个字符串,然后用折半查找法查找这个字符串是否在已存在的程序中。
主要目的是联系结构体数组;
#include <stdio.h>
#include <string.h>
struct key{
char *word;
int count;
}keytab[8]={
{"auto",0},
{"define",0},
{"main",0},
{"message", 0},
{"printf",0},
{"pthread",0},
{"sizeof",0}
};
int binsearch(char * g_char, struct key keytab[], int key_num)
{
int low = 0;
int high = key_num -1;
int mid ;
int out ;
while(high >= low)
{
mid = (low+high) / 2;
out = strcmp(g_char, keytab[mid].word) ;
if(out > 0)
{
low = mid+1;
}
else if(out < 0)
{
high = mid-1;
}
else
{
return 0;
}
}
return -1;
}
int main(void)
{
int err;
char get_char[10];
int key_num=0;
#if 0
while(strcmp(keytab[key_num].word,"")!= NULL)
key_num++;
#endif
key_num = (sizeof keytab/sizeof(struct key));
printf("word is %s\n",keytab[7].word);
printf("Please input you want to find word\n");
scanf("%s",get_char);
err = binsearch(get_char, keytab,key_num);
if(err == 0)
{
printf("Find the word\n");
}
else
printf("Sorry not find your word\n");
return 0;
}
因为假设不知道结构体数组,我想用下面的代码实现算出数组的大小,可是总是报段错误。后来
用gdb进行调试,才知道。这个函数本来有7个数组元素,本来没有keytab[7]这个元素的,系统不知道
数组元素已经完成他会一直进行下去。keytab[7]={0x00,0x00};keytab[8]={0x00,0x00};当key_num>6时,
就要进行keytab[7].word 与“”进行比较了。这就存在操作0x00这个地址了。所以报段错误了。
#if 0
while(strcmp(keytab[key_num].word,"")!= NULL)
key_num++;
#endif
可以进行的改进是while(keytab[key_num++].word != NULL)
来代替上面的代码,这样是一个解决问题的办法
但是C程序设计语言书中给出了更高明的算法:key_num = ( sizeof keytab/(sizeof key));
这是个有关字符串指针赋值的问题。呵呵……就到这里吧
主要目的是联系结构体数组;
#include <stdio.h>
#include <string.h>
struct key{
char *word;
int count;
}keytab[8]={
{"auto",0},
{"define",0},
{"main",0},
{"message", 0},
{"printf",0},
{"pthread",0},
{"sizeof",0}
};
int binsearch(char * g_char, struct key keytab[], int key_num)
{
int low = 0;
int high = key_num -1;
int mid ;
int out ;
while(high >= low)
{
mid = (low+high) / 2;
out = strcmp(g_char, keytab[mid].word) ;
if(out > 0)
{
low = mid+1;
}
else if(out < 0)
{
high = mid-1;
}
else
{
return 0;
}
}
return -1;
}
int main(void)
{
int err;
char get_char[10];
int key_num=0;
#if 0
while(strcmp(keytab[key_num].word,"")!= NULL)
key_num++;
#endif
key_num = (sizeof keytab/sizeof(struct key));
printf("word is %s\n",keytab[7].word);
printf("Please input you want to find word\n");
scanf("%s",get_char);
err = binsearch(get_char, keytab,key_num);
if(err == 0)
{
printf("Find the word\n");
}
else
printf("Sorry not find your word\n");
return 0;
}
因为假设不知道结构体数组,我想用下面的代码实现算出数组的大小,可是总是报段错误。后来
用gdb进行调试,才知道。这个函数本来有7个数组元素,本来没有keytab[7]这个元素的,系统不知道
数组元素已经完成他会一直进行下去。keytab[7]={0x00,0x00};keytab[8]={0x00,0x00};当key_num>6时,
就要进行keytab[7].word 与“”进行比较了。这就存在操作0x00这个地址了。所以报段错误了。
#if 0
while(strcmp(keytab[key_num].word,"")!= NULL)
key_num++;
#endif
可以进行的改进是while(keytab[key_num++].word != NULL)
来代替上面的代码,这样是一个解决问题的办法
但是C程序设计语言书中给出了更高明的算法:key_num = ( sizeof keytab/(sizeof key));
这是个有关字符串指针赋值的问题。呵呵……就到这里吧
相关文章推荐
- 一个有关数组的问题的解答
- 定义一个结构体变量(包括年、月、日),编写程序,要求输入年月日,程序能够计算并输出改日是本年中的第几天。注意闰年问题。(数组)
- 一个数组分配大小的问题
- net c#数组问题 声明一个100大小的数组 随机插入1-100之间的数,不能重复
- 作业三-应用分支与循环结构解决问题——.输入一个整数 x,计算并输出下列分段函数 sign(x) 的值
- 有关动态多维数组的一个问题
- 结构体对齐导致结构体数组大小变化引发的指针越界操作问题.
- 计算一个结构的大小
- vue计算属性时v-for处理数组时遇到的一个bug问题
- 关于结构指针类型数组做函数参数的一个问题#include<stdio.h>typede
- c++中利用sizeof运算符计算结构体大小问题探讨
- sizeof()计算一个类的对象大小和sizeof(string)的问题
- 7、定义一个文件输入流,调用read(byte[] b) 方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5,不考虑中文编码问题)。
- sizeof()计算一个类的对象大小和sizeof(string)的问题
- 问题:利用输入输出流重载实现一个整数数组的输入和输出。 要求:输入一个数组大小 及 所有元素的值,排序后输出该数组的所有元素。
- struct的成员对齐问题-结构体实际大小问题
- Linux下struct的成员对齐问题-结构体实际大小
- IDEA怎么打包可运行jar的一个有关问题
- 【DP】计算一个数组的最长递增子序列
- 一个有关perl hash结构的how to