| | |
update chararray values in function argument
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
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
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)
void fix_name(char *name){ if(name[strlen(name)-1]!='/'){ char *tmp = new char[(strlen(name)+1)]; strcpy(tmp,name); tmp[strlen(name)] = '/'; printf("%s\n",tmp); name=tmp; } } int main(int argc, char** argv){ const char* sds = "asf/asdf"; const char* sdsd =strdup(sds) printf("%s\n",sdsd); (fix_name(sdsd)); printf("%s\n",sdsd); return 0; }
Last edited by monkey_king; Jun 15th, 2009 at 10:48 pm.
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.
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...
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...
•
•
Join Date: Aug 2008
Posts: 149
Reputation:
Solved Threads: 8
Hmm thanks for the char[] approach.
But I cant nail down my problem with the updated cstring.
The following code
which gives rise to
Do I need to give my function that should update my cstring a **char pointer, instead of a *char pointer?
thanks in advance
But I cant nail down my problem with the updated cstring.
The following code
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstring> void fix_name(char *name){ printf("pointer in function arg: %p\n",name); if(name[strlen(name)-1]!='/'){ char *tmp = new char[(strlen(name)+1)]; strcpy(tmp,name); tmp[strlen(name)] = '/'; printf("content of new cstring: %s\n",tmp); printf("pointer of new cstring %p\n",tmp); name=tmp; printf("pointer of function arg: %p\n",name); } } int main(int argc, char** argv){ char sdsd[] = "asf/asdf"; printf("%s\n",sdsd); fix_name(sdsd); printf("%s\n",sdsd); return 0; }
C++ Syntax (Toggle Plain Text)
./a.out asf/asdf pointer in function arg: 0x7fff0ef7b5e0 content of new cstring: asf/asdf/ pointer of new cstring 0x23f4010 pointer of function arg: 0x23f4010 asf/asdf
thanks in advance
you need to actually dynamically allocate the orginal memory
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstring> void fix_name(char **name){ printf("pointer in function arg: %p\n",name); int len = strlen(*name); if((*name)[len-1]!='/'){ char *tmp = new char[len+2]; strcpy(tmp,*name); strcat(tmp,"/"); printf("content of new cstring: %s\n",tmp); printf("pointer of new cstring %p\n",tmp); delete[] *name; *name=tmp; printf("pointer of function arg: %p\n",*name); } } int main(int argc, char** argv){ char* sdsd = new char[11]; strcpy(sdsd,"asf/asdf"); printf("%s\n",sdsd); fix_name(&sdsd); printf("%s\n",sdsd); delete[] sdsd; return 0; }
![]() |
Similar Threads
- Problem with string to function argument (C++)
- Update the null values of a particular column (MS SQL)
- Homework help - Using function to create a triangle (C++)
- Expected primary-expression issue (C++)
- Returning Multiple Objects (C#)
- datagrid, innertext, javascript, database update (ASP.NET)
- changing values of a struct (C)
- Error C2660 Function does not take 1 argument (C++)
- Pointer to function as an argument (C++)
Other Threads in the C++ Forum
- Previous Thread: Sum of vowels
- Next Thread: How to display password in asteriks in c++ ?
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






