Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

structs (not a complicated question, just need it answered immediately ;D)

Posted on 2004-11-04
40
Medium Priority
?
341 Views
Last Modified: 2008-02-01
How do you proceed to the next node for a struct within a struct  (eg, temp->NODE = temp->NODE.next;)
0
Comment
Question by:blaze_wk
  • 15
  • 10
  • 6
  • +4
40 Comments
 
LVL 2

Expert Comment

by:Sadrul
ID: 12494396
it depends on what temp is, and what data-members it has.

if temp is of type NODE, and it has two members: data and next, then you would do something like:
NODE *temp = ...;

temp = temp->next;

-- Adil
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12494399
struct node {
    struct node *next;
};
struct node listhead;

...


struct node elem = listhead;
// use next is NULL to be the end of the list
while( elem->next ) {
    elem = elem->next;
    printf( "moving on...\n" );
}

to add to the list at the end:

elem->next = malloc( sizeof( struct node ) );
elem->next->next = NULL; // make this the end!

Note you should correctly initialise all data in the node when allocated...

to deallocate:

// ONLY do this on the next-to-end element...
free( elem->next );
elem->next = NULL;
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12494409
Hi blaze_wk,
What you wrote looks OK, as long as temp->node is a NODE* pointer and NODE contains a NODE.next pointer.

Cheers!

Stefan
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:stefan73
ID: 12494421
Example:

struct Node{
    struct Node* next;
    ...
}

struct ListOfSomething{
    struct Node* NODE;
    ...
}
0
 

Author Comment

by:blaze_wk
ID: 12494554

     for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
              temp->MESSAGE.num = temp2->num; /*this is what i did, but error says it's incompatible types in assignment */
              strcpy((temp->MESSAGE.contents),temp2->contents);
              temp->MESSAGE = temp->MESSAGE.next;
           }
        }
     }
0
 

Author Comment

by:blaze_wk
ID: 12494583
structs are as below

typedef struct node {
  int num;
  char contents[10];
  struct node *next;
}nodule;

typedef struct node2 {
  int num;
  nodule NODULE;
  struct node2 *next;
}element;

0
 
LVL 2

Expert Comment

by:Sadrul
ID: 12494642
what's MESSAGE?

-- Adil
0
 

Author Comment

by:blaze_wk
ID: 12494701
ah sorrry typo errors when pasting here

nodule *head = NULL;
nodule *temp2;
element *temp;
element *head2 = NULL;

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
              temp->nodule.num = temp2->num; /*this is what i did, but error says it's incompatible types in assignment */
              strcpy((temp->nodule.contents),temp2->contents);
              temp->nodule = temp->nodule.next;
           }
        }
     }
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12494798
Hi Blaze,

>>   temp->nodule.num = temp2->num; /*this is what i did, but error says it's incompatible types in assignment */

try:

 temp->nodule->num = temp2->num; /*this is what i did, but error says it's incompatible types in assignment */



0
 
LVL 2

Expert Comment

by:Sadrul
ID: 12494803
ok. what's nodule? what's element? where did you use node, node2?

-- Adil
0
 

Author Comment

by:blaze_wk
ID: 12494827
i tried  temp->nodule->num = temp2->num;

