943,685 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 430
  • C RSS
Mar 22nd, 2009
0

Suggest the formal arguments

Expand Post »
I have a structure
  1. struct Info {
  2. char *name;
  3. char **value;
  4. };
and a function
  1. void addValue(struct Info *info,char *value ,int posVal)
  2. {
  3. int size=strlen(value)+1;
  4. info->value[posVal]= (char *)malloc(size);
  5. strcpy(info->value[posVal],value);
  6. info->value[posVal+1]=NULL;
  7. }
  8.  
  9. Main
  10.  
  11. struct Info info[10]
  12. .......
  13. .......
  14.  
  15. initValArrSize(&info[0],1);
  16. /*
  17. Make size+1 single dimension arrays of size char *
  18. void initValArrSize(struct XMLInfo *info, int size )
  19. {
  20. info->value=(char **)malloc((size+1)*sizeof(char *));
  21. }
  22. */
now calling addvalue
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 . addValue(info[0].value[0],"Inspiron");

Please suggest the addValue definition to accommodate this change
Last edited by Ancient Dragon; Mar 23rd, 2009 at 12:33 am. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gamodg is offline Offline
15 posts
since Mar 2007
Mar 22nd, 2009
0

Re: Suggest the formal arguments

>Please suggest the addValue definition to accommodate this change
Looking at the way you call the function. The function definition should look like:

  1. void addValue(struct Info **info, char *value, int posVal);

So that's the function addValue which takes pointer-to-pointer of struct Info type, char pointer and an integer.

This would eliminate the requirement of returning the struct Info * from your addValue. If that makes sense!

NB: Please use the code tags. It makes it more readable and perhaps you can get more help.

-ssharish
Last edited by ssharish2005; Mar 22nd, 2009 at 4:44 pm.
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Mar 23rd, 2009
0

Re: Suggest the formal arguments

Thanks for the reply !!!
I am not returning anything from function addValue. I want to pass
  1. info[0].value[0]
by address. Please suggest the function signature.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gamodg is offline Offline
15 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Relatively Prime
Next Thread in C Forum Timeline: stadium coordinator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC