Sponsored Content
Top Forums Programming C- trying to code a 'spare array'; 'enum' fauled. Post 302697941 by alex_5161 on Friday 7th of September 2012 03:39:10 PM
Old 09-07-2012
Corona688 -
Thanks for replay. Not what I have asked, but appreciate attention!

'Spare'=='Sparse', thank for correction and sorry for misspelling.
Regarding the >>enum E_TYPE {1,4,2};<< I actually mean to have an ability to define an association like: typedef enum E_TYPE {1=0,4=1,2=2} E_TP; (Agree, looks ugly, but why not?)
So use it like " int a = (E_TP)4;" and get a==1.
Would be usefull Smilie

Unfortunately, your code is not appliable to my intention.

First:
to have an element the loop should be used: I would like to avoid run a loop on any info-by-key request!
Whith loop my task could be done absolutely simple:
Code:
  struct { int key,val; }INF[] = {{10,5},{20,4},{40,8}};
  int get_val(int key) {
    int i;
    for(i; i<(sizeof(INF)/sizeof(INF[0]);i++) 
       if (INF[i].key==key) return INF[i].val;
  }

That it! No any smart structure with the conditions static members, no any extra complication with pointing to internal member-array, no any dynamic allocations.
(BTW, in your example it is completely unneeded: assign values in declaration and define 'p' as sized array, if you use '16' in function.)

Second: My 'big point' is to define a data construction and avoid as possible any run-time functionality to get a value (except some initialization on beginning)


I'l try to explain again:

I have in a header file an INF array of structures with two integers .
I would like to construct a way to access second value by the first one.
And I'd like to do it WITHOUT any loop through whole INF array!

Also, I going to be able to make changes in the header file without touching the source code when I need add, change or remove a pair in the INF array.

If it would be not integer 'key', I would implement it with 'enum', having a reference-array, that by enumerated values (by default - the same as an array is indexed) would return the INF array index for member with 'key'-label (the enumeration element). By that I would heve the val-value!
That array would be initialized on beginning by one time loop through the INF array and assigning the INF index to the REF-array element with the index==enumerated value of the first INF int: key!
(Hope, now I have better explained what I am looking for?)

Last edited by alex_5161; 09-07-2012 at 04:52 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Vfstab on spare disk - HOW ? Mount ?

Hi, guys ! Could someone clarify one thing for me: I start machine from disk0, and want to check the /etc/vfstab on disk1. How do i do it ? Tried to write: cd / mount /dev/dsk/c0t1d0s0 /mnt But if I do cd /mnt, it is empty. I expected to see disk1 there ? Or am I wrong ? How do I... (3 Replies)
Discussion started by: DGoubine
3 Replies

2. Programming

what is the base type of enum

helo i have asked in exam what that what is the base type of enum options are given bewlo (1) long int (2) short int (3) signed int (4) unsigned int can u tell me what is the exact answer from the above option Regards, Amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

3. IP Networking

DNS ENUM RR interpretation

Hi Guys, This is really really urgent. Am looking out for some quick answers. I'm developing a DNS Resolver client that interprets DNS Query repsonses & pass on the needful to DNS applications. When an ENUM query(modified to an nslookup naptr query) is issued & an NAPTR RR(Resource Record)... (1 Reply)
Discussion started by: smanu
1 Replies

4. Programming

enum in c++

#include <iostream> #include <stdio.h> using namespace std; typedef struct A { enum a{ red,blue,green}a; }obj11; obj11 obj1; int main() { //obj1.a=red; // how to set variable ? cout<<"sizeof struct is n"<<sizeof(obj1); cout<<"obj1.a is"<<obj1.a; if... (1 Reply)
Discussion started by: crackthehit007
1 Replies

5. Solaris

Hot Spare pool

One more query in SVM :) Now with hot spare spool... I can understand adding/replacing a slice in particular hot spare pool with "-a / -r" option (or) adding a slice to all existing hot spare pool with "-all" option. Here my query is for deleting, we have only option "-d". 1) If the hot... (2 Replies)
Discussion started by: gowthamakanthan
2 Replies