but this are the errors :(
invalid type argument of `->'
incompatible types in assignment
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12494869
Hi Blaze,

You aren't really making a "list within a list".  Check the struct:

typedef struct node2 {
  int num;
  nodule NODULE;
  struct node2 *next;
}element;


NODULE needs to be a pointer so that you can have a list of nodules:

typedef struct node2 {
  int num;
  nodule *NODULE;
  struct node2 *next;
}element;

This will, of course, mean a few other minor changes, too.


Kent

0
 
LVL 2

Expert Comment

by:Sadrul
ID: 12494954
eh... sorry. ignore my last message :blush:

-- Adil
0
 

Author Comment

by:blaze_wk
ID: 12495108
so by assigning a pointer to

typedef struct node2 {
  int num;
  nodule *NODULE;
  struct node2 *next;
}element;

what would be the proper syntax to accessing temp->NODULE-> contentes
   
temp->nodule.num = temp2->num;
              strcpy((temp->NODULE.contents),temp2->contents);
              temp->NODULE = temp->NODULE.next;
0
 

Author Comment

by:blaze_wk
ID: 12495326
but my program worked fine without adding that pointer, its just that the compiler isn't accepting
temp->NODULE = temp->NODULE.next
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12495562
temp->NODULE is a "struct node", while temp->NUDULE.next is a pointer to that type...

either change temp->NODULE to be a "struct node *", or copy into it what's contained in temp->NODULE.next (memcpy() on "*(temp->NODULE.next) will do it
0
 
LVL 16

Expert Comment

by:imladris
ID: 12495631
Since it is talking about incompatible types, I think it would be really helpful if you posted the definition of nodule.
0
 

Author Comment

by:blaze_wk
ID: 12495810
definition of nodule..
see im making a linked list of linked list
and the particular section im having difficulties in is

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE.num = temp2->num;
              strcpy((temp->NODULE.contents),temp2->contents);
              temp->NODULE = temp->NODULE.next;
           }
        }
     }

cos i need temp->NODULE to proceed to next node in order to continue copying its contents
it's errors for "temp->NODULE = temp->NODULE.next;" is

178: request for member `MESSAGE' in something not a structure or union

've tried "temp->NODULE = temp->NODULE->next;" but it didn't work ;D

0
 
LVL 5

Expert Comment

by:van_dy
ID: 12495883
How have you declared temp and temp2 ?
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12495955
"ve tried "temp->NODULE = temp->NODULE->next;" but it didn't work ;D"

temp->NODULE is a struct, temp->NODULE->next is a pointer, you need to dereference the pointer to assign to the structure:

temp->NODULE = *(temp->NODULE->next);
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12495992
please Post 3 things:

1) definition for nodule and element
2)  declaration for temp and temp2
3)   your for loop as it looks now.
0
 

Author Comment

by:blaze_wk
ID: 12496210
;D thanks for helping everyone
van_dy... they're on of the earlier posts above

 cjjclifford,
ive tried what u said but these were the errors i got
request for member `MESSAGE' in something not a structure or union
invalid type argument of `->'
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12496360
I am assuming the following
------------------------------
typedef struct node {
  int num;
  char contents[10];
  struct node *next;
}nodule;

typedef struct node2 {
  int num;
  nodule NODULE;
  struct node2 *next;
}element;

element *temp;
nodule *temp2;
-----------------------------

with above, your for loop is
for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE.num = temp2->num;
              strcpy((temp->NODULE.contents),temp2->contents);
              temp->NODULE = temp->NODULE.next; << ------here is the problem
           }
        }
    }

to make the above work i would suggest to change the definition of
 elemt to
typedef struct node2 {
  int num;
  nodule *NODULE;
  struct node2 *next;
}element;
and leave the definition of nodule as such. then your for loop should look like

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE->num = temp2->num;
              strcpy((temp->NODULE->contents),temp2->contents);
              temp->NODULE = temp->NODULE.next; << ------here is the problem
           }
        }
    }
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12496420
oops, I fell into my own whole too...

since NODULE is a structure, you get at "next" with NODULE.next, but since NODULE.next is a pointer that has to be dereferenced before assigning!

temp->NODULE = *(temp->NODULE.next);
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12496436
By the way, what is MESSAGE ?
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12496438
"my own whole " ... wow, fantastic grammer there... should be "my own hole"

... meaning "the same trap I was trying to highlight"...

Cheers,
C.
0
 

Author Comment

by:blaze_wk
ID: 12496595
this is what i have (somewhat). Below is not the complete code of my program.. what ive pasted is just the highlight of my troubles ;D

ypedef struct node {
  int num;
  char contents[10]; /*receive its contents thru the commandline with File, dont worry bout this matter it's not of a problem*/
  struct node *next;
}nodule;

