C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-07-2009, 05:28 AM   #1
Registered User
 
Join Date: Jun 2009
Posts: 3
manipulating arg in the: function(char* arg); without returning it[C,freebsd7.1,gcc]

As the title of the topic is not so clear, now here i can clear my problem...

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   Reply With Quote
Old 06-07-2009, 05:32 AM   #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   Reply With Quote
Old 06-07-2009, 05:44 AM   #3
Registered User
 
Join Date: Jun 2009
Posts: 3
Quote:
Originally Posted by matsp View Post
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
Thanks for your reply - my actual code is this... but it is assumes to be do in this way....

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);    
}
Now i want to make this condition true , so cake function will parsed by compiler...
that method which i have described above is supposed to be done in the shared library which i have created....
mali2 is offline   Reply With Quote
Old 06-07-2009, 05:55 AM   #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   Reply With Quote
Old 06-07-2009, 06:01 AM   #5
Deathray Engineer
 
MacGyver's Avatar
 
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   Reply With Quote
Old 06-07-2009, 06:13 AM   #6
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 06-07-2009, 06:28 AM   #7
Registered User
 
Join Date: Jun 2009
Posts: 3
Quote:
Originally Posted by matsp View Post
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
Quote:
Originally Posted by MacGyver View Post
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.
hmm... the purpose: "It is my assignment given by the prof in my university", I am sure there is no mistake...the above hsym(arg) got the reference from dynamic shared lib, and my purpose to doing it is to make the if condition true, so cake function will parsed.
mali2 is offline   Reply With Quote
Old 06-07-2009, 09:04 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by mali2
the purpose: "It is my assignment given by the prof in my university", I am sure there is no mistake.
Maybe you should provide some context: what module is this prof teaching?
__________________
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   Reply With Quote
Old 06-07-2009, 02:09 PM   #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   Reply With Quote
Old 06-08-2009, 01:53 PM   #10
Frequently Quite Prolix
 
dwks's Avatar
 
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   Reply With Quote
Old 06-08-2009, 02:42 PM   #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);
Instead of
Code:
backup=arg;
Then you can change the string passed to foo without effecting the backup variable. (Note: there is still the restriction that the new string must be shorter than the old one)
__________________
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   Reply With Quote
Reply

Tags
function(char* arg), manipulating arg, [c freebsd7.1 gcc]

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ranged numbers Desolation Game Programming 8 07-25-2006 10:02 PM


All times are GMT -6. The time now is 01:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22