• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Calling and filling a struct from another struct (nested structs) in C

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, first post here!

I have an exercise, where I should store in lists the len of strings entered by the user, and for every new work,  create a new node. So, I have the following struct in my code

I can totally initialize and fill the COUNT



Ultimately, what I want to do, is for different input words, call struct COUNT from inside struct occurrence and basically for every word that is given, store the length of it in the list. So, how should I initialize the struct occurrence and call the COUNT from within, so something line


but I dont get it to work...

I hope my question is clear enough


EDIT - actual code is here https://onlinegdb.com/Ski9loEwO
 
Saloon Keeper
Posts: 23515
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Al!

You can't "call" a struct. You can only call code. The more accurate way of saying what you want is that you'd like to set the result of a function call as a referenced member of an embedded struct (yikes!)

Technically, I don't see anything wrong with your expression, but as we say here, https://coderanch.com/wiki/660111/Doesnt-Work-Useless

If you could provide us some information on what's actually happening when you try to do this it would help a lot!
 
Rancher
Posts: 301
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Al

Couple of things I noticed...
That shouldn't even compile; malloc() only takes one argument. What OS/compiler are you using?
I think you think sizeof(textin) is the size of the memory area textin is pointing out, but it isn't; it's the size of the textin variable. It's a pointer, so depending on your OS, it's probably 8 bytes.

You read the string into the memory pointed to by textin, but I can't see you doing anything else with it. Doesn't it need to be stored in node?
 
Al Kas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:Hi Al

Couple of things I noticed...
That shouldn't even compile; malloc() only takes one argument. What OS/compiler are you using?
I think you think sizeof(textin) is the size of the memory area textin is pointing out, but it isn't; it's the size of the textin variable. It's a pointer, so depending on your OS, it's probably 8 bytes.

You read the string into the memory pointed to by textin, but I can't see you doing anything else with it. Doesn't it need to be stored in node?



Ok, I just commented this part out - I haven't actually tested that, I just pasted a part of the whole code, as the main quest was  how to use the nested struct...
 
Al Kas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Welcome to the Ranch, Al!

You can't "call" a struct. You can only call code. The more accurate way of saying what you want is that you'd like to set the result of a function call as a referenced member of an embedded struct (yikes!)

Technically, I don't see anything wrong with your expression, but as we say here, https://coderanch.com/wiki/660111/Doesnt-Work-Useless

If you could provide us some information on what's actually happening when you try to do this it would help a lot!



I guess, the question is how to properly initialize the occurence and then fill, and then move to the next node of the COUNT list.
 
Marshal
Posts: 72612
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't edit posts like that after they have been repleid to; it makes the thread difficult to follow. I have reverted the edit.
 
Al Kas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Welcome to the Ranch, Al!

You can't "call" a struct. You can only call code. The more accurate way of saying what you want is that you'd like to set the result of a function call as a referenced member of an embedded struct (yikes!)

Technically, I don't see anything wrong with your expression, but as we say here, https://coderanch.com/wiki/660111/Doesnt-Work-Useless

If you could provide us some information on what's actually happening when you try to do this it would help a lot!



Ok, so here what I do


counter increases every time the user enters a new word. So, for each word, a occurence should be initialized

but that gives a segmentation fault

please find the code here

https://onlinegdb.com/Ski9loEwO
 
John Matthews
Rancher
Posts: 301
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't set the value of that ocurrence_list_thehead pointer field, and as it's in a global array, it will be 0, a NULL pointer. You need to make it point to something valid before you try to write to where it's pointing to.
 
John Matthews
Rancher
Posts: 301
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, have you tried debugging it? Just add some printfs to show you what is (or isn't) happening.

And try correcting the indentation. If you do, you'll see that the return at the bottom of the code is inside the while(1) loop; presumably that's not what you want?
 
Al Kas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:You haven't set the value of that ocurrence_list_thehead pointer field, and as it's in a global array, it will be 0, a NULL pointer. You need to make it point to something valid before you try to write to where it's pointing to.



But I think I do at L64, no ? In any case, could you maybe help here editing/suggesting what the code should be so that it works ? ...I am afraid I am not advanced enough to understand what I need to do in terms of coding from this point onwards...
 
John Matthews
Rancher
Posts: 301
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, no, not yet anyway. I haven't really grasped what it is you're doing yet, just been looking at the mechanics of the code.

Off to bed now; will have another look tomorrow if I get chance, but hopefully someone else might be able to help.
 
John Matthews
Rancher
Posts: 301
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Kas wrote:But I think I do at L64, no ?

No - you only set occ, not any of the ocurrence_list_thehead fields in the array that occ is pointing to.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic