Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   struct and pointer question (http://www.velocityreviews.com/forums/t952196-struct-and-pointer-question.html)

Bill Cunningham 09-14-2012 02:26 AM

struct and pointer question
 
In fread/fwrite the first parameter takes a pointer to a void. The
generic pointer. I wrote a struct that would insert the in file name and out
file name through fopen. My struct looks like this.

struct param {
char *infile;
char *outfile;
void *buf;
};
In the function I wrote that takes struct param as a type I don't know how
to chose a type in the struct for void *. I have:

fread(p.buf,1,sizeof p.buf,p.infile);

Is there no way to change a return type as declared in a struct's elements ?
The elements are all of type struct param now.
Should I just leave the void * variable out of the struct and declare it
as a type char * or int * or size_t * whatever I want later as I need it?

Is this clear?

Bill



Ben Bacarisse 09-14-2012 02:56 AM

Re: struct and pointer question
 
"Bill Cunningham" <nospam@nspam.invalid> writes:

> In fread/fwrite the first parameter takes a pointer to a void. The
> generic pointer. I wrote a struct that would insert the in file name and out
> file name through fopen. My struct looks like this.
>
> struct param {
> char *infile;
> char *outfile;
> void *buf;
> };
> In the function I wrote that takes struct param as a type I don't know how
> to chose a type in the struct for void *. I have:
>
> fread(p.buf,1,sizeof p.buf,p.infile);


You probably want buf to be array, not a pointer. This might be what
you mean by the fact that you " don't know how to chose a type in the
struct for void *". I suspect that means you don't know what type buf
should have.

If you don't want an array in the struct then the fread call is probably
wrong. The count argument should be the number of characters that you
want to read (size of p.buf is a small number like 4 or 8 -- it's the
size of a pointer).

> Is there no way to change a return type as declared in a struct's
> elements ?


I don't know what this means. Functions have a return type, but a
struct's elements just has a type.

> The elements are all of type struct param now.


That does not make sense. Two are of type char * and one is of type
void *.

> Should I just leave the void * variable out of the struct and declare it
> as a type char * or int * or size_t * whatever I want later as I need
> it?


It's impossible to tell because you give no context. What is this
struct for? It may be fine as it is of it could be all wrong. Without
context is just three bit of data collected together for no apparent
reason.

> Is this clear?


Not without more context.

--
Ben.

Bill Cunningham 09-14-2012 03:23 AM

Re: struct and pointer question
 
Ben Bacarisse wrote:
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>
>> In fread/fwrite the first parameter takes a pointer to a void.
>> The generic pointer. I wrote a struct that would insert the in file
>> name and out file name through fopen. My struct looks like this.
>>
>> struct param {
>> char *infile;
>> char *outfile;
>> void *buf;
>> };
>> In the function I wrote that takes struct param as a type I don't
>> know how to chose a type in the struct for void *. I have:
>>
>> fread(p.buf,1,sizeof p.buf,p.infile);

>
> You probably want buf to be array, not a pointer. This might be what
> you mean by the fact that you " don't know how to chose a type in the
> struct for void *". I suspect that means you don't know what type buf
> should have.
>
> If you don't want an array in the struct then the fread call is
> probably wrong. The count argument should be the number of
> characters that you want to read (size of p.buf is a small number
> like 4 or 8 -- it's the size of a pointer).
>
>> Is there no way to change a return type as declared in a struct's
>> elements ?

>
> I don't know what this means. Functions have a return type, but a
> struct's elements just has a type.
>
>> The elements are all of type struct param now.

>
> That does not make sense. Two are of type char * and one is of type
> void *.
>
>> Should I just leave the void * variable out of the struct and
>> declare it as a type char * or int * or size_t * whatever I want
>> later as I need
>> it?

>
> It's impossible to tell because you give no context. What is this
> struct for? It may be fine as it is of it could be all wrong.
> Without context is just three bit of data collected together for no
> apparent reason.
>
>> Is this clear?

>
> Not without more context.


I suspect the author of fread and fwrite used the generic pointer type
for a reason. So you could change array types like for example char
buf[200]; or int buf [1000]; I usually use a size_t as the array type. But
putting the type void * in the prototype gives flexibility normally. But
what about in the struct? Do I need to change the type to int * or size_t *
for the array type. I don't know how to change it later to a int * or char *
..

Make Sense? If not I'll post some code.

Bill



Barry Schwarz 09-14-2012 06:26 AM

Re: struct and pointer question
 
On Thu, 13 Sep 2012 23:23:06 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>Ben Bacarisse wrote:
>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>
>>> In fread/fwrite the first parameter takes a pointer to a void.
>>> The generic pointer. I wrote a struct that would insert the in file
>>> name and out file name through fopen. My struct looks like this.
>>>
>>> struct param {
>>> char *infile;
>>> char *outfile;
>>> void *buf;
>>> };
>>> In the function I wrote that takes struct param as a type I don't
>>> know how to chose a type in the struct for void *. I have:
>>>
>>> fread(p.buf,1,sizeof p.buf,p.infile);

>>
>> You probably want buf to be array, not a pointer. This might be what
>> you mean by the fact that you " don't know how to chose a type in the
>> struct for void *". I suspect that means you don't know what type buf
>> should have.
>>
>> If you don't want an array in the struct then the fread call is
>> probably wrong. The count argument should be the number of
>> characters that you want to read (size of p.buf is a small number
>> like 4 or 8 -- it's the size of a pointer).
>>
>>> Is there no way to change a return type as declared in a struct's
>>> elements ?

>>
>> I don't know what this means. Functions have a return type, but a
>> struct's elements just has a type.
>>
>>> The elements are all of type struct param now.

>>
>> That does not make sense. Two are of type char * and one is of type
>> void *.
>>
>>> Should I just leave the void * variable out of the struct and
>>> declare it as a type char * or int * or size_t * whatever I want
>>> later as I need
>>> it?

>>
>> It's impossible to tell because you give no context. What is this
>> struct for? It may be fine as it is of it could be all wrong.
>> Without context is just three bit of data collected together for no
>> apparent reason.
>>
>>> Is this clear?

>>
>> Not without more context.

>
> I suspect the author of fread and fwrite used the generic pointer type
>for a reason. So you could change array types like for example char
>buf[200]; or int buf [1000]; I usually use a size_t as the array type. But
>putting the type void * in the prototype gives flexibility normally. But
>what about in the struct? Do I need to change the type to int * or size_t *
>for the array type. I don't know how to change it later to a int * or char *


You have allowed the presence of the structure to completely obscure
whatever the real topic of your question is. Your real question seems
to be how to use fread and fwrite. Both functions require four pieces
of information:

1 - The address of a buffer. fread will store data obtained from
the stream in this buffer. fwrite will copy the data from the buffer
to the stream. No matter what type of address you provide, it will be
converted to a void* if it has a different type. If you define the
buffer as an array and use the array name as the argument, then you
don't have to worry about the type since the array name will be
automatically converted to the address of the first element with the
correct type. If you dynamically allocate the buffer or otherwise
designate its address in a pointer, then you would use the pointer as
the argument. While neither fread nor fwrite care about the pointer
type, when you attempt to access the elements of the buffer, it is
your responsibility to make sure that the pointer is of the correct
type.

2 - The size of a single unit of data in the buffer. One
possibility would be sizeof(int) if the buffer contains objects of
type int.

3 - The number of objects to be transferred to or from the
buffer.

4 - The address of a FILE object usually obtained from fopen().
Neither fread nor fwrite take the file name as an argument as you
coded in your original post.

>Make Sense? If not I'll post some code.


Since you tend to post code that has nothing to do with your question,
you should include a verbal description of what it you intend to
accomplish.

--
Remove del for email

Nick Keighley 09-14-2012 10:24 AM

Re: struct and pointer question
 
On Sep 14, 3:26*am, "Bill Cunningham" <nos...@nspam.invalid> wrote:

> * * In fread/fwrite the first parameter takes a pointer to a void. The
> generic pointer. I wrote a struct that would insert the in file name and out
> file name through fopen.


what does "through fopen" mean?


> My struct looks like this.
>
> struct param {
> * * char *infile;
> * * char *outfile;
> * * void *buf;};


looks ok

> In the function I wrote that takes struct param as a type


you mean "as a parameter" or "as an argument"

> I don't know how
> to chose a type in the struct for void *.


I've no idea what this means. void* IS a type.

> I have:
>
> * *fread(p.buf,1,sizeof p.buf,p.infile);


ok. But make sure buf is pointing to something. Where do you think
fread(0 is going to put the data it reads?

> Is there no way to change a return type as declared in a struct's elements ?


no idea what this means. A structs elements do not declare a "return
type"

> The elements are all of type struct param now.


what elements? The elements in struct param are certainly not of type
struct param

> * * Should I just leave the void * variable out of the struct and declare it
> as a type char * or int * or size_t * whatever I want later as I need it?


no idea

> * * Is this clear?


you're kidding, right?

Barry Schwarz 09-14-2012 05:34 PM

Re: struct and pointer question
 
On Fri, 14 Sep 2012 03:24:27 -0700 (PDT), Nick Keighley
<nick_keighley_nospam@hotmail.com> wrote:

>On Sep 14, 3:26*am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
>
>> * * In fread/fwrite the first parameter takes a pointer to a void. The
>> generic pointer. I wrote a struct that would insert the in file name and out
>> file name through fopen.

>
>what does "through fopen" mean?
>
>
>> My struct looks like this.
>>
>> struct param {
>> * * char *infile;
>> * * char *outfile;
>> * * void *buf;};

>
>looks ok
>
>> In the function I wrote that takes struct param as a type

>
>you mean "as a parameter" or "as an argument"
>
>> I don't know how
>> to chose a type in the struct for void *.

>
>I've no idea what this means. void* IS a type.
>
>> I have:
>>
>> * *fread(p.buf,1,sizeof p.buf,p.infile);

>
>ok. But make sure buf is pointing to something. Where do you think
>fread(0 is going to put the data it reads?


I wonder what fread will do with a char* when it is expecting a FILE*
as the fourth argument.


--
Remove del for email

Bill Cunningham 09-14-2012 06:41 PM

Re: struct and pointer question
 
Barry Schwarz wrote:

> I wonder what fread will do with a char* when it is expecting a FILE*
> as the fourth argument.


I didn't notice that error. It's not in my code either. I don't seem to
be making myself clear. I will post some code later when I get the time. If
that doesn't work I'll go from there.

Bill



Angel 09-14-2012 08:03 PM

Re: struct and pointer question
 
On 2012-09-14, Bill Cunningham <nospam@nspam.invalid> wrote:
> In fread/fwrite the first parameter takes a pointer to a void. The
> generic pointer. I wrote a struct that would insert the in file name and out
> file name through fopen. My struct looks like this.
>
> struct param {
> char *infile;
> char *outfile;
> void *buf;
> };
> In the function I wrote that takes struct param as a type I don't know how
> to chose a type in the struct for void *. I have:
>
> fread(p.buf,1,sizeof p.buf,p.infile);
>
> Is there no way to change a return type as declared in a struct's elements ?
> The elements are all of type struct param now.
> Should I just leave the void * variable out of the struct and declare it
> as a type char * or int * or size_t * whatever I want later as I need it?
>
> Is this clear?


It's pretty clear that you have no idea what you're talking about or
what you are doing, yes. You've asked about fread() and fwrite()
several times now and you still don't seem to understand what these
functions do. (As well as generic confusion about structures,
functions, and return values.)

fread() reads objects from a stream into memory. fwrite() writes
objects from memory into a stream. In both cases, it is your job to
provide the following information to the function:
1. The location in memory to read from or write to.
2. The size of one single object, in bytes.
3. The number of objects to be read or written.
4. The stream to read from or write to.

The first argument of fread() and fwrite() is of type void* because
these functions can be used to read or write any kind of object, hence
you can pass any type of pointer to it. It's your duty to pass a
pointer that matches with your needs. (If you are reading char objects,
use a pointer to char. If you're writing structures, pass a pointer to
a structure.)

Here is a very simple example of a program that writes some structures
to a file:


#include <stdio.h>

struct Person
{
char name[20];
unsigned int age;
};

int main(void)
{
struct Person people[] = { { "John", 34u },
{ "Dick", 48u },
{ "Harry", 25u } };

FILE *file = fopen("people.db", "wb");
fwrite(people, sizeof (people[0]), 3, file);
fclose(file);
}


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

Bill Cunningham 09-14-2012 09:29 PM

Re: struct and pointer question
 
Barry Schwarz wrote:
> On Fri, 14 Sep 2012 03:24:27 -0700 (PDT), Nick Keighley
> <nick_keighley_nospam@hotmail.com> wrote:
>
>> On Sep 14, 3:26 am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
>>
>>> In fread/fwrite the first parameter takes a pointer to a void. The
>>> generic pointer. I wrote a struct that would insert the in file
>>> name and out file name through fopen.

>>
>> what does "through fopen" mean?
>>
>>
>>> My struct looks like this.
>>>
>>> struct param {
>>> char *infile;
>>> char *outfile;
>>> void *buf;};

>>
>> looks ok
>>
>>> In the function I wrote that takes struct param as a type

>>
>> you mean "as a parameter" or "as an argument"
>>
>>> I don't know how
>>> to chose a type in the struct for void *.

>>
>> I've no idea what this means. void* IS a type.
>>
>>> I have:
>>>
>>> fread(p.buf,1,sizeof p.buf,p.infile);

>>
>> ok. But make sure buf is pointing to something. Where do you think
>> fread(0 is going to put the data it reads?

>
> I wonder what fread will do with a char* when it is expecting a FILE*
> as the fourth argument.


#include <stdio.h>

struct param {
char *infile;
char *outfile;
void *buf;
};

#include "p.h"

int copi(struct param p)
{
size_t nread, nwrite;
FILE *in, *out;
if ((in = fopen(p.infile, "rb")) == NULL) {
perror("fopen 1");
return 1;
}
if ((out = fopen(p.outfile, "wb")) == NULL) {
perror("fopen 2");
return 2;
}
do {
nread = fread(p.buf, 1, sizeof p.buf, in);
nwrite = fwrite(p.buf, 1, nread, out);
}
while (!feof(in));
fclose(in);
fclose(out);
printf("%zu %zu\n", nread, nwrite);
return 0;
}

My question concerns the generic pointer in the struct. It's the prototype
for fread/fwrite in my copi function but what if I wanted a int * or char *
how would I choose that in a file calling main? Say I want buf to be a
size_t for example.

Bill



Bill Cunningham 09-14-2012 09:31 PM

Re: struct and pointer question
 
Angel wrote:
> It's pretty clear that you have no idea what you're talking about or
> what you are doing, yes. You've asked about fread() and fwrite()
> several times now and you still don't seem to understand what these
> functions do.


[snip]

I write successful code with fread and fwrite all the time.

Bill




All times are GMT. The time now is 07:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.