[c debian linux] Please suggest the function definition

gamodg's Avatar, Join Date: Mar 2009
Light Poster
I have a structure

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



and a function


PHP Code:
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 Code:
struct Info info[10
....... 
....... 
PHP Code:
 
[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 *)); 

*/ 
[/LEFT


now calling addvalue


PHP Code:
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 Code:
addValue(info[0].value[0],"Inspiron"); 


Please suggest the addValue definition to accommodate this change
0
xpi0t0s's Avatar, Join Date: Aug 2004
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?
0
gamodg's Avatar, Join Date: Mar 2009
Light Poster
Thanks for the reply.

I made the following changes.

PHP Code:
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 !!!
0
xpi0t0s's Avatar, Join Date: Aug 2004
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?
0
gamodg's Avatar, Join Date: Mar 2009
Light Poster
Hi
Thanks for the reply !!!!

Quote:
Originally Posted by xpi0t0s View Post
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?
I want to do this because I want to reduce the number of parameters passed to the function. Since it is a part of a large project so in furture remembering what the posVal or number passed means will be a problem.
Like passing info[0].value[0] so it becomes clear the value I am intending to pass.

Also, what about posVal?
Since value inside structure is a double dimension array so posVal tells which position the value to be inserted like info[0].value[1] or info[0].value[2] here value[1]/[2] tells where the value to be inserted.
0
xpi0t0s's Avatar, Join Date: Aug 2004
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?