Thread: Struct node

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    53

    Arrow Struct node

    Code:
    strcut Node * curPtr;
    strcut Node  relation[10];
    
    curPtr= (strcut Node * )relation[0];
    
    while(curPtr->nextPtr != NULL)//find the last node of the list
                      {
                        printf("error check 1\n");
                        fflush(stdout);
                        curPtr =(struct Node)curPtr->nextPtr;
                        if(curPtr ==NULL)
                        {
                          printf("2");}
                      }
    this is part of my code, the output is
    error check1
    error check1
    Segmentation Fault (core dumped)

    I ran it with gdb, it says that the line
    [while(curPtr->nextPtr != NULL)//find the last node of the list]
    is wrong. Need help!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    curPtr= (strcut Node * )relation[0];
    Q. Why are you typcasting that?
    A. Because my compiler complained that relation[0] wasn't a pointer.

    That's because it wasn't a pointer! You shouldn't need to typecast assigning an address to a pointer of the same type.
    Code:
    curPtr = & relation[ X ];
    curPtr = relation + X;
    Pick one.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    I tried both
    curPtr= & relation[x];
    and
    curPtr=relation + x;

    but it gaves me
    main.c:75: warning: assignment from incompatible pointer type

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it doesn't.
    Code:
    int main( void )
    {
        struct foo { int x; } bar[2], *p = & bar[0];
        p = bar + 1;
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    Code:
    curPtr = & relation[i];//set curPtr to the array
                    while(curPtr->nextPtr != NULL)//find the last node of the list
                      {
                        curPtr = curPtr->nextPtr;
                      }
    this is part of mine;

    oh i see whats wrong now, I creat struct node* relation[x];
    thanks man

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    Code:
    struct node{char name[10];} relation[x];
    
    char * str;
    str= relation[x].name;
    Is this the right way of getting name? When I try to use relation[x]->name it gives error.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It still never enters your mind that including the error you get is a good idea, does it?
    Code:
    int main( void )
    {
        struct foo { char name[10]; } bar[2];
        char *p = bar[0].name;
        p = bar[1].name;
        return 0;
    }
    That's fine. So what are you really doing and what is the error you are getting?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    the error is:

    invalid type argument of `->'

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have any -> operators in the sample you posted here. You can just post your actual segment of code that has the error in it, instead of typing it in.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    Code:
    char *relname;
    struct Tablenode {
      char * name;
      struct Tablenode *nextPtr;
    } relation[x];
    
    relname = relation[x]->name;//set relname
    and the error is
    invalid type argument of `->'

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So stop using -> there. Did I use -> in my example? Did you use -> in the example where I said it was fine? No. So why are you using it here, especially when it just told you that it was wrong to do that.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by p595285902 View Post
    Code:
    char *relname;
    struct Tablenode {
      char * name;
      struct Tablenode *nextPtr;
    } relation[x];
    
    relname = relation[x]->name;//set relname
    and the error is
    invalid type argument of `->'
    Two reasons ...
    1) C does not assign strings across the = sign.
    2) It will assign a pointer, which you went ahead and dereferenced.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Two reasons ...
    1) C does not assign strings across the = sign.
    2) It will assign a pointer, which you went ahead and dereferenced.
    1. There aren't actually any strings being assigned. He's assigning to a character pointer, which you can do in every case:
    Code:
    char foo[X];
    char *bar = "";
    char *baz;
    
    baz = foo;
    baz = bar;
    Both possibilities here are perfectly legal.

    2. He didn't dereference anything, because he wasn't working with a pointer to a structure, when he used the [x] notation. He could only use the arrow, if he did this:
    Code:
    struct foo { char name[X]; } bar[2];
    char *baz;
    
    baz = (bar + 1)->name;
    That would have been fine. It didn't have anything to do with dereferencing name.


    Quzah.
    Last edited by quzah; 03-22-2011 at 09:29 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm donating my vector lib.
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 06-24-2010, 06:10 PM
  2. Linked list acting as string
    By drater in forum C Programming
    Replies: 30
    Last Post: 04-30-2008, 07:10 PM
  3. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Problem with Link List
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 04-24-2002, 11:49 PM