update chararray values in function argument

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

update chararray values in function argument

 
0
  #1
Jun 15th, 2009
Hi
I'm having a very basic newbie problem,
The compiler forces me to do a strdup (because of cons correctness),
can I void the extra line in my code, and then I cant seem to update my array
after the funciton call

  1. void fix_name(char *name){
  2. if(name[strlen(name)-1]!='/'){
  3. char *tmp = new char[(strlen(name)+1)];
  4. strcpy(tmp,name);
  5. tmp[strlen(name)] = '/';
  6. printf("%s\n",tmp);
  7. name=tmp;
  8. }
  9. }
  10.  
  11. int main(int argc, char** argv){
  12. const char* sds = "asf/asdf";
  13. const char* sdsd =strdup(sds)
  14. printf("%s\n",sdsd);
  15. (fix_name(sdsd));
  16. printf("%s\n",sdsd);
  17. return 0;
  18. }
Last edited by monkey_king; Jun 15th, 2009 at 10:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: update chararray values in function argument

 
0
  #2
Jun 16th, 2009
you could do this: Also, its not necessary to surround function calls with parentheses.
int main(int argc, char** argv){
  char sdsd[] = "asf/asdf";
  printf("%s\n",sdsd);
  fix_name(sdsd);
  printf("%s\n",sdsd);
  return 0;
}
Last edited by Ancient Dragon; Jun 16th, 2009 at 1:23 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: update chararray values in function argument

 
0
  #3
Jun 16th, 2009
Apropos, your fix_name is wrong. It does nothing (except memory leak).

You modify dynamically allocated copy of the string argument then assign the pointer to the parameter (but not to the argument) - that's all. Now you have a memory leak (tmp is not deallocated) and the call argument is not changed...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: update chararray values in function argument

 
0
  #4
Jun 16th, 2009
Hmm thanks for the char[] approach.

But I cant nail down my problem with the updated cstring.
The following code

  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. void fix_name(char *name){
  5. printf("pointer in function arg: %p\n",name);
  6. if(name[strlen(name)-1]!='/'){
  7. char *tmp = new char[(strlen(name)+1)];
  8. strcpy(tmp,name);
  9. tmp[strlen(name)] = '/';
  10. printf("content of new cstring: %s\n",tmp);
  11. printf("pointer of new cstring %p\n",tmp);
  12. name=tmp;
  13. printf("pointer of function arg: %p\n",name);
  14. }
  15. }
  16.  
  17. int main(int argc, char** argv){
  18. char sdsd[] = "asf/asdf";
  19.  
  20. printf("%s\n",sdsd);
  21. fix_name(sdsd);
  22. printf("%s\n",sdsd);
  23. return 0;
  24. }
which gives rise to
  1. ./a.out
  2. asf/asdf
  3. pointer in function arg: 0x7fff0ef7b5e0
  4. content of new cstring: asf/asdf/
  5. pointer of new cstring 0x23f4010
  6. pointer of function arg: 0x23f4010
  7. asf/asdf
Do I need to give my function that should update my cstring a **char pointer, instead of a *char pointer?

thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: update chararray values in function argument

 
0
  #5
Jun 16th, 2009
you need to actually dynamically allocate the orginal memory
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. void fix_name(char **name){
  5. printf("pointer in function arg: %p\n",name);
  6. int len = strlen(*name);
  7. if((*name)[len-1]!='/'){
  8. char *tmp = new char[len+2];
  9. strcpy(tmp,*name);
  10. strcat(tmp,"/");
  11. printf("content of new cstring: %s\n",tmp);
  12. printf("pointer of new cstring %p\n",tmp);
  13. delete[] *name;
  14. *name=tmp;
  15. printf("pointer of function arg: %p\n",*name);
  16. }
  17. }
  18.  
  19. int main(int argc, char** argv){
  20. char* sdsd = new char[11];
  21. strcpy(sdsd,"asf/asdf");
  22.  
  23. printf("%s\n",sdsd);
  24. fix_name(&sdsd);
  25. printf("%s\n",sdsd);
  26. delete[] sdsd;
  27. return 0;
  28. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: update chararray values in function argument

 
0
  #6
Jun 16th, 2009
What for this overcomplicated unclear code? What's a programming case where this nightmare required?

This thread was started from a very suspicious-looking remark The compiler forces me to do a strdup (because of cons correctness) and now we have one of the most strange snippet I have ever seen...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: update chararray values in function argument

 
0
  #7
Jun 16th, 2009
Thanks ancient dragon,
the trick was passing a pointer pointer then.

@arkm

The programming problem is simple.
check if a char* contains a trailing slash,
otherwise update the char*, so that it does.

I'm all ears for a more simple version,not using std::string
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: update chararray values in function argument

 
0
  #8
Jun 16th, 2009
Originally Posted by monkey_king View Post
Thanks ancient dragon,
the trick was passing a pointer pointer then.
No -- read the code I posted carefully because there are other changes.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: update chararray values in function argument

 
0
  #9
Jun 16th, 2009
Yes,
you've added 'delete []', and have to dereference the double pointers.

Thanks for your help again ancient dragon.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC