LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2006, 06:45 PM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Question Accessing a struct within a struct?


How can I access a struct within a struct? Right now I am attempting this:
Code:
stuct task_struct *task;

for_each_process(task)
{
  printk("%d\n", task->sched_info->pid)
}

Last edited by smoothdogg00; 12-20-2006 at 07:04 PM.
 
Old 12-20-2006, 07:10 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
And your problem is?

Grab thee a piece of paper and draw a picture of what the data-structures are. It's certainly not a problem for a struct to be contained within another struct ... as you can imagine, it happens all the time!

Let's look at your code:
Code:
task->sched_info->pid
Given that you have said that task is "a pointer to a struct task, the question before the jury is .. "is the sched_info component of a task structure 'a pointer to' something that contains a pid?"

(I don't know: I didn't take the time to look for myself before writing this. But then again, neither did gcc...)

By using the construct ->, which is "pointer dereference," you're saying that it does. You're writing code that says that some element of the structure pointed-to by task (that is, a task_struct) contains a pointer, named sched_info, to something (that is, to another struct, located somewhere) that contains an element called pid? "Can I, gcc, your long-suffering but ever-obedient servant, possibly make sense of this thy pronouncement, Oh Grand Master, that I may render executable code from it?" (If not, then it has many incomprehensible ways to declare to thee that thou art full of ka-ka.)

Draw a picture. Grab a piece of paper, or a cocktail napkin, or what have you, and draw a picture. In this case, your bar-napkin drawing would have two boxes... and one box would point to the second one.

It could, of course, be the case that "this thingy-whatchamacallit that is pointed-to by task is actually a pretty big struct which contains, among its many parts, a sub-struct which includes an element called pid. In that case, you'd write something like this:
Code:
task->sched_info.pid
(Notice the "." versus "->".)

Here, your bar-napkin drawing has only one box, and within the outer box there's an inner one.

I emphasize again... draw a picture. If you don't firmly understand what the situation is supposed to look like, then you cannot possibly describe it to a computer.

Last edited by sundialsvcs; 12-20-2006 at 07:15 PM.
 
Old 12-21-2006, 12:06 AM   #3
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Interesting methodology. Thanks for your response, it fixed my problem. :-)
 
Old 12-21-2006, 01:38 AM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
I already knew what sundialsvcs, but I have to congratulate you anyway for such a fine explanation!

Maybe it's not a lot but I just want to say that it show a lot of dedication and its good to see people like that

Cheers!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
array of struct ChimpFace9000 Programming 7 12-20-2017 11:46 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration