473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

pointers to structures

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
Jul 6 '06 #1
11 1853

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
Jul 6 '06 #2
R Dow wrote:
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?
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

Jul 6 '06 #3
Frederick Gotham posted:

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

#include <stdlib.h>

--

Frederick Gotham
Jul 6 '06 #4
Frederick Gotham wrote:

#include <stdlib.h>
blah(struct sfinfo *info) {

SOUNDFILE *outsfp;

outsfp = malloc(sizeof(*outsfp)); /* Remove ERRORs */
if (outsfp == NULL) {
handle_error();
return;
}
sfinfocopy(info, &(outsfp->info));

}
I fixed YOUR errors (inline and indented).

Tom

Jul 6 '06 #5
R Dow wrote:
>...
void sfinfocopy(struct sfinfo *from, struct sfinfo *to) {
to->srate = from->srate;
to->ssize = from->ssize;
to->chans = from->chans;
}
...
sfinfocopy(info, &(outsfp->info));
In addition to what others posted, why not just

info = outsfp->info;

instead of calling a function?
>outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE)))==NULL;
There is one right parenthesis too many. You should get a compile
error here.
Jul 6 '06 #6
Thanks for that. In fact, I tried to extract the example from the actual
code to make it easier to read, and thus introduced errors. So I
tried testing the extract and the extract works fine, so there is
obviously a far nastier error causing the problems.

Thanks again

Robert
Jul 6 '06 #7
R Dow wrote:
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;
A bit strange, since you use typedef for one struct and leave the
other as is: consistency is IMHO *a good thing* in the long run.

Also, all uppercase identifiers are generally used for macros, so
you might want to look at that again, as well.
and a function so:

void sfinfocopy(struct sfinfo *from, struct sfinfo *to) {
I would be picky to say this, but still, the first parameter should
ideally be const-qualified:
void sfinfocopy(const struct sfinfo *from, struct sfinfo *to) {
A design choice really, to indicate that this is read-only in
sfinfocopy.
to->srate = from->srate;
to->ssize = from->ssize;
to->chans = from->chans;

}
This is redundant. For such simple structures, assignment is good
enough.It wasn't there always, but has been for a while now, so you
can write:
struct sfinfo to, from;
/* fill out from */
...
to = from;
If I then do something like this:

blah(struct sfinfo *info) {
Needs a return type. The implicit int is not a good choice anymore.
Or, put in void, if you'd not want to return anything back to the
caller.
SOUNDFILE *outsfp;

outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE)))==NULL;
A few problems here. Let's take them apart one by one.
[1] #include <stdlib.his required to bring malloc() in scope
[2] the cast is unnecessary except for *very* few cases (your's doesn't
look like one till now, so ...)
[3] the comparison with NULL: comparison returns 1(if true) or 0
(if false), always. So, here, outsfp becomes either 0 or 1 and
[a] when it's zero you don't chek it and trying to access info
through outsfp is Undefined Behavior and will possibly
land you with a crash
[b] when it's one, you are trying to use 1, an integer as the
address of an object of type SOUNDFILE, which on most
systems will be an error. Integers and pointers are ,
interchangeable but the result is implementation defined
and it is very likely you can't have an object placed at
such an address, let alone use it.

So, ideally you should've written:[*]
outsfp = malloc(sizeof *outsfp);
and taken the trouble of testing it like this:
if ( outsfp == NULL ) {
die_horribly();
/* return error code, probably */
}
and gone on to use it when and where required.
You'd need to give outsfp some valid state first so that the copy
makes sense. So fill it in:
outsfp->pRawBuf = malloc( /*some memory, maybe?*/ );
outsfp->fp = someSndfp;
...
sfinfocopy(info, &(outsfp->info));
This is where nasal demons start flying out ;-) See above.
}

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

[*] A better approach would be to club the two in one statement
-- RAII (IMHO is a good thing), like:
SOUNDFILE *outsfp = malloc(sizeof *outsfp);
/* so do we have something here? */
if ( ... ) {
...
HTH
--
Suman

Jul 6 '06 #8
R Dow wrote:
Thanks for that. In fact, I tried to extract the example from the
actual code to make it easier to read, and thus introduced errors. So
I tried testing the extract and the extract works fine, so there is
obviously a far nastier error causing the problems.
You've ably demonstrated why we want to see complete, minimal programs
that reproduce the error.


Brian
Jul 6 '06 #9
You don't need a function to copy a struct, the regular assignment
operator works just fine:

to = from;

----

The problem is in your malloc:
outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE)))==NULL;
the ==NULL business sets outsfp to false or true, not a good pointer.

so try:

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

if( outsfp != NULL ) outsfp->info = incoming;

Jul 6 '06 #10
"Ancient_Hacker" <gr**@comcast.netwrites:
You don't need a function to copy a struct, the regular assignment
operator works just fine:

to = from;

----

The problem is in your malloc:
>outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE)))==NULL;

the ==NULL business sets outsfp to false or true, not a good pointer.

so try:

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

if( outsfp != NULL ) outsfp->info = incoming;
Much better:

outsfp = malloc(sizeof *outsfp);
if (outsfp == NULL) {
/* error handling */
}
....
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 6 '06 #11

Frederick Gotham wrote:
Frederick Gotham posted:
outsfp = (SOUNDFILE *)malloc(sizeof(SOUNDFILE));

#include <stdlib.h>
.... and free(outsfp); somewhere.

Jul 6 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Konstantin Shemyak | last post by:
I have a big structure tree. All leaves are scalar values (no pointers). Present are arrays, structures and unions. I want to be able to store/read the content of the structure in/from a file, and...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
7
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop...
6
by: Sam | last post by:
Hi everyone I'm confused by these declarations: struct hostent *he; server *s = (server*)malloc(sizeof(server)); /* in the code * he = gethostbyname(name);
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
7
by: copx | last post by:
How do you implement an event queue in C? The problem I had is that events needed pointers to the objects they affect and I do not know any way to check if pointers are actually valid in C. The...
12
by: addinall | last post by:
Hi guys and gals, Haven't been in here for a good decade or so! I'm having a braid-dead moment that is lasting for a couple of days. Declaring pointers to functions witrhin structures and...
10
by: arnuld | last post by:
thanks
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.