struct and pointer question

A

Angel

Yes! Yes! I hoped I could cross the usenet understanding barrier. This post
is not directly related to the fread fwrite functions I just happened to use
them so I can get used to them. Personally I like fgetc and fputc.

"get used to them"? Weren't you writing successful code with them all
the time?
 
B

Bill Cunningham

Barry said:
Strange that you never post any of this successful code when asking a
question. Or did I miss the smiley?

After a long struggle I seem to have caught onto them atleast well
enough to use them. I seem to IMO have caught onto something. Though my
loops need work.

Bill
 
B

Barry Schwarz

On Fri, 14 Sep 2012 19:07:51 -0400, "Bill Cunningham"

snip
This thread has nothing to do with fread or fwrite.

Then why are you repeatedly asking how to get those functions to work
with int*, char*, and size_t types?

Why don't you tell us what this thread is really about?
 
B

Barry Schwarz

Ben said:
I read this about a dozen times and finally I think I worked it out.
The trouble is that you ask how do so what you think is needed when
that's not what's needed at all.

I think you want a function that can read int one time and chars the
next. Other times you might want it to read size_ts or even something
else like doubles. To do that, you don't change the type of the
buffer, you change the size of the data being read.
[snip]

Problem solved. Thanks Much.

All evidence to the contrary.
 
B

Bill Cunningham

Barry said:
Why don't you tell us what this thread is really about?

It's about passing a struct to a function called copi that calls on fread
fwrite. Indirectly fread and fwrite is being used so maybe it is in a away
about those 2 function's first parameter. Which is void *. This is the
generic pointer. That's why I have void *buf in my struct.

include stdio.h

