943,600 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 984
  • C++ RSS
Jun 15th, 2009
0

update chararray values in function argument

Expand Post »
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

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jun 16th, 2009
0

Re: update chararray values in function argument

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jun 16th, 2009
0

Re: update chararray values in function argument

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 16th, 2009
0

Re: update chararray values in function argument

Hmm thanks for the char[] approach.

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

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jun 16th, 2009
0

Re: update chararray values in function argument

you need to actually dynamically allocate the orginal memory
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jun 16th, 2009
0

Re: update chararray values in function argument

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 16th, 2009
0

Re: update chararray values in function argument

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
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jun 16th, 2009
0

Re: update chararray values in function argument

Thanks ancient dragon,
the trick was passing a pointer pointer then.
No -- read the code I posted carefully because there are other changes.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jun 16th, 2009
0

Re: update chararray values in function argument

Yes,
you've added 'delete []', and have to dereference the double pointers.

Thanks for your help again ancient dragon.
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008

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: Sum of vowels
Next Thread in C++ Forum Timeline: How to display password in asteriks in c++ ?





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


Follow us on Twitter


© 2011 DaniWeb® LLC