Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > structs and pointers

Reply
Thread Tools

structs and pointers

 
 
Bill Cunningham
Guest
Posts: n/a
 
      04-28-2009
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


 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      04-28-2009
On April 28, 2009 13:29, in comp.lang.c, Bill Cunningham
() wrote:

> 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. ------


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      04-28-2009

"Richard" <rgrdev_@gmail.com> wrote in message
news:gt7ght$7ru$...
> 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
> ideas. Try floats - Falconer will be along to make a fool of himself
> once more.



 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      04-28-2009

"Han from China" <autistic-> wrote in message
news: d.net...

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      04-29-2009

"Richard" <rgrdev_@gmail.com> wrote in message
news:gt7v4i$bi5$...

> 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;


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-29-2009
"Bill Cunningham" <> writes:
> "Richard" <rgrdev_@gmail.com> wrote in message
> news:gt7v4i$bi5$...
>
>> 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;


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;
}

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      04-29-2009

"Keith Thompson" <kst-> wrote in message
news:...

> 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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-29-2009
"Bill Cunningham" <> writes:
> "Keith Thompson" <kst-> wrote in message
> news:...
>
>> 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) ???


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.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
R J C
Guest
Posts: n/a
 
      04-29-2009
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-29-2009
R J C <> writes:
> 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.


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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57