6. Solaris

Hot Spare replacement

Hi Guys, Can Someone pls let me know the thorough process for Hot spare replacement as current Hot spare slice has broken down . :mad: Thanks ---------- Post updated at 06:34 PM ---------- Previous update was at 05:21 PM ---------- Update : Its a solaris 10 box (1 Reply)
Discussion started by: Solarister
1 Replies

7. Programming

enum and C preprocessor

Say I have a list of enumerations I wish to use to select a variable at compile-time: enum pins { PIN_A=1, PIN_B=7, PIN_C=6, } int VAR1, VAR2, VAR3, VAR4, VAR5, VAR6, VAR7; #define PIN_TO_VAR(NUM) VAR ## NUM int main(void) { PIN_TO_VAR(PIN_A)=32;... (2 Replies)
Discussion started by: Corona688
2 Replies

8. Solaris

How to get spare disks working

Dears how can i make this spare disks working online to replace a defective disks vxdisk list DEVICE TYPE DISK GROUP STATUS c0t10d0s2 sliced - - error c0t11d0s2 sliced disk08 rootdg online c1t16d0s2 sliced ... (3 Replies)
Discussion started by: thecobra151
3 Replies

9. Programming

Mixed enum types - coverity defect

Hi All, I came across this error "MIXING ENUM TYPES" when I run my C program against the Coverity Tool. I've made many search relating to the error, but I didnt find the exact solution. Can anyone help me to overcome this.? Thanks in Advance.!! (3 Replies)
Discussion started by: Parameswaran
3 Replies

10. Solaris

How to determine if i have spare disks in Solaris?

Hi Guys, obviously new to SOLARIS SUN SPARC 5.10 I would really appreciate if you help me see how to find free disks available in my system. Like i am a linux admin. If i want to grow a file system in linux. I would first have a look at my volume groups to see if they have free PEs if not then... (2 Replies)
Discussion started by: aiqbal
2 Replies
thai/thinp.h(3) 						      libthai							   thai/thinp.h(3)

NAME
thai/thinp.h - Thai string input sequence filtering. SYNOPSIS
Data Structures struct thinpconv_t Input sequence correction info. Enumerations enum thstrict_t { ISC_PASSTHROUGH = 0, ISC_BASICCHECK = 1, ISC_STRICT = 2 } Strictness of input sequence checking, according to WTT 2.0. Functions int th_isaccept (thchar_t c1, thchar_t c2, thstrict_t s) Check for acceptance of input sequence. int th_validate (struct thcell_t context, thchar_t c, struct thinpconv_t *conv) Check and try to correct input sequence. Detailed Description Thai string input sequence filtering. Enumeration Type Documentation enum thstrict_t Strictness of input sequence checking, according to WTT 2.0. Enumerator ISC_PASSTHROUGH No check ISC_BASICCHECK Basic check ISC_STRICT Strict check Function Documentation int th_isaccept (thchar_tc1, thchar_tc2, thstrict_ts) Check for acceptance of input sequence. Parameters: c1 : previous character c2 : the newly input character s : strictness level Returns: non-zero if accepted, 0 otherwise Checks if, according to WTT 2.0 strictness level s, c2 is allowed after c1. int th_validate (struct thcell_tcontext, thchar_tc, struct thinpconv_t *conv) Check and try to correct input sequence. Parameters: context : previous cell c : the newly input character conv : the storage for resulting correction info Returns: 0 if the input is to be rejected non-zero otherwise. Given the previous cell as context, edit the input buffer using the given input c, maintaining WTT canonical order, and do some convenient correction in conv. Author Generated automatically by Doxygen for libthai from the source code. Version 0.1.14 Tue Jun 17 2014 thai/thinp.h(3)
All times are GMT -4. The time now is 10:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy