把握命运,追逐梦想

对自己所做的事要有兴趣,同时还要能够坚持不懈

统计

留言簿(1)

阅读排行榜

评论排行榜

#

memcmp的使用

#include<string.h>
#include
<stdio.h>

int main()
{
    
char a[9= "fadsgdga";
    
char b[13]= "fadsgdgdfsd";

    
int cmp = memcmp(a,b,8);
    
if(cmp == 0)
    
{
        printf(
"相等");
    }

    
else if( cmp == 1)
    
{
        printf(
"大于");
        
    }

    
else
    
{
        printf(
"小雨");
    }

    
return 0;
}

posted @ 2009-07-29 15:44 把握命运 阅读(182) | 评论 (0)编辑 收藏

读取mp3文件的尾部信息

#include<string.h>
#include
<stdio.h>
struct Infomation
{
    
char tag[4];
    
char name[31];
    
char singer[31];
    
char aname[31];
    
char year[5];
    
char content[29];
    
char other[4];
}
;

int main()
{
    FILE 
*pFile;
    pFile 
= fopen("11.mp3","r+");

    
char strbuf[129];

    fseek(pFile,
-128,2);
    
    
int ret = 0;

    ret 
= fread(strbuf,128,1,pFile);

    Infomation a;
    memset(
&a,0,sizeof(Infomation));

    rewind(pFile);

    memcpy(
&a.tag,strbuf,3);
    memcpy(
&a.name,&strbuf[3],30);

    memcpy(
&a.singer,&strbuf[33],30);
    memcpy(
&a.aname,&strbuf[63],30);
    memcpy(
&a.year,&strbuf[93],4);
    memcpy(
&a.content,&strbuf[97],28);
    memcpy(
&a.other,&strbuf[125],3);

    printf(
"%s\n",a.tag);
    printf(
"%s\n",a.name);
    printf(
"%s\n",a.singer);
    printf(
"%s\n",a.aname);
    printf(
"%s\n",a.year);
    printf(
"%s\n",a.content);
    printf(
"%s\n",a.other);

    fclose(pFile);
    
return 0;

}

//
//MP3歌曲的基本信息存在了MP3文件的最后128个字节里,
//其结构是:
//1-3 TAG 
//4-33 歌曲名(Take Me To Your Heart )
//34-63 歌手名(Michael Learns to Rock)
//64-93 专辑名(Take Me to Your Heart)
//94-97 年(2004)
//98-125 备注 (http://www.uptu.com
//126 保留位,这时为0,则说明有音轨,下一位就是音轨
//127 保留位,为音轨(第几首歌)(OC)
//128 保留位 (风格)(66)
//请写出一个读取MP3歌曲信息的函数,将信息存放在结构成员变量中。

posted @ 2009-07-29 15:20 把握命运 阅读(547) | 评论 (0)编辑 收藏

数组 指针 const

#include<stdio.h>
#include
<string.h>

int main()
{
    
//下面的写法是可以的
    char a[5= "abcd";    
    
const char b[10= "safajshjf";
    
const char *= "asdfdsgd";
    
char *const d = "safdsfgsdg";
    
const char* const e = "adsgfsdg";


    
//下面的c和上面的d,e不同,c不是const的,而是c指向的内存区是const的,所以下面的写法可以
    c = "fasdffds";
    
//下面的三行都不行,原因是数组名除了初始化时,再也不能作为左值了。而const类型的d和e也不能再作为左值。
    /*a="afds";
    b = "asfdsfdgg";
    d = "fasdgsdg";
    e = "fasdfgg";
*/



    

    
//下面的可以,因为第一参数要求是非const的,下面的第一参数都满足条件
    strcpy(a,"sdfs");
    strcpy(d,
"sfsdgd");
    
//下面的不通过,因为第一参数要求是非const的,因为const常量不能修改
    /*strcpy(b,"sfsdgf");
    strcpy(c,"sfsgddg");
    strcpy(e,"safsgdg");
*/


    

    
//下面的写法是可以的,因为上面的a数组并不是常量,而是把“abcd”赋值给了a所指向的内存区,所以a的地址可以赋值给别的指针
    char *f=a;
    f 
= d;
    
//下面的是不行的,因为b的数组内存是常量的,不可以进行修改,所以不能把b的地址赋值给非const指针
    /*f = b;
    f = c;
    f =e;
*/



    
//下面的五个都是可以的,因为const和非const的变量都可以赋值给const类型的变量
    const char *= a;
    g  
=b;
    g  
= c;
    g 
= d;
    g 
= e;

    
//下面尝试修改上面的五块内存区


    
return 0;
}


//总结:
//const指针的用法如上例所示,分别是限制指针和指针内存区域的,但是虽然有这些限制,也仅是针对当前变量的限制,如果原本的内存地址已知,后来赋值给了const*类型的
//指针,那么利用原来的内存地址依然是可以修改该内存区域的。
//
//数组名相当于*const类型的,因为数组名和*const都在以后都不能再给数组名或指针赋值,而只能修改数组的内存区或指针指向的内存区。
//const* typename const类型的是指针和内存区都不可以修改的。
//
//无论是整形数组还是字符数组,初始化的时候都是赋值初始化的,不要把字符数组的初始化理解成把一个常量字符串的指针赋值给了字符数组名。
    //下面的写法是可以的
    char a[5= "abcd";    
00401740  mov         eax,dword ptr [___xi_z+2Ch (4020E4h)] 
00401745  mov         dword ptr [ebp-0Ch],eax 
00401748  mov         cl,byte ptr [___xi_z+30h (4020E8h)] 
0040174E  mov         
byte ptr [ebp-8],cl 
    
const char b[8= "safader";
00401751  mov         edx,dword ptr [___xi_z+34h (4020ECh)] 
00401757  mov         dword ptr [ebp-1Ch],edx 
0040175A  mov         eax,dword ptr [___xi_z
+38h (4020F0h)] 
0040175F  mov         dword ptr [ebp
-18h],eax 
    
const char *= "asdfdsgd";
00401762  mov         dword ptr [ebp-24h],offset ___xi_z+3Ch (4020F4h) 
    
char *const d = "safdsfgsdg";
00401769  mov         dword ptr [ebp-20h],offset ___xi_z+48h (402100h) 
    
const char* const e = "adsgfsdg";
00401770  mov         dword ptr [ebp-28h],offset ___xi_z+54h (40210Ch) 
//可以看出来,这是上面的程序的前五行的代码的反汇编,前两行的是赋值,汇编代码可以看出来,先把静态区中的常量字符串赋值给了eax,然后后eax转给字符数组的内存区,但是后面的三个则是直接把静态区的常量字符串的地址赋值给了指针,如果只赋值给const*指针还好,但是连*const指针也是直接赋值,结果导致了后面的strcpy(d,"sfd")的静态区内存访问错误

posted @ 2009-07-29 10:19 把握命运 阅读(1231) | 评论 (0)编辑 收藏

判断闰年

#include<stdio.h>
int main()
{
    
    printf(
"请输入年份:");
    
int year = 0;
    scanf(
"%d",&year);
    
if(year%4 == 0 )
    
{
        
if(year%100 == 0 )
        
{
            
if(year%400 == 0)
            
{
                printf(
"%d是闰年",year);
            }

            
else
            
{
                printf(
"%d不是闰年",year);
            }

        }

        
else
        
{
            printf(
"%d是闰年",year);
        }

    }

    
else
    
{
        printf(
"%d不是闰年",year);
    }

    scanf(
"%d",&year);
    
return 0;
}

posted @ 2009-07-28 16:54 把握命运 阅读(113) | 评论 (0)编辑 收藏

malloc的错误和正确用法

#include<stdlib.h>
#include
<stdio.h>
int main()
{
    
char *p;
    p 
= (char*)malloc(10);
    p 
= "Hello";
    printf(
"%s\n",p);
    
return 0;
}


#include
<string.h>
#include
<stdio.h>
#include
<malloc.h>

int main()
{
    
char *str;
    str 
=(char*)malloc(10);
    strcpy(str,
"Hello");
    printf(
"String is %s",str);
    free(str);
    
return 0;
}

posted @ 2009-07-28 16:42 把握命运 阅读(220) | 评论 (0)编辑 收藏

qsort的用例

#include<stdio.h>
#include
<string.h>
#include
<stdlib.h>

int sort_func(const void*a,const void*b)
{
    
return (strcmp((const char*)a,(const char*)b));
}


int main()
{
    
char list[5][4= {"car","cat","cab","cap","can"};
    qsort((
void*)list,5,sizeof(list[0]),sort_func);
    
for(int i = 0; i <5++i)
    
{
        printf(
"%s",list[i]);
        printf(
"\n");
    }

    
return 0;
}

posted @ 2009-07-28 16:39 把握命运 阅读(177) | 评论 (0)编辑 收藏

上课的atof函数的使用例子

#include<stdio.h>
#include
<stdlib.h>

int main()
{
    
float f;
    
char *str = "34.31";
    f 
= atof(str);
    printf(
"%.2f",f);
    
return 0;

}

posted @ 2009-07-28 16:37 把握命运 阅读(353) | 评论 (0)编辑 收藏

随机数函数rand和种子函数srand的使用

#include<stdlib.h>
#include
<stdio.h>
#include
<time.h>

int main()
{
    
int i;
    time_t t;
    srand((unsigned)time(
&t));
    printf(
"ten random numbers from 0 to 99\n\n");
    
for(i = 0; i<10; i++)
    
{
        printf(
"%d\n",rand()%100);
    }

}

posted @ 2009-07-28 16:32 把握命运 阅读(423) | 评论 (1)编辑 收藏

刚刚上课两天,记录点小代码(求素数的)

     摘要: #include<stdio.h>#include<memory.h>#include<math.h>struct PrimeBlock{    PrimeBlock();    void addPrime(int );   ...  阅读全文

posted @ 2009-07-28 16:26 把握命运 阅读(310) | 评论 (0)编辑 收藏

仅列出标题
共5页: 1 2 3 4 5