不将函数的返回值传递给变量,为什么?

时间:2013-06-14 17:47:21

标签: c

void reversefunction(const char * argv2,const char * argv3){

FILE *stream1=NULL;
FILE *stream2=NULL;

byteone table[HEADERLENGTH];
byteone numberofchannels;
byteone movebytes;

bytefour i;
bytefour sizeofdata;
bytefour var_towrite_infile;

stream1=fopen(argv2,"rb");
stream2=fopen(argv3,"wb+");

if(stream1==NULL){
    printf("\n.xX!- failed - to - open - file -!Xx.\n");
    exit(0);
}

if(stream2==NULL){
    printf("\n.xX!- failed - to - create - new - file -!Xx.\n");
    exit(0);
}

printf(".xX!- %s - opened - success -!Xx.\n",argv2);

fread(table,1,HEADERLENGTH,stream1);

//这里启动问题

numberofchannels=little_endian_to_bytefour((table+22),NUMCHANNELS);
sizeofdata=little_endian_to_bytefour((table+40),SUBCHUNK2SIZE);

//这里结束问题

fwrite(table,1,HEADERLENGTH,stream2);

movebytes=numberofchannels*2;

i=sizeofdata;
fseek(stream1,i,SEEK_SET);

while(i>=0){
    fread(&var_towrite_infile,4,movebytes,stream1);
    fwrite(&var_towrite_infile,4,movebytes,stream2);
    i=i-movebytes;
    fseek(stream1,i,SEEK_SET);
    printf("%d\n",i);
    printf("%d\n",sizeofdata);
    printf("%d\n",little_endian_to_bytefour((table+40),SUBCHUNK2SIZE));
    printf("-------------\n");
}

fclose(stream1);
fclose(stream2);
return;

}

所以,当我试图传递变量numberofchannels和sizeofdata函数little_endian_to_bytefour的返回值时,它不会传递任何内容。当我打印返回值时,它打印正确。那么为什么会这样呢?

//终端屏幕

.
.
.

0
0
113920
-------------
0
0
113920
-------------
0
0
113920
-------------

.
.
.

//屏幕终端

//其他信息

typedef unsigned char byteone;

typedef unsigned short int bytetwo;

typedef unsigned int bytefour;



bytefour little_endian_to_bytefour(byteone *table, byteone bit_length){

    bytefour number=0;

    if(bit_length==2){
        number=table[1];
        number<<=8;
        number|=table[0];
    }
    else{
        number=table[3];
        number<<=8;
        number|=table[2];
        number<<=8;
        number|=table[1];
        number<<=8;
        number|=table[0];
    }

    return number;

}

小例子/ *

int myfunction(int var1, int var2)
{

  int var3;

  var3=var1+var2

  return var3;

}

int main(void){

  int zaza1;

  zaza1=myfunction(2,3);

  printf("the number is %d",zaza1);

return;
}

// terminal

数字为0

//终端

* /

2 个答案:

答案 0 :(得分:0)

你可能会在你声明的var3之间感到困惑。返回myfunctionarg3返回值var1+var2

也就是说,你返回一个未初始化的变量var3,它就是0。

答案 1 :(得分:0)

typedef unsigned char byteone;
typedef unsigned int bytefour;

bytefour little_endian_to_bytefour(byteone *table, byteone bit_length);

byteone numberofchannels;
byteone movebytes;

numberofchannels = little_endian_to_bytefour((table+22),NUMCHANNELS);
sizeofdata = little_endian_to_bytefour((table+40),SUBCHUNK2SIZE);
movebytes = numberofchannels * 2;

您正在为unsigned int变量分配unsigned char返回值,因此部分值将被丢弃。恰好在这种情况下,剩下的部分为零。

目前还不清楚这里打算发生什么。您是否只是要声明变量的类型为bytefour

如果您已经编译并启用了警告,那么您应该已经注意到这个潜在的问题。

相关问题