this is the error I got at the bottom:
#include <stdio.h>
// algorithm : use two pointers and they're n-1 nodes apart.
// when first reach the end, 2nd one reaches nth node.

typedef struct node {
struct node* next;
int data;
}node;

node* findnthnode( node *head, int n)
{
node *p1;
node *p2;
p1 = *p1->next->next;


// if head is null, return null
// if n > length of list, return null
// if n = 0, we return the tail.
// if there're two nodes,
// n=0, we return node2,
// n=1, we return node1(head)
// n>=2, we return NULL

// let p1 go n-1 steps
for (; n>0 && p1 ; n--){
p1 = p1->next;
}

if (!p1)
return NULL;
else
p2 = head;

// move p1, p2 together to traverse list
while (p1 && p1->next){
p1 = p1->next;
p2 = p2->next;
}
return p2;
}

int main()

{
// build a list
node a[3] = 0;
node *p;
int i;
a[0].data = 2;
a[0].next = &a[1];
a[1].data = 5;
a[1].next = &a[2];
a[2].data = 3;
a[2].next = NULL;

for (i=0;i<4;i++ ){
p = findnthnode( a, i);
if (p)
printf("found %dth node = %d\n",i, p->data);
else
printf("not found!\n");
}
return 0;
}




10.cpp: In function `node* findnthnode(node*, int)':
10.cpp:14: cannot convert `node' to `node*' in assignment
10.cpp: In function `int main()':
10.cpp:46: invalid initializer


--- On Mon, 10/27/08, peternilsson42 <[EMAIL PROTECTED]> wrote:
From: peternilsson42 <[EMAIL PROTECTED]>
Subject: [c-prog] Re: question about compiler warning
To: [email protected]
Date: Monday, October 27, 2008, 12:14 AM










    
            frank zhu <frank.zhu.mingyu@ ...> wrote:

>

> Hi,

> I've coded a program to find the nth node from the end

> of the linked list.

> It works and get me the right result, but I am puzzled

> as why I get the compiler warnings at the following line,

> p1 = p1->next;

> findnthnode. c:28: warning: assignment from incompatible

> pointer type

<snip>

> #include <stdio.h>

> // algorithm : use two pointers and they're n-1 nodes

> apart. when first 

> reach the end, 2nd one reaches nth node.

> 

> typedef struct {

>     struct node* next;

>     int data;

> }node;



Here you declare an anonymous struct (i.e. a struct with

no tag name) containing a member of type struct node; and

you also create an alias for the anonymous struct called

node. Neither the anonymous struct, or its alias, has

the type struct node.



You probably meant...



typedef struct node { /* note the tag */

     struct node* next;

     int data;

  } node;



> node* findnthnode( node *head, int n)

> {

>     node *p1;

<snip>

>         p1 = p1->next;



The 'next' member of the anonymous struct is a pointer

to a struct node, not a pointer to the anonymous struct.

Indeed, you never actually declare what a struct node

is.



C allows pointers to incomplete types.



The assignment is a constraint violation and conforming

compilers are required to issue a diagnostic. However,

you appear to have your 'buzz' level so low that it's

not reported as an error.



Try p1 = p1->next->next and see how far your compiler

gets!



This is one of the reasons why many C programmers won't

do the typedef. Instead they simply use struct node

throughout.



-- 

Peter




      

    
    
        
         
        
        








        


        
        


      

[Non-text portions of this message have been removed]

Reply via email to