Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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
struct/constructor/pointer-C编程语言_C_Pointers_Struct - Fatal编程技术网

struct/constructor/pointer-C编程语言

struct/constructor/pointer-C编程语言,c,pointers,struct,C,Pointers,Struct,我有一个关于创建封装输入缓冲区的数据结构的问题。我目前的问题是malloc,但我觉得可能是某个指针定义了我的错误 我必须创建一个构造函数,它接受一个10x10的8位二维数组,并创建一个副本进行处理 它必须使用以下函数声明和结构定义 file - SP.h typedef struct SP_struct { uint8_t * base_of_buffer; uint16_t width; uint16_t height; uint16_t

我有一个关于创建封装输入缓冲区的数据结构的问题。我目前的问题是malloc,但我觉得可能是某个指针定义了我的错误

我必须创建一个构造函数,它接受一个10x10的8位二维数组,并创建一个副本进行处理

它必须使用以下函数声明和结构定义

file - SP.h
typedef struct SP_struct    {
    uint8_t *   base_of_buffer;
    uint16_t    width;
    uint16_t    height;
    uint16_t    x0;
    uint16_t    y0;
    uint8_t     pixel_size;
} SP_struct;

SP_struct*             SP_create           (const unsigned char* s, int width, int height); // constructor

#包括“pixel_sum.h”
#包括
#包括
#包括
int main()
{
uint16_t i=0;
无符号字符伪数组[4][4]={0,1,0,1,
1,2,1,0,
0,1,4,1,
1,0,1,0};
printf(“\n主循环\n”);
printf(“*伪数组=%d\n”,*伪数组);
结构SP_struct*SP_struct_ptr=SP_create(*虚拟数组,4,4);
struct SP_struct*SP_struct_ptr2=SP_create(*dummy_array,4,4);//如果这一行没有注释,它可以正常工作
printf(“完成的主回路”);
}
SP_struct*SP_create(常量无符号字符*s、整型宽度、整型高度){
uint16_t i=0;
printf(“------------SP_create\n”);
printf(“*s=%d\n”,*s);//这将获取值
printf(“s=%d\n”,s);//这将获取值
//printf(“%d\n”,宽度);
//printf(“%d\n”,高度);
SP_struct*p=malloc(宽度*高度*大小(p->pixel_大小));;
printf(“p=%d\n”,p);//这将获取值
printf(“&p=%d\n”,&p);//这将获取值
p->x0=12;
p->y0=12;
//p
printf(“p->base\u of_buffer=%d\n”,p->base\u of_buffer);
printf(“&p->x0=%d\n”,&p->x0);
printf(“p->x0=%d\n”,p->x0);
printf(“p->y0=%d\n”,p->y0);
对于(i=0;i缓冲区[0+i]的基\u=s[i];
//printf(“%d.”,i);
printf(“%d\n”,p->u缓冲区[0+i]的基u);
}
返回p;
}
当我用SP_struct_ptr2的行运行它时,它运行良好,并给我一个指针和正确的地址

但当我在SP_struct_ptr2行中回复评论时,我得到


有什么想法吗?我在windows 64位上使用gcc 6.3.0,这些是编译中唯一的文件。

您需要分别分配
SP\u struct
base\u缓冲区
。您的代码可能应该是这样的:

SP_struct*     SP_create(const unsigned char* s, int width, int height){
    ...
    SP_struct *p = malloc(sizeof(*p));
    if (p) {
        p->pixel_size = PIXEL_SIZE;
        p->base_of_buffer = malloc(width*height*sizeof(p->pixel_size)*sizeof(*p->base_of_buffer));
        ...
    }
    return p;
}

您需要分别分配缓冲区的
SP_struct
base_
。缓冲区的
基\u
未指向有效的内容。
SP_struct*     SP_create(const unsigned char* s, int width, int height){
    ...
    SP_struct *p = malloc(sizeof(*p));
    if (p) {
        p->pixel_size = PIXEL_SIZE;
        p->base_of_buffer = malloc(width*height*sizeof(p->pixel_size)*sizeof(*p->base_of_buffer));
        ...
    }
    return p;
}