[c debian linux] Please suggest the function definition

Discussion in 'Debian' started by gamodg, Mar 22, 2009.

  1. gamodg

    gamodg New Member

    Joined:
    Mar 22, 2009
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I have a structure

    PHP:
    struct Info 
    char *name
    char **value
    }; 



    and a function


    PHP:
    void addValue(struct Info *info,char *value ,int posVal

    int size=strlen(value)+1
    info->value[posVal]= (char *)malloc(size); 
    strcpy(info->value[posVal],value); 
    info->value[posVal+1]=NULL



    Main



    PHP:
    struct Info info[10
    ....... 
    ....... [/
    LEFT]
     
    [
    LEFT]initValArrSize(&info[0],1); 
    /* 
    Make size+1 single dimension arrays of size char * 
    void initValArrSize(struct XMLInfo *info, int size ) 

    info->value=(char **)malloc((size+1)*sizeof(char *)); 

    */ 


    now calling addvalue


    PHP:
    addValue(&info[0],"Inspiron"0); 



    I want to change it to info[0].value[0] so that it reduces the number of arguments in addValue method and also pass it by reference so that changes are reflected in main
    ex .

    PHP:
    addValue(info[0].value[0],"Inspiron"); 


    Please suggest the addValue definition to accommodate this change
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    info[0].value[0] is of type char*, and so in "Inspiron", so I'd guess something like:
    Code:
    void addValue(char *info_value,char *value)
    
    > I want to change it to info[0].value[0] so that it reduces the number of arguments

    Why?
     
  3. gamodg

    gamodg New Member

    Joined:
    Mar 22, 2009
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the reply.

    I made the following changes.

    PHP:
    void addValue(char *info,char *value )
    {
            
    int size=strlen(value)+1;
            
    info= (char *)malloc(size);
            
    strcpy(info,value);

    When i try to run it the program terminates abruptly.
    Please help !!!
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    If you want to modify info[0].value[0], then you have to pass the address in and dereference that pointer within the function:
    Code:
    void addValue(char **info,char *value )
    {
            int size=strlen(value)+1;
            *info= (char *)malloc(size);
            strcpy(*info,value);
    }
    void test()
    {
            addValue(&(info[0].value[0]),"Inspiron");  
    }
    
    Again: why do you want to do this?
    
    Also, what about posVal?
     
  5. gamodg

    gamodg New Member

    Joined:
    Mar 22, 2009
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hi
    Thanks for the reply !!!!

     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Again: why do you want to do this? i.e. "so that it reduces the number of arguments"
    so the question is: Why do you want to reduce the number of arguments?

    What benefit do you think you will get from that?
    Have you done code profiling and found this to be a bottleneck of some kind?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice