A problem about printf

来源:互联网 发布:淘宝店铺没有流量 编辑:程序博客网 时间:2024/04/26 18:56

Today I try to get  the store mode of 'char' type.I write the code:

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

int main(int argc, char *argv[])
{
    FILE *fp=fopen("data","wb+");
    int a[]={'a','A'};
    char b[3];
    if(fp==NULL){
        printf("%s","error!");
        exit(1);
    }   
    fwrite(a,sizeof(int),2,fp);
    rewind(fp);
    fread(b,sizeof(char),2,fp);
    fclose(fp);
    printf("a[0]=%d,a[1]=%d,a=%s/n",&a[0],&a[1],a);
    printf("b[0]=%d,b[1]=%d,b=%s/n",&b[0],&b[1],b); 
  system("PAUSE"); 
  return 0;
}

I got the result:

a[0]=2293608.a[1]=2293612,a=a

b[0]=2293592,b[1]=2293593,b=a

While the file "data" is:

a[sapce][space][sapce][space]A

Why?

Then I read the code and find :

printf("a[0]=%d,a[1]=%d,a=%s/n",a[0],a[1],a);
    printf("b[0]=%d,b[1]=%d,b=%s/n",&b[0],&b[1],b); 

it not work,because the &a[0] return the address of a[0] not value of a[0].

Shame!

 

原创粉丝点击