typedef struct node2 {
  int num;
  nodule *NODULE;
  struct node2 *next;
}element;

int main()
{
 
nodule *head = NULL;
nodule *temp2;
element *temp;
element *head2 = NULL;

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE->num = temp2->num;
              strcpy((temp->NODULE->contents),temp2->contents);
              temp->NODULE = *(temp->NODULE.next); << ------ ive changed it to this
           }
        }
    }
but the errors i receive now are
request for member `num' in something not a structure or union
request for member `contents' in something not a structure or union
request for member `next' in something not a structure or union
request for member `contents' in something not a structure or union

0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12496756

The only thing I can think of is that the compiler isn't cleaver enough to recognise "NODULE" as being "struct node" - change the definition of "struct node2" to the following:

typedef struct node2 {
  int num;
  struct node NODULE;
  struct node2 *next;
}element;

this may require some other changes in the code to support NODULE being a struct now, and not a typedef... (a typedef is "something not a structure or union")
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12496869
just compile the following prog to verify wat clifford  is suggesting

//--------------------
#define NULL  0

typedef struct node {
  int num;
  char contents[10];
  struct node *next;
}nodule;

typedef struct node2 {
  int num;
  nodule *NODULE;
  struct node2 *next;
}element;

int main()
{
 
nodule *head = NULL;
nodule *temp2;
element *temp;
element *head2 = NULL;

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE->num = temp2->num;
              strcpy((temp->NODULE->contents),temp2->contents);
              temp->NODULE = temp->NODULE->next;
           }
        }
    }
return 0;
}

Do you get the same errors?
0
 

Author Comment

by:blaze_wk
ID: 12496910
typedef struct node {
  int num;
  char contents[10];
  struct node *next;
}nodule;

typedef struct node2 {
  int num;
  struct node NODULE; /* <-------------------------------------------changed from nodule *NODULE;*/
  struct node2 *next;
}element;


int main()
{
 
nodule *head = NULL;
nodule *temp2;
element *temp;
element *head2 = NULL;

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE->num = temp2->num
              strcpy((temp->NODULE->contents),temp2->contents);
              temp->NODULE = *(temp->NODULE.next); << -----------------------------this section is left at this
           }
        }
    }

it compiles properly
but when i run it it's segmentation fault
0
 

Author Comment

by:blaze_wk
ID: 12496923

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE->num = temp2->num
              strcpy((temp->NODULE->contents),temp2->contents);
              temp->NODULE = *(temp->NODULE.next); << ----------------IT"S only when i with this section here segmentation fault happens
           }
        }
    }
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12497007
ummh, i didnt want you to run it. just wanted to see if the compiler
was able to identify NODULE as a pointer to structure. anyways,  did
you compile the program i posted without making any changes to it?
if it compiled successfully then the error is probably coming from some
other part of your program. Why dont u post whatever code you have(all of it)
along with the errors u are getting?
0
 

Author Comment

by:blaze_wk
ID: 12497185
other parts of the program are working fine without   temp->NODULE = *(temp->NODULE.next); which causes segmentation default

i tried

typedef struct node2 {
  int num;
  nodule *NODULE; /* <-------------------------------------------changed from nodule *NODULE;*/
  struct node2 *next;
}element;

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE->num = temp2->num;
              strcpy((temp->NODULE->contents),temp2->contents);
              temp->NODULE = temp->NODULE->next;
           }
        }
    }

this code causes segmentation default even without "temp->NODULE = temp->NODULE->next;" but with


with
typedef struct node2 {
  int num;
  nodule NODULE;<--------------------------- changed from        nodule *NODULE;
  struct node2 *next;
}element;

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE->num = temp2->num;
              strcpy((temp->NODULE->contents),temp2->contents);
              /*temp->NODULE = *(temp->NODULE.next); */  without this line it works perfectly fine .. , only with it segmentation fault happens, but it compiles properly with this line though with it
           }
        }
    }
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12497254
in your second for loop with
typedef struct node2 {
  int num;
  nodule NODULE;<--------------------------- changed from        nodule *NODULE;
  struct node2 *next;
}element;

