LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-15-2005, 05:48 PM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

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, 05:58 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Can you show your union definition?
 
Old 09-15-2005, 06:09 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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, 06:47 PM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

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, 07:12 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
try &qdisc_stats[0]
 
Old 09-16-2005, 11:10 AM   #6
paulsm4
LQ 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 11:27 AM.
 
Old 09-16-2005, 11:43 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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



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

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

All times are GMT -5. The time now is 04:49 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