structs and pointers

B

Bill Cunningham

I have this code written like this:

#include <stdio.h>
#include <sys/stat.h>

int main()
{
int i;
struct stat st;
if ((i = stat("/bin/e", &st)) == -1) {
fputs("stat error", stderr);
return -1;
}
printf("%i\n", st.st_blocks);
printf("%i\n", st.st_blksize);
return 0;
}

Now if I wanted instead of

struct stat st;
struct stat *st;

How would that change the second parameter to stat()? Would it be st instead
of &st, or *st ?

Bill
 
L

Lew Pitcher

I have this code written like this:

#include <stdio.h>
#include <sys/stat.h>

int main()
{
int i;
struct stat st;
if ((i = stat("/bin/e", &st)) == -1) {
fputs("stat error", stderr);
return -1;
}
printf("%i\n", st.st_blocks);
printf("%i\n", st.st_blksize);
return 0;
}

Now if I wanted instead of

struct stat st;
struct stat *st;

How would that change the second parameter to stat()? Would it be st
instead of &st, or *st ?

Hi, Bill

Take a look at the following code, based on your example above...

#include <stdio.h>
#include <sys/stat.h>

int main()
{
int i;

struct stat statstruct;
struct stat *st;

/* Initialize st
** st is a "pointer to struct stat", so
** it takes the address of a struct stat
** to initialize st
*/
st = &statstruct;

/* stat() needs a "pointer to struct stat"
** and that's exactly what st is
*/
if ((i = stat("/bin/e", st)) == -1) {
fputs("stat error", stderr);
return -1;
}

/* st is a "pointer to struct stat"
** so, to access the struct stat members
** through st, we have to dereference the
** pointer. Two methods are shown
*/
printf("%i\n", (*st).st_blocks); /* method 1 - *(st)->member */
printf("%i\n", st->st_blksize); /* method 2 - st->member */

return 0;
}

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Bill Cunningham

Richard said:
2/10. You're slipping Bill.

The return of 0 and -1 and use of stat was a nice touch though. But
surely even you don't expect us to believe you are still totally
incompetent and don't know what a pointer is still?

I know what a pointer is. Working with them is different. Dereferencing
is my problem.

You need some new
 
B

Bill Cunningham

It would be st. But you'd have to make sure you've allocated space.
Search for the recent thread "problem on fonction mkdir" (misspelling
intentional) to see someone using stat() in that manner. Unless
you have a good reason not to use the &st form, stick with &st.

That makes sense. Since I don't fully understand working with pointers.
The prototype of stat()'s second parameter takes a pointer to type struct.
So I want to declare a pointer like such. struct stat *sp;

I am reading prototypes a little better now.

Bill
 
B

Bill Cunningham

You don't understand how to dereference a pointer even though you know
what a pointer is?

You never saw "*p" ?

Yes that's declaring a pointer. char *p;
 
K

Keith Thompson

Bill Cunningham said:
Yes that's declaring a pointer. char *p;

It's also the syntax for dereferencing a pointer.

#include <stdio.h>
int main(void)
{
int n = 42;
int *p; /* declared p as a pointer; p's type is int* */
p = &n;
printf("*p = %d\n", *p); /* The "*" operator dereferences p */
return 0;
}
 
B

Bill Cunningham

It's also the syntax for dereferencing a pointer.

#include <stdio.h>
int main(void)
{
int n = 42;
int *p; /* declared p as a pointer; p's type is int* */
p = &n;
printf("*p = %d\n", *p); /* The "*" operator dereferences p */
return 0;
}
int *p pointer to int
p=&n pointer p points to the location in memory allocated for int n where
the value 42 is in memory.
The printf line is totally confusing and throughs me off. Why would one want
to dereference? If you want to print the location in memory p points to I
guess you would do this.

printf("%p\n",p);

If you wanted to print the value at n's locaation in memory which is stored
there ie 42 Then would you use

printf("%i\n",*p) ???

I've heard of dereferencing but have no idea what it is. I am thinking
pointers are being declared.

Bill
 
K

Keith Thompson

Bill Cunningham said:
int *p pointer to int
p=&n pointer p points to the location in memory allocated for int n where
the value 42 is in memory.
The printf line is totally confusing and throughs me off. Why would one want
to dereference? If you want to print the location in memory p points to I
guess you would do this.

printf("%p\n",p);

