1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

[c debian linux] Please suggest the function definition

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

  1. gamodg

    gamodg New Member

    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

    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

    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

    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

    Hi
    Thanks for the reply !!!!

     
  6. xpi0t0s

    xpi0t0s Mentor

    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