int main(void) {
struct param p;
p.buf= /*Have no idea what to do now */

Now I can give up on this, or bring it to clc for help. I decided to do
that. I don't know how to code a pointer through a struct for differing
types of pointers like void * is for. I guess it doesn't matter in another
post I was told what type of buffer it is but the size of it so I consider
the question answered. But if you have anything to add I'm listening please
do.

Bill
 
B

Bill Cunningham

Angel said:
"get used to them"? Weren't you writing successful code with them all
the time?

Very funny. Now you know I struggled with those 2 functions for a long time.
My man pages not explaining in a way I could understand and not giving
examples I had to come to the group.

Bill
 
B

Bill Cunningham

Bill said:
It's about passing a struct to a function called copi that calls on
fread fwrite. Indirectly fread and fwrite is being used so maybe it
is in a away about those 2 function's first parameter. Which is void
*. This is the generic pointer. That's why I have void *buf in my
struct.
include stdio.h

int main(void) {
struct param p;
p.buf= /*Have no idea what to do now */

Now I can give up on this, or bring it to clc for help. I decided to
do that. I don't know how to code a pointer through a struct for
differing types of pointers like void * is for. I guess it doesn't
matter in another post I was told what type of buffer it is but the
size of it so I consider the question answered. But if you have
anything to add I'm listening please do.

Bill

Pardon me I want an array.

B
 
B

Ben Bacarisse

Bill Cunningham said:
It's about passing a struct to a function called copi that calls on fread
fwrite. Indirectly fread and fwrite is being used so maybe it is in a away
about those 2 function's first parameter. Which is void *. This is the
generic pointer. That's why I have void *buf in my struct.

include stdio.h

int main(void) {
struct param p;
p.buf= /*Have no idea what to do now */

Now I can give up on this, or bring it to clc for help. I decided to do
that. I don't know how to code a pointer through a struct for differing
types of pointers like void * is for. I guess it doesn't matter in another
post I was told what type of buffer it is but the size of it so I consider
the question answered. But if you have anything to add I'm listening please
do.

I think you missed the point. No one can help if we don't know what the
high-level goal is. Anyone who's guessed what your question means (I
have for example) knows that you should not be trying to do that, but
the right way to do it depends on what the program's high-level goal is.

Imagine this:

Q: I don't know how to alter the return type of a function.
CLC: You can't. What are you trying to do?
Q: I'm trying to alter the return type of a function.
CLC: No, what is the high-level goal?
Q: I want to process input where the data is either a number or a string
like "no data". I've written double get_data(); but I want main to
be able to alter that so get_data returns a char *.
CLC: Ah! You don't do that by "altering the return type". There are
several options...

I know exactly what you want the function to do. But I also know that's
not the right way to do anything in C so it would be pointless my
telling to how to do it. Whatever you program's objective it, there is
a better way than the one you are asking for advice about.
 
B

Bill Cunningham

Ben said:
I think you missed the point. No one can help if we don't know what
the high-level goal is. Anyone who's guessed what your question
means (I have for example) knows that you should not be trying to do
that, but the right way to do it depends on what the program's
high-level goal is.

Imagine this:

Q: I don't know how to alter the return type of a function.
CLC: You can't. What are you trying to do?
Q: I'm trying to alter the return type of a function.
CLC: No, what is the high-level goal?
Q: I want to process input where the data is either a number or a
string like "no data". I've written double get_data(); but I want
main to be able to alter that so get_data returns a char *.
CLC: Ah! You don't do that by "altering the return type". There are
several options...

I know exactly what you want the function to do.

You hit the nail right on the head :)
 
B

Bill Cunningham

Barry said:
Ben said:
I read this about a dozen times and finally I think I worked it out.
The trouble is that you ask how do so what you think is needed when
that's not what's needed at all.

I think you want a function that can read int one time and chars the
next. Other times you might want it to read size_ts or even
something else like doubles. To do that, you don't change the type
of the buffer, you change the size of the data being read.
[snip]

Problem solved. Thanks Much.

All evidence to the contrary.

What do you mean? I'm a little squemish about the !feof(in) in the
while. I was told that loops should look for the positive not the negative.
Now that my mind is coming back. I can actually focus better now from
going down on clonazepam dosage. I'm going to break out kandr2 again. What
do you mean "all evidence to the contrary."

Bill


Bill
 
G

Greg Martin

Pardon me I want an array.

B
If the question is "How is a void* used?" then it is best to just ask.

A void* is a generic pointer. It can't be dereferenced and so must be
cast to another type before being dereferenced. In the case of your
struct a programmer might assign a char* (or other) to buf but before
they can access the value they would need to cast it back to the type
they are using. This is why the size and number of items is required by
fread et al.
 
B

Bill Cunningham

Greg said:
If the question is "How is a void* used?" then it is best to just ask.

A void* is a generic pointer. It can't be dereferenced and so must be
cast to another type before being dereferenced. In the case of your
struct a programmer might assign a char* (or other) to buf but before
they can access the value they would need to cast it back to the type
they are using. This is why the size and number of items is required
by fread et al.

Could you please show me an example of what you mean? I always thought
dereferencing was changing through a pointer the original value that is
pointed to. So I've read on online tutorials. So any pointer to a type can
be dereferenced other than the generic pointer then? OK.

B
 
A

Angel

Could you please show me an example of what you mean? I always thought
dereferencing was changing through a pointer the original value that is
pointed to. So I've read on online tutorials. So any pointer to a type can
be dereferenced other than the generic pointer then? OK.

Yes, any type pointer can be dereferenced, except void pointers. However,
unless the pointer is actually pointing to a valid object of the correct
type, the result is undefined. There is one exception to this; you can
safely access any object as a stream of bytes through a char pointer.
This is actually how the fread() and fwrite() functions work.


For example, here is a function show_bytes() that can be used to show
the bytes in memory for any object you like:

#include <stdio.h>

void show_bytes(void *obj, const size_t size)
{
for (size_t i = 0; i < size; i++)
{
printf("%02x ", *(((unsigned char *) obj) + i));
}
putchar('\n');
}


int main(void)
{
int foo = 42;
double bar = 3.14159265359;
FILE *baz = stdout;

show_bytes(&foo, sizeof foo);
show_bytes(&bar, sizeof bar);
show_bytes(&baz, sizeof baz);
}
 
B

Bill Cunningham

Angel said:
Yes, any type pointer can be dereferenced, except void pointers.
However, unless the pointer is actually pointing to a valid object of
the correct type, the result is undefined. There is one exception to
this; you can safely access any object as a stream of bytes through a
char pointer. This is actually how the fread() and fwrite() functions
work.
[...]

OK thanks for the info. I will remeber that.

B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,078
Messages
2,570,570
Members
47,204
Latest member
MalorieSte

Latest Threads

Top