• C

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

How do you proceed to the next node for a struct within a struct  (eg, temp->NODE = temp->NODE.next;)
blaze_wkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SadrulCommented:
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
cjjcliffordCommented:
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
stefan73Commented:
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
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

stefan73Commented:
Example:

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

struct ListOfSomething{
    struct Node* NODE;
    ...
}
0
blaze_wkAuthor Commented:

     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
blaze_wkAuthor Commented:
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
SadrulCommented:
what's MESSAGE?

-- Adil
0
blaze_wkAuthor Commented:
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
Kent OlsenData Warehouse Architect / DBACommented:
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
SadrulCommented:
ok. what's nodule? what's element? where did you use node, node2?

-- Adil
0
blaze_wkAuthor Commented:
i tried  temp->nodule->num = temp2->num;

but this are the errors :(
invalid type argument of `->'
incompatible types in assignment
0
Kent OlsenData Warehouse Architect / DBACommented:
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
SadrulCommented:
eh... sorry. ignore my last message :blush:

-- Adil
0
blaze_wkAuthor Commented:
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
blaze_wkAuthor Commented:
but my program worked fine without adding that pointer, its just that the compiler isn't accepting
temp->NODULE = temp->NODULE.next
0
cjjcliffordCommented:
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
imladrisCommented:
Since it is talking about incompatible types, I think it would be really helpful if you posted the definition of nodule.
0
blaze_wkAuthor Commented:
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
van_dyCommented:
How have you declared temp and temp2 ?
0
cjjcliffordCommented:
"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
van_dyCommented:
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
blaze_wkAuthor Commented:
;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
van_dyCommented:
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
cjjcliffordCommented:
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
van_dyCommented:
By the way, what is MESSAGE ?
0
cjjcliffordCommented:
"my own whole " ... wow, fantastic grammer there... should be "my own hole"

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

Cheers,
C.
0
blaze_wkAuthor Commented:
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
cjjcliffordCommented:

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
van_dyCommented:
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
blaze_wkAuthor Commented:
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
blaze_wkAuthor Commented:

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
van_dyCommented:
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
blaze_wkAuthor Commented:
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
van_dyCommented:
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
blaze_wkAuthor Commented:
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
van_dyCommented:
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
blaze_wkAuthor Commented:

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
van_dyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
blaze_wkAuthor Commented:
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
van_dyCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.