Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   [c debian linux] Please suggest the function definition (http://www.go4expert.com/forums/c-debian-linux-suggest-function-t16625/)

gamodg 23Mar2009 00:23

[c debian linux] Please suggest the function definition
 
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

xpi0t0s 23Mar2009 00:49

Re: [c debian linux] Please suggest the function definition
 
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?

gamodg 23Mar2009 08:46

Re: [c debian linux] Please suggest the function definition
 
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 !!!

xpi0t0s 23Mar2009 22:53

Re: [c debian linux] Please suggest the function definition
 
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?

gamodg 24Mar2009 08:51

Re: [c debian linux] Please suggest the function definition
 
Hi
Thanks for the reply !!!!

Quote:

Originally Posted by xpi0t0s (Post 44740)
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.

xpi0t0s 24Mar2009 13:48

Re: [c debian linux] Please suggest the function definition
 
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?


All times are GMT +5.5. The time now is 23:16.