Array having Integer and String elements

This is a discussion on Array having Integer and String elements within the C++ Programming forums, part of the General Programming Boards category; Hi Can anybody help me to decide the datastructure I should use for defining an array Of elements which can ...

  1. #1
    skm
    skm is offline
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    Array having Integer and String elements

    Hi
    Can anybody help me to decide the datastructure I should use for defining an array Of elements which can be Integer as well as string.
    since I want to put GID (Group ID available in LInux System) in an array.
    elements can be

    grp1
    grp2
    123
    1097
    1397
    grp4
    grp5
    3790

    etc..

    ..skm

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Well you cannot have an array consisting of elements of two different types. You have to make a decision since you have several options. They may all be stored as strings and converted to numeric (if necessary) when accessed or if you insist on having them remain in numeric form, store them in two different arrays. It's either that or create a data type that contains a string and an int and use that as the array, but that would be an horrid thing.

  3. #3
    skm
    skm is offline
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Please let me know the BEst way to store all elements as strings and convert them to numeric.
    Right now I am assuming that the Input is provided as Numeric .
    The code is as follows :Plz let me know the changes that i can do.

    //const char *gidlist : comma separated list of gids. ( argument)
    // snapshot starts
    char *token=NULL;
    int gidint[5];
    int i=0, count=0;
    int is_member=0;

    // code to read the const char* and put elem in integer array
    token=strtok((char *)gidlist,",");
    while (token != NULL) {
    gidint[i]=atoi(token);
    token=strtok(NULL,",");
    i++;
    }

    // code for further manipulations
    // int Is_Running (int) ( function to return flag)
    count=i;
    for (i=0;i<count;i++) {
    if (Is_Running(gidint[i])) {
    printf ("GroupMember found %d",gidint[i]);
    is_member = 1; break;
    }
    }
    //snapshot ends

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    you should check if the string contains non-digit characters before doing atoi()....i believe atoi("grp2") returns (int) 2.

    i also believe that a named group also has a numerical ID..i personally, would look up the group ID programatically with linux, and store it in nID...if the numerical ID was entered. set lpszID to NULL...
    struct{
    int nID;
    char *lpszID[MAX_GID_LENGTH];
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    skm
    skm is offline
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Thanx for the quick reply.
    I have used a struct as follows :
    struct ID {
    int gid;
    char *groupname;
    } id;

    now the parameter that is received is const char* gidlist

    token=strtok((char *)gidlist,",");
    while (token != NULL) {
    tmp=atoi(token);
    if (isdigit(tmp)) {
    id.gid=tmp;
    id.groupname=NULL;
    printf("digit %d\n",id.gid);
    }
    else {
    id.groupname=token;
    printf("Nondigit char %s\n",id.groupname);

    }

    What is going wrong here, isdigit() doesn't seem to work..if I don't use atoi(), I get a segmentation fault error.

  6. #6
    skm
    skm is offline
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    I have used the following code for 'test for nondigits' and it works nicely.
    now I just wanted to confirm if this coding(so far what i have done) is the most appropriate or some better Implementation can be done.
    anyways.....Thanx for all uer answers. :-)



    int test_for_nondigits( const char *s ) {
    int nd = 0;
    size_t len;
    const char *p;

    len = strlen( s );
    if( len == 0 )
    return -1;
    p = s;
    while( *p ) { if( !isdigit( *p ) ) nd++; p++; } return nd;
    }

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Woop?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i'm thinking that a valid group ID name must start with an alphabetical character or an underscore..if this is true you can simply check the first character....

    (impromptu code..not tested)
    Code:
    struct ID
    {
       int nID;
       char lpszID[10];
    }id;
    
    char token[400] = NULL;
    token = strtok(gidlist, ",");
    while(token)
    {
    	if( isdigit(*token) )
    	{
    		id.nID = atoi(token);
    		id.lpszID = NULL;
    	}else{
    			             //fake function below 	
    		id.nID = 0; // nID = lookUpLinuxGroupIdNumber(token)
    		strcpy(&id.lpszID, token); 
    	}
    	printf("Group Name: %s; Group ID: %d\n", id.lpszID, id.nID);
    	token = strtok(NULL, ",");
    }
    there is probably some error checking needed to be done here...but it depends on what you're going to use this data for and the exact specs of group ID's and numbers.

    by the way...use code tags when posting code
    Last edited by misplaced; 12-16-2004 at 02:47 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. String To Integer Array!!!!
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-29-2001, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21