If you wanted to print the value at n's locaation in memory which is stored
there ie 42 Then would you use

printf("%i\n",*p) ???

Note that that's almost the same thing as the printf statement that I
wrote, that you found so confusing. "%i" and "%d" mean the same thing
in format strings.

Dereferencing a pointer means getting what it points to. p is a
pointer object; its value is an address (it happens to be the address
of n). *p is an int object, the same object as n. The value of *p is
42.
I've heard of dereferencing but have no idea what it is. I am thinking
pointers are being declared.

Declaring a pointer and dereferencing a pointer are very different
things, though they use similar syntax (for reasons I won't get into).

A pointer object contains an address. Dereferencing the pointer
object gives you the thing that the pointer points to.

(I see that "Han from China" and Richard nolastname have posted in
this thread. Be aware that I won't see anything they write.)
 
R

R J C

Keith Thompson scribbled:
(I see that "Han from China" and Richard nolastname have posted in
this thread. Be aware that I won't see anything they write.)

Keith, I know Han from China and Richard stir the pot on this newsgroup
and probably deserve not to be read, but you /do/ come across as being
somewhat obsessed. Neither Han from China nor Richard said anything
about you in this thread, and therefore one does have to wonder what
your problem is and why you seem to enjoy inciting the same drama
they seem to enjoy inciting. I've been lurking this newsgroup, and even
tho I've criticized HfC's hyper-ISO mode as being deliberately
unhelpful, he /does/ help a lot of people. I've seen you help a lot of
people too. If only as a matter of self-marketing, you should give the
jabs a rest unless there is an obvious need for them {and I think we
can both agree that there will be an obvious need sooner or later}, for
they do your image no favors and quite a bit of damage.

My $0.2

Rob
 
K

Keith Thompson

R J C said:
Keith Thompson scribbled:

Keith, I know Han from China and Richard stir the pot on this newsgroup
and probably deserve not to be read, but you /do/ come across as being
somewhat obsessed. Neither Han from China nor Richard said anything
about you in this thread, and therefore one does have to wonder what
your problem is and why you seem to enjoy inciting the same drama
they seem to enjoy inciting. I've been lurking this newsgroup, and even
tho I've criticized HfC's hyper-ISO mode as being deliberately
unhelpful, he /does/ help a lot of people. I've seen you help a lot of
people too. If only as a matter of self-marketing, you should give the
jabs a rest unless there is an obvious need for them {and I think we
can both agree that there will be an obvious need sooner or later}, for
they do your image no favors and quite a bit of damage.

No jab was intended, though I can see how it might have appeared that
way. I merely wanted Bill Cunningham to be aware that, whatever HfC
and Richard write in this thread (and apart from your brief account I
honestly have no idea what that might be), I would not be reading it.
For example, if they comment on something I wrote, no conclusions
should be drawn from my failure to respond. In this particular
thread, I thought it was particularly important to do what I can to
avoid any more confusion than already exists.
 
K

Kenny McCormack

Keith Thompson said:
(I see that "Han from China" and Richard nolastname have posted in
this thread. Be aware that I won't see anything they write.)

I can't see you! Really, really, I can't!

But I know you are there (because of my Extra Special Powers!)
 
B

Barry Schwarz

That makes sense. Since I don't fully understand working with pointers.
The prototype of stat()'s second parameter takes a pointer to type struct.
So I want to declare a pointer like such. struct stat *sp;

Not necessarily. Your original code passed a pointer to struct by
taking the address of an existing struct. Not knowing anything about
stat(), all I can say is that most functions that take a pointer to
struct really expect this. You only need to declare a pointer to
struct object when your code needs one. In this case it doesn't.

If you actually passed a pointer object to the function, you would
have to insure the pointer object contained a valid value. The only
two valid values would be the address of such a struct (or the address
of a block of allocated memory at least large enough to hold such a
struct) or NULL. And many functions are not prepared to handle NULL
values.
I am reading prototypes a little better now.

Prototypes provide only the MINIMUM amount of information you need to
code a function call. They do not tell you anything about how the
function uses the arguments. If you don't know that, your chances of
using the function are somewhat remote.

In this case, I expect the function will attempt to store values in
the struct being pointed to. There are other cases where the function
requires a pointer to struct simply because it is usually more
efficient to pass a pointer than to pass the entire struct.
 

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,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top