指向结构的指针 [英] pointers to structures

查看:87
本文介绍了指向结构的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有点困惑,没有编程一段时间。任何帮助

都会感激不尽。


如果我定义了几个结构:


struct sfinfo {


双倍srate;

短ssize;

短期支票;

};


typedef struct {

void * pRawBuf;

FILE * fp;

struct sfinfo info;

} SOUNDFILE;


和函数如此:


void sfinfocopy(struct sfinfo * from,struct sfinfo * to){


to-> srate = from-> srate;

to-> ssize = from-> ssize;

to-> chans = from-> chans;


}


如果我这样做:


blah(struct sfinfo * info){


SOUNDFILE * outsfp;


outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE)))== NULL;


sfinfocopy(info,&(outsfp-> info));

}


它没有'似乎工作。表达式&(outsfp-> info)是不对的,是吗?

Robert

I am a bit confused here, having not programmed for some time. Any help
would be gratefully received.

If I define a couple of structures thus:

struct sfinfo {

double srate;
short ssize;
short chans;
};

typedef struct {
void *pRawBuf;
FILE *fp;
struct sfinfo info;
} SOUNDFILE;

and a function so:

void sfinfocopy(struct sfinfo *from, struct sfinfo *to) {

to->srate = from->srate;
to->ssize = from->ssize;
to->chans = from->chans;

}

If I then do something like this:

blah(struct sfinfo *info) {

SOUNDFILE *outsfp;

outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE)))==NULL;

sfinfocopy(info, &(outsfp->info));

}

It doesn''t seem to work. The expression &(outsfp->info) isn''t right, is it?
Robert

推荐答案



以下编译对我来说很好:


#include< stdio.h>


struct sfinfo {


双倍srate;

短ssize;

短期chans;


};


typedef struct {


void * pRawBuf;

FILE * fp;

struct sfinfo info;


} SOUNDFILE;

void sfinfocopy(struct sfinfo * from,struct sfinfo * to){


to-> srate = from-> srate;

to-> ssize = from-> ssize;

to-> chans = from-> chans;


}

blah(struct sfinfo * info){


SOUNDFILE * outsfp;


outsfp =(SOUNDFILE *)malloc(sizeof(SOUNDFILE)); / *删除错误* /


sfinfocopy(info,&(outsfp-> info));


}

int main(无效)

{

struct sfinfo obj = {0};


blah(& obj);

}


-


Frederick Gotham

The following compiles just fine for me:

#include <stdio.h>

struct sfinfo {

double srate;
short ssize;
short chans;

};

typedef struct {

void *pRawBuf;
FILE *fp;
struct sfinfo info;

} SOUNDFILE;
void sfinfocopy(struct sfinfo *from, struct sfinfo *to) {

to->srate = from->srate;
to->ssize = from->ssize;
to->chans = from->chans;

}
blah(struct sfinfo *info) {

SOUNDFILE *outsfp;

outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE)); /* Remove ERROR */

sfinfocopy(info, &(outsfp->info));

}
int main(void)
{
struct sfinfo obj = {0};

blah(&obj);
}

--

Frederick Gotham


R Dow写道:
R Dow wrote:

blah(struct sfinfo * info){


SOUNDFILE * outsfp;


outsfp =(SOUNDFILE *)malloc(sizeof(SOUNDFILE)))== NULL;


sfinfocopy(info,&(outsfp- > info));


}


它似乎无法奏效。表达式&(outsfp-> info)不对,是吗?
blah(struct sfinfo *info) {

SOUNDFILE *outsfp;

outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE)))==NULL;

sfinfocopy(info, &(outsfp->info));

}

It doesn''t seem to work. The expression &(outsfp->info) isn''t right, is it?



不,没关系。这是你的malloc系列。


1.不要回复[提示:包括stdlib.h]

2.使用sizeof(* outfp)作为里面的大小

3.什么是== NULL应该做什么?

4.在malloc之后添加一个if语句来检查NULL


Tom

No it''s fine. It''s your malloc line.

1. Don''t cast return [hint: include stdlib.h]
2. use sizeof( *outsfp ) as the size inside
3. What is ==NULL supposed to do?
4. Add an if statement after the malloc to check for NULL

Tom


Frederick Gotham发布:

Frederick Gotham posted:


outsfp =(SOUNDFILE *)malloc(sizeof(SOUNDFILE));
outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE));



#include< stdlib.h>


-


Frederick Gotham


#include <stdlib.h>

--

Frederick Gotham


这篇关于指向结构的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