LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 09-15-2005, 06:48 PM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Rep: Reputation: 45
array of unions (c code)


Hi i have declared an array of unions like that
union qdisc qdisc_stats[10];


I am trying to access each element separately like that
qdisc_stats[0].htb.parameter (htb is a struct )

but this returns me the eroor

parse.c:126: error: subscripted value is neither array nor pointer
 
Old 09-15-2005, 06:58 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Can you show your union definition?
 
Old 09-15-2005, 07:09 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
This works for me:
Code:
typedef struct htb
{
        char *parameter;
} htb;

union qdisc {
        int i;
        struct htb htb;
}
qdisc_stats[10];

main()
{
        qdisc_stats[0].htb.parameter="foo";
        printf("%s %d\n", qdisc_stats[0].htb.parameter, qdisc_stats[0].i);
}
 
Old 09-15-2005, 07:47 PM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
Thx i have fixed it
Now i have something else

union qdisc qdisc_stats[10];

I want to pass the following union as a parameter to a function


void read_diff_parameters(struct diff_configuration *diff,union qdisc *qdisc){


the function is called
read_diff_parameters(&diff_configuration,&qdisc_stats);


this prints the following warning
3threads.c:47: warning: passing arg 2 of `read_diff_parameters' from incompatible pointer type
 
Old 09-15-2005, 08:12 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
try &qdisc_stats[0]
 
Old 09-16-2005, 12:10 PM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Whoops - I accidentally posted "reply" to the wrong thread.

The (related!) thread I meant to reply to is here:

http://www.linuxquestions.org/questi...hreadid=363900

Last edited by paulsm4; 09-16-2005 at 12:27 PM.
 
Old 09-16-2005, 12:43 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Quote:
Please consider at least *borrowing* a copy of K&R from a library until I can get you a copy. Please?
well, you "unposted" just in time I was about to answer to that ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 unions in c alaios Programming 3 09-19-2005 10:02 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 02:37 AM
structres and unions problem linux_lover2005 Programming 2 04-15-2005 09:31 AM
php session array : What's wrong in this code vikaspa Programming 2 03-30-2004 09:37 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM


All times are GMT -5. The time now is 11:55 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration