Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C在打印函数中的第一个值后,尝试打印F时出现分段错误_C_Function_Pointers - Fatal编程技术网

C在打印函数中的第一个值后,尝试打印F时出现分段错误

C在打印函数中的第一个值后,尝试打印F时出现分段错误,c,function,pointers,C,Function,Pointers,当我尝试在readWav()函数中的*缓冲区[I]上使用printf时,我遇到了分段错误。而且buffer能够正确地传递到main(),我能够用datas[I]检查main中的值。我只是简单地复制了main()中的printf,必须用*缓冲区[I]替换datas[I],然后它就可以编译了。它在readWav()中打印第一个值,然后出现SegFault 在readWav()-缓冲区[I])-换句话说,如果datas[I]引用main()中的实际值,那么readWav()中的缓冲区会有什么问题 更新

当我尝试在
readWav()
函数中的
*缓冲区[I]
上使用
printf
时,我遇到了分段错误。而且
buffer
能够正确地传递到
main()
,我能够用
datas[I]
检查
main
中的值。我只是简单地复制了
main()
中的
printf
,必须用
*缓冲区[I]
替换
datas[I]
,然后它就可以编译了。它在
readWav()
中打印第一个值,然后出现SegFault

readWav()
-缓冲区[I])-换句话说,如果
datas[I]
引用
main()
中的实际值,那么
readWav()
中的
缓冲区
会有什么问题

更新:
main()
data
传递给其他仍需添加的函数。这就是为什么我尝试在
readWav()
中打印它,认为它在值传递到其他函数的方式上非常相似-如果我错了,请纠正我

#include <stdio.h>
#include "sndfile.h"

int readWav(const char *const fname, long *numFrames, int *sRate, float **buffer);

int main(int argc, char *argv[])
{
    int   sRates, sRatem, ret;
    long  nSamples = 0, nSamplem;
    float *datas, *datam;

    printf("Read Test\n");
    if (argc != 3) {
        fprintf(stderr, "Expecting two wav file as argument\n");
        return 1;
    }

    ret = readWav(argv[1], &nSamples, &sRates, &datas);
    if (ret != 0) {
        printf("Error\n");
        return 1;
    }
    // Output Info
    printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n",
        nSamples, argv[1], sRates, (float)nSamples/sRates);

    for (i=0; i < nSamples ; i++) {
        printf("%d\t %f\n", i, datas[i]);
    }

    free(datas);

    return 0;
//Cleanup etc:
}

int readWav(const char *const fname, long *numFrames, int *sRate, float **buffer)
{
    // Open sound file
    SF_INFO sndInfo;

    if ((sRate == NULL) || (numFrames == NULL) || (buffer == NULL)) {
        fprintf(stderr, "Invalid arguments passed to readWav()\n");
        return 1;
    }

    SNDFILE *sndFile = sf_open(fname, SFM_READ, &sndInfo);
    if (sndFile == NULL) {
        fprintf(stderr, "Error reading source file '%s': %s\n", fname, sf_strerror(sndFile));
        return 1;
    }

    // Allocate memory
    *buffer = malloc(sndInfo.frames * sndInfo.channels * sizeof(float));
    if (*buffer == NULL) {
        fprintf(stderr, "Could not allocate memory for file\n");
        sf_close(sndFile);

        return 1;
    }

    *sRate = sndInfo.samplerate;
    // Load data
    *numFrames = sf_readf_float(sndFile, *buffer, sndInfo.frames);
    // Check correct number of samples loaded
    if (*numFrames != sndInfo.frames) {
        fprintf(stderr, "Did not read enough frames for source\n");
        sf_close(sndFile);
        free(*buffer);
    }
    else {
        printf("Successfully read file\n");
        *numFrames = sndInfo.frames;
    }
    // Output Info
    printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n",
        *numFrames, fname, *sRate, (float)*numFrames/sndInfo.samplerate);

    for (i=0; i < sndInfo.frames ; i++) {
        printf("%d\t %f\n", i, *buffer[i]);
    }

    sf_close(sndFile);
    return(0);
}
#包括
#包括“sndfile.h”
int readWav(常量字符*const fname,长*numFrames,int*sRate,浮点**缓冲区);
int main(int argc,char*argv[])
{
int sRates、sRatem、ret;
长nSamples=0,nSamplem;
浮点*数据,*数据M;
printf(“读取测试”);
如果(argc!=3){
fprintf(stderr,“需要两个wav文件作为参数\n”);
返回1;
}
ret=readWav(argv[1]、&nSamples、&srate和数据);
如果(ret!=0){
printf(“错误\n”);
返回1;
}
//输出信息
printf(“从%s读取%ld帧,采样率:%d,长度:%fs\n”,
nSamples,argv[1],srate,(float)nSamples/srate);
对于(i=0;i
在解除指针防护之前,您正在访问数组索引

您本想执行
(*buffer)[i]
,但实际上执行了
*(buffer[i])
(请注意,为了清晰起见,我添加了括号)


下次,请记住,
*
的优先级低于
[]

您必须首先解除引用:
(*buffer)[i]
,谢谢它能工作。如果我添加第三个函数,比如fthird(),那么我可以使用与readWav()相同的格式。我只是浏览了一下你的代码,但马上就知道了,因为我以前也遇到过类似的问题。别忘了你可以接受你认为最好的答案!令人敬畏的以赛亚-你对优先权的解释{*的优先权低于[]}