• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Issues with file reading using structs

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So this is an example of what I was trying to do but I was having a segfault or something when I run it. Also I noticed when I replace the struct values with regular variables it works fine. Why am I having this issue ?
 
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many numbers does the file contain? Are there an exact multiple of 3?
Please be more specific about what went wrong.
 
Rancher
Posts: 329
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because test is a pointer to a structure, but it's not actually pointing at a structure; it could be pointing anywhere.

Make test a structure rather than a pointer to a structure (so test-> becomes test.).
 
John Matthews
Rancher
Posts: 329
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...or you could do something like:
and leave the rest unchanged.
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How many numbers does the file contain? Are there an exact multiple of 3?
Please be more specific about what went wrong.




th file data looks kind of like this:

 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:...or you could do something like:
and leave the rest unchanged.



what does doing that do?
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also would that still follow the instructions:
"dynamically create a struct Test pointed by test"
 
John Matthews
Rancher
Posts: 329
11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
test is still a pointer to a structure, but it also defines one of those structures, aTest, and makes test point to it.
 
John Matthews
Rancher
Posts: 329
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...but to dynamically create a structure you need to use malloc() (which will replace aTest).
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh so is it looking for something like ?
 
John Matthews
Rancher
Posts: 329
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that would work. Although...

You don't need to cast the malloc() return value (unless your code is C++); malloc() returns a void*, and they don't need to be cast to other pointer types.

Rather than do sizeof(struct Test), the code would be more robust if you used sizeof(*test), the size of whatever test is pointing at. That makes the code independent of the type.
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok thank you
 
John Matthews
Rancher
Posts: 329
11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing - I would use
just so you can shorten 'struct Test' to 'Test'.
 
pie. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic