* pointer notation
@ 2003-10-28 9:42 km
2003-10-28 10:17 ` Thomas Steudten
2003-10-28 12:00 ` Luciano Moreira - igLnx
0 siblings, 2 replies; 6+ messages in thread
From: km @ 2003-10-28 9:42 UTC (permalink / raw)
To: linux-c-programming
Hi all,
how different is the notation
(p)->next (what do the parenthesis around the pointer signify ? )
different from
p ( while p is a pointer)
regards,
KM
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: pointer notation
2003-10-28 9:42 pointer notation km
@ 2003-10-28 10:17 ` Thomas Steudten
2003-10-28 12:00 ` Luciano Moreira - igLnx
1 sibling, 0 replies; 6+ messages in thread
From: Thomas Steudten @ 2003-10-28 10:17 UTC (permalink / raw)
To: km; +Cc: linux-c-programming
Hi
(p)->next and p->next are the same. I think you saw a sourcecode, where
maybe p is replaced by a macro like #define p ptr->a.
However the priority in c is "()" first, then "->".
> Hi all,
> how different is the notation
> (p)->next (what do the parenthesis around the pointer signify ? )
> different from
> p ( while p is a pointer)
--
Tom
LINUX user since kernel 0.99.x 1994.
RPM Alpha packages at http://alpha.steudten.com/packages
Want to know what S.u.S.E 1995 cdrom-set contains?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: pointer notation
2003-10-28 9:42 pointer notation km
2003-10-28 10:17 ` Thomas Steudten
@ 2003-10-28 12:00 ` Luciano Moreira - igLnx
2003-10-28 17:46 ` km
1 sibling, 1 reply; 6+ messages in thread
From: Luciano Moreira - igLnx @ 2003-10-28 12:00 UTC (permalink / raw)
To: km; +Cc: linux-c-programming
Try to think that p is a MACRO, like as (arbitrary macro):
"#define p pBaseList+5*(x->element)+1"
When you do:
"p->next"
it ll be:
"pBaseList+5*(x->element)+1->next"
the code above seems to be unsafe, so, if you do:
"(p)->next"
or:
"#define p (pBaseList+5*(x->element)+1)"
it ll free or code of "precedence bugs".
Luciano
km wrote, On 28/10/2003 07:42:
> Hi all,
> how different is the notation
> (p)->next (what do the parenthesis around the pointer signify ? )
> different from
> p ( while p is a pointer)
>
> regards,
> KM
>
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: pointer notation
2003-10-28 12:00 ` Luciano Moreira - igLnx
@ 2003-10-28 17:46 ` km
2003-10-28 18:40 ` Mike Pastore
2003-10-28 18:46 ` Jeff Woods
0 siblings, 2 replies; 6+ messages in thread
From: km @ 2003-10-28 17:46 UTC (permalink / raw)
To: Luciano Moreira - igLnx; +Cc: linux-c-programming
Hi all,
i should have posted the snippet before itself to be clear, which i am doing now.
It is from a double-linked list snippet as follows :
----------------CODE START---------------------------------------
#include<stdio.h>
#include<stdlib.h>
struct node
{
int content;
struct node *next;
struct node *previous;
};
/* prototype declaration */
void add(struct node **s,int c);
/*main program*/
int main()
{
struct node *p;
p = NULL;
add(&p,21);
return 0;
}
/* function */
void add(struct node **s,int c)
{
struct node *r,*q=*s;
if(*s==NULL)
{
*s = (struct node *)malloc(sizeof(struct node));
(*s)->previous = NULL; /* (*s) ??? whats that ? */
(*s)->next = NULL;
(*s)->content = c
}
else
{
/*bla bla*/
}
}
----------------CODE END-------------------------------------
In the above code we can see the statement called "(*s)->previous = NULL;". I didnt get what exactly it means to be and how different is *s different from (*s) in this context. can it be written in any other form alternatively ?
thanks for the feedback,
regards,
KM
-----------------------------------------------------------------
On Tue, Oct 28, 2003 at 10:00:01AM -0200, Luciano Moreira - igLnx wrote:
> Try to think that p is a MACRO, like as (arbitrary macro):
> "#define p pBaseList+5*(x->element)+1"
>
> When you do:
> "p->next"
> it ll be:
> "pBaseList+5*(x->element)+1->next"
>
> the code above seems to be unsafe, so, if you do:
> "(p)->next"
> or:
> "#define p (pBaseList+5*(x->element)+1)"
> it ll free or code of "precedence bugs".
>
> Luciano
>
> km wrote, On 28/10/2003 07:42:
>
> >Hi all,
> >how different is the notation
> >(p)->next (what do the parenthesis around the pointer signify ? )
> >different from
> >p ( while p is a pointer)
> >
> >regards,
> >KM
> >
> >-
> >To unsubscribe from this list: send the line "unsubscribe
> >linux-c-programming" in
> >the body of a message to majordomo@vger.kernel.org
> >More majordomo info at http://vger.kernel.org/majordomo-info.html
> >
> >
> >
> >
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: pointer notation
2003-10-28 17:46 ` km
@ 2003-10-28 18:40 ` Mike Pastore
2003-10-28 18:46 ` Jeff Woods
1 sibling, 0 replies; 6+ messages in thread
From: Mike Pastore @ 2003-10-28 18:40 UTC (permalink / raw)
To: km; +Cc: linux-c-programming
Quite simply, s is a pointer to a pointer to a struct. You want to
dereference it once (with a prefix "*") before dereferencing it a second
time (with an infix "->"). Maybe the following pseudo-equivalent code
will help you understand:
struct node **s, *t; /* introduce temporary "t" */
t = *s; /* dereference s, store result in t */
t->previous = NULL; /* set member of t (*s) to NULL */
The infix "->" binds tighter (has a higher precedence) than the prefix
"*". The incorrect statement "*s->previous = NULL;" is therefore
equivalent to "*(s->previous) = NULL;" and will attempt to get the
"previous" member of s and then dereference the result. But s has no
previous member! It is a pointer to a pointer to a struct, NOT a pointer
to a struct!
The use of parenthesis in "(*s)->previous = NULL;" establishes a clear
order of operations (dereference and THEN get member), which a couple
folks have already mentioned.
--
Mike Pastore
mike@oobak.org
On Tue, 2003-10-28 at 17:46, km wrote:
> Hi all,
> i should have posted the snippet before itself to be clear, which i am doing now.
>
> It is from a double-linked list snippet as follows :
> ----------------CODE START---------------------------------------
> #include<stdio.h>
> #include<stdlib.h>
> struct node
> {
> int content;
> struct node *next;
> struct node *previous;
> };
> /* prototype declaration */
> void add(struct node **s,int c);
> /*main program*/
> int main()
> {
> struct node *p;
> p = NULL;
> add(&p,21);
> return 0;
> }
>
> /* function */
> void add(struct node **s,int c)
> {
> struct node *r,*q=*s;
>
> if(*s==NULL)
> {
> *s = (struct node *)malloc(sizeof(struct node));
> (*s)->previous = NULL; /* (*s) ??? whats that ? */
> (*s)->next = NULL;
> (*s)->content = c
> }
> else
> {
> /*bla bla*/
> }
> }
> ----------------CODE END-------------------------------------
> In the above code we can see the statement called "(*s)->previous = NULL;". I didnt get what exactly it means to be and how different is *s different from (*s) in this context. can it be written in any other form alternatively ?
> thanks for the feedback,
> regards,
> KM
>
>
> -----------------------------------------------------------------
> On Tue, Oct 28, 2003 at 10:00:01AM -0200, Luciano Moreira - igLnx wrote:
> > Try to think that p is a MACRO, like as (arbitrary macro):
> > "#define p pBaseList+5*(x->element)+1"
> >
> > When you do:
> > "p->next"
> > it ll be:
> > "pBaseList+5*(x->element)+1->next"
> >
> > the code above seems to be unsafe, so, if you do:
> > "(p)->next"
> > or:
> > "#define p (pBaseList+5*(x->element)+1)"
> > it ll free or code of "precedence bugs".
> >
> > Luciano
> >
> > km wrote, On 28/10/2003 07:42:
> >
> > >Hi all,
> > >how different is the notation
> > >(p)->next (what do the parenthesis around the pointer signify ? )
> > >different from
> > >p ( while p is a pointer)
> > >
> > >regards,
> > >KM
> > >
> > >-
> > >To unsubscribe from this list: send the line "unsubscribe
> > >linux-c-programming" in
> > >the body of a message to majordomo@vger.kernel.org
> > >More majordomo info at http://vger.kernel.org/majordomo-info.html
> > >
> > >
> > >
> > >
> >
> > -
> > To unsubscribe from this list: send the line "unsubscribe
> > linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: pointer notation
2003-10-28 17:46 ` km
2003-10-28 18:40 ` Mike Pastore
@ 2003-10-28 18:46 ` Jeff Woods
1 sibling, 0 replies; 6+ messages in thread
From: Jeff Woods @ 2003-10-28 18:46 UTC (permalink / raw)
To: km; +Cc: Luciano Moreira - igLnx, linux-c-programming
At 10/28/2003 11:16 PM +0530, km wrote:
>In the above code we can see the statement called "(*s)->previous =
>NULL;". I didnt get what exactly it means to be and how different is *s
>different from (*s) in this context. can it be written in any other form
>alternatively ?
"*s" and "(*s)" are exactly the same. However, "*s->next" and "(*s)->next"
are NOT the same because without the parentheses field selection through
the pointer ("->") wouild be done before indirection ("*") of the "handle"
(pointer to a pointer) "s". IOW, the parentheses are only there (as is
usually the case in a C expression) to control precedence of binding.
--
Jeff Woods <kazrak+kernel@cesmail.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-10-28 18:46 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-10-28 9:42 pointer notation km
2003-10-28 10:17 ` Thomas Steudten
2003-10-28 12:00 ` Luciano Moreira - igLnx
2003-10-28 17:46 ` km
2003-10-28 18:40 ` Mike Pastore
2003-10-28 18:46 ` Jeff Woods
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).