you are doing
              temp->NODULE = *(temp->NODULE.next);
this can give a segmentation violation.  reason being, it is possible for temp->NODULE.next to be NULL (we check only temp != NULL
in the begining of the for loop). when u dereference a NULL pointer you are going to get segmentation fault.

What are you trying to accomplish exactly? it might be possible to do it
in a diferent/better way.
0
 

Author Comment

by:blaze_wk
ID: 12497314
for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE.num = temp2->num;
              strcpy((temp->NODULE.contents),temp2->contents); /* copying the a file's contents into the linked list of linked list
              /*temp->NODULE = *(temp->NODULE.next); */ the node will then proceed to the next node for next copying, so if i initialise a 0 to this it would work? IT's only this line that's troubling me
           }
        }
    }

0
 
LVL 5

Expert Comment

by:van_dy
ID: 12497441
I believe, you are trying to do the following
  if temp->num == temp2->num,  you want to copy the whole nodule pointed to by temp2 into
temp->NODULE.  Am i right ??

If it is so, what you can do is replace the line  "temp->NODULE = *(temp->NODULE.next);"
with
          temp->NODULE.next = temp2->next;

please clarify
0
 

Author Comment

by:blaze_wk
ID: 12497722

sorry but im having trouble explaining it but i'll give it go
for each temp->NODULE there's char *contents and int num, what im trying to do is that when temp2->num = temp->num, it copies temp2's contents into temp->NODULE.contents, and once it's done it will move one node forward(temp->NODULE->next) for the next array to be copied in..

for example
if temp->num = 3 and temp2-> num = 3 all the strings within temp2 will be copied into temp->NODULE.contents (this is done with               strcpy((temp->NODULE.contents),temp2->contents); /*copies array
              /*temp->NODULE = *(temp->NODULE.next) /* proceeds to the next node to copy next string
the outcome will look like this
5
array 1
array 2
array 3


for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
           temp->NODULE.num = temp2->num;
              strcpy((temp->NODULE.contents),temp2->contents);
              /*temp->NODULE = *(temp->NODULE.next); */
           }
        }
    }
0
 
LVL 5

Accepted Solution

by:
van_dy earned 2000 total points
ID: 12497941
ok i get you now. in that case i would suggest the following approach.

for (temp = head2;temp !=NULL;temp = temp -> next)
     {
        nodule *p = &(temp->NODULE);
        for (temp2 = head;temp2 !=NULL;temp2 = temp2 -> next)
        {
           if ((temp -> num) == (temp2 -> num))
           {
              p->num = temp2->num;
              strcpy((p->contents),temp2->contents);
              /*temp->NODULE = *(temp->NODULE.next); */
              // there are several problems with the above
              // statement. temp->NODULE is a single nodule
              //structure. temp->NODULE.next is not initialised.
              // what you are trying is to generate another linked
              // list within one single temp node. for that you will
              // need dynamic memory allocation. see the following
              p->next = calloc(1, sizeof(nodule));
              p = p->next;
           }
        }
    }

It should work fine now
0
 

Author Comment

by:blaze_wk
ID: 12498221
Excellent!! this is the last bit!!

     for (temp = head2; temp!=NULL; temp = temp -> next)
     {
        printf("Message ");
        printf("%d\n",temp->num);
/* For every temp-> num, it holds a lists of strings. (As located below) How am i to print temp->NODULE.contents untill it's 0*/
        printf("%s\n",&(temp->NODULE.contents)));
        printf("\n");
     }

5
Array 1
Array 2
Array 3

6
Array 1
Array 2
Array 3
   
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12498330
ok what you are trying to do is to
print out the contents for each temp
node. Here is how you should do it.


for (temp = head2; temp!=NULL; temp = temp -> next)
     {
        nodule *node = &temp->NODULE;  
        printf("Message: ");
        printf("%d\n",temp->num);
        for(; node; node = node->next)
            printf("\t%s\n", node->contents);
      }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question