Sponsored Content
Top Forums Programming C- trying to code a 'spare array'; 'enum' fauled. Post 302697905 by Corona688 on Friday 7th of September 2012 02:35:44 PM
Old 09-07-2012
I have no idea what you're even trying to do with that enum. Usually you'd make an enum like enum { TITLE1=3, TITLE2=5, TITLE3=99 }; so that when you use TITLE1 in your code it becomes the number 3, and so forth. Their values are fixed at compile time, just like a #define and such.

I think you mean 'sparse array', not 'spare array'.

The C language's features closely match the CPU's features, so complex data types like sparse arrays aren't directly supported. You can make them, though. Here's a quick-and-simple one which works as a simple list like you wanted. Not the most efficient if you want it to hold thousands of numbers, but for a few dozen, should be fine...

Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct pair {        int key, value;    } pair;

typedef struct sparse {
        int size, pos;
        pair *p;
}  sparse;

void sparse_free(sparse *s);
sparse *sparse_create(void);
int sparse_get(sparse *s, int key);
int sparse_set(sparse *s, int key, int value);

int main(void)
{
        int n;
        sparse *s=sparse_create();

        // Add a bunch of random elements to the sparse array
        // Keys will be numbers 0 through 7.  Values will be 0 through 10.
        for(n=0; n<10; n++)     sparse_set(s,rand()%8, n);
        // Find everything that was set.  Absent items will show as -1
        for(n=0; n<8; n++)      printf("key %d value %d\n", n, sparse_get(s, n));

        // Print the literal contents of the array raw
        printf("\nContents of sparse array\n");
        for(n=0; n<s->pos; n++)
                printf("I %d\tK %d\tV %d\n", n, s->p[n].key, s->p[n].value);

        sparse_free(s);
        return(0);
}

sparse *sparse_create(void)
{
        sparse *s=malloc(sizeof(sparse));
        if(s == NULL) return(NULL);

        s->size=16;     s->pos=0;
        s->p=malloc(sizeof(pair)*16);
        return(s);
}

int sparse_get(sparse *s, int key)
{
        int n;
        for(n=0; n<s->pos; n++) if(s->p[n].key == key) return(s->p[n].value);

        return(-1);
}

int sparse_set(sparse *s, int key, int value)
{
        int n;

        // Hunt for key.  If found, just update and return.
        for(n=0; n<s->pos; n++)
        if(s->p[n].key == key)
        {
                s->p[n].value=value;
                return(0);
        }

        // Add 16 more elements if full
        if(s->pos >= (s->size - 1))
        {
                pair *p=realloc(s->p, sizeof(pair) * (s->size+16));
                if(p == NULL) return(-1);
                s->size += 16;
                s->p=p;
        }

        // Add to end.
        s->pos++;
        s->p[s->pos].key=key;
        s->p[s->pos].value=value;
        return(0);
}

void sparse_free(sparse *s){ if(s) { free(s->p); free(s); } }

Code:
$ gcc sparse.c
$ ./a.out

key 0 value 0
key 1 value 8
key 2 value 6
key 3 value 3
key 4 value 7
key 5 value -1
key 6 value 1
key 7 value 5

Contents of sparse array
I 0     K 0     V 0
I 1     K 7     V 5
I 2     K 6     V 1
I 3     K 1     V 8
I 4     K 3     V 3
I 5     K 2     V 6
I 6     K 4     V 7

$


Last edited by Corona688; 09-07-2012 at 03:44 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
XkbKeyNumActions(3)						   XKB FUNCTIONS					       XkbKeyNumActions(3)

NAME
XkbKeyNumActions - Computes the number of actions associated with the key corresponding to keycode SYNOPSIS
int XkbKeyNumActions macro ( xkb, keycode ) XkbDescPtr xkb; KeyCode keycode; ARGUMENTS
- xkb Xkb description of interest - keycode keycode of interest DESCRIPTION
A key action defines the effect key presses and releases have on the internal state of the server. For example, the expected key action associated with pressing the Shift key is to set the Shift modifier. There is zero or one key action associated with each keysym bound to each key. Just as the entire list of key symbols for the keyboard mapping is held in the syms field of the client map, the entire list of key actions for the keyboard mapping is held in the acts array of the server map. The total size of acts is specified by size_acts, and the number of entries is specified by num_acts. The key_acts array, indexed by keycode, describes the actions associated with a key. The key_acts array has min_key_code unused entries at the start to allow direct indexing using a keycode. If a key_acts entry is zero, it means the key does not have any actions associated with it. If an entry is not zero, the entry represents an index into the acts field of the server map, much as the offset field of a KeySymMapRec structure is an index into the syms field of the client map. The reason the acts field is a linear list of XkbActions is to reduce the memory consumption associated with a keymap. Because Xkb allows individual keys to have multiple shift levels and a different number of groups per key, a single two-dimensional array of KeySyms would potentially be very large and sparse. Instead, Xkb provides a small two-dimensional array of XkbActions for each key. To store all of these individual arrays, Xkb concatenates each array together in the acts field of the server map. The key action structures consist only of fields of type char or unsigned char. This is done to optimize data transfer when the server sends bytes over the wire. If the fields are anything but bytes, the server has to sift through all of the actions and swap any nonbyte fields. Because they consist of nothing but bytes, it can just copy them out. XkbKeyNumActions computes the number of actions associated with the key corresponding to keycode. This should be the same value as the result of XkbKeyNumSyms. STRUCTURES
The KeySymMapRec structure is defined as follows: #define XkbNumKbdGroups 4 #define XkbMaxKbdGroup (XkbNumKbdGroups-1) typedef struct { /* map to keysyms for a single keycode */ unsigned char kt_index[XkbNumKbdGroups]; /* key type index for each group */ unsigned char group_info; /* # of groups and out of range group handling */ unsigned char width; /* max # of shift levels for key */ unsigned short offset; /* index to keysym table in syms array */ } XkbSymMapRec, *XkbSymMapPtr; SEE ALSO
XkbKeyNumSyms(3) X Version 11 libX11 1.2.1 XkbKeyNumActions(3)
All times are GMT -4. The time now is 03:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy