gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:50:40 25 4
gpt4 key购买 nike

void 反向函数( 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;
}

//终端

数字是0

//终端结束

*/

最佳答案

您可能会混淆在 myfunction 中声明并返回的 var3 和获取 var1 值的 arg3 +var2.

也就是说,您返回一个未初始化的变量 var3,以防万一 0。

关于c - 不将函数的返回值传递给变量,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17114386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com