![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 3
| manipulating arg in the: function(char* arg); without returning it[C,freebsd7.1,gcc] I have a program source code, main.c . In which i m intending to do following thing with out changing the source code... ---------------------------------------------------------------------------------------------------- Code: #include <stdio.h>
int main(int argc,char **argv)
{
char* backup;
char* arg = argv && argc > 0 ? argv[1] : NULL;
backup=arg;
foo(arg);
if (strcmp(arg, backup) != 0)
{
printf("Yeah! u success; as u have manipulated arg");
}
else
printf("You Failed!; no change in arg; arg and backup both are same");
exit(0);
}
void foo(char *arg)
{
arg="what can i do in it :( ??????? " ;
}
I am beginner in C, I want to do some thing in this funtion so i would be able to change the arg, without returning arg from this method, as i m not supposed to change in the main method, i can only do things in foo function. waiting for ur suggestions.. thanks for your concentration. I m using freebsd7.1 and compiler is gcc. |
| mali2 is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| I doubt you can do that. arg and backup point to the same bit of memory. You have no way to modify arg inside foo(), so you can't make it point to something else. Maybe you have misunderstood the task? -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 | |
| Registered User Join Date: Jun 2009
Posts: 3
| Quote:
Code: static void* function(char* arg){
fprintf(stderr, "int 3h\n");
printf("You've made it passed the first obstacle!\n");
printf("-----\nStep 2. Case of the missing dynlib\n");
char* backup = strdup(arg);
void* handle = dlopen("mylib.so", RTLD_NOW);
voidfun hsym = dlsym(handle, "NULL");
hsym(arg);
if (strcmp(arg, backup) != 0) cake(arg);
}
that method which i have described above is supposed to be done in the shared library which i have created.... | |
| mali2 is offline | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| If you pass a char * to a function in C, you can not change it to point somewhere else. I'm not sure what you are supposed to do, but it's either breaking some rules, or you have misunderstood what you are supposed to do. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #5 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| If you know about the details of the implementation, you could theoretically alter the value of arg in main() by determining its location in relation to the copy of arg being passed into it (ie. calculations of the stack, but assuming a heck of a lot of things, most of which can't really be determined easily). This isn't really in the domain of standard C, though, as this requires one to deviate into, at the very least, a murky area of implementation defined behavior, if not outright undefined behavior. To make sure there is no mistake, I will state again that this task, as I understand it, is impossible in standard C. I don't even understand its purpose.
__________________ |
| MacGyver is offline | |
| | #6 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > char* arg = argv && argc > 0 ? argv[1] : NULL; Your strcmp() will blow up anyway if arg ends up being NULL. As would your modified strdup() variant. Why does this look like an exercise in stack smashing to get the code to do something non-obvious based on it's intention.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #7 | ||
| Registered User Join Date: Jun 2009
Posts: 3
| Quote:
Quote:
| ||
| mali2 is offline | |
| | #8 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #9 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Only way a variable can be altered in the calling function is by passing its address as an argument to the callee. |
| itCbitC is offline | |
| | #10 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| . . . for example: Code: char *rating = "Good";
modify_rating(&rating);
void modify_rating(char **rating) {
*rating = "Bad";
}
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #11 |
| Registered User Join Date: Apr 2006
Posts: 1,193
| Perhalps the code is ment to be Code: backup= malloc(strlen(arg)+1); strcpy(backup, arg); Code: backup=arg;
__________________ It is too clear and so it is hard to see. A dunce once searched for fire with a lighted lantern. Had he known what fire was, He could have cooked his rice much sooner. |
| King Mir is online now | |
![]() |
| Tags |
| function(char* arg), manipulating arg, [c freebsd7.1 gcc] |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Ranged numbers | Desolation | Game Programming | 8 | 07-25-2006 10:02 PM |