Thread: manipulating arg in the: function(char* arg); without returning it[C,freebsd7.1,gcc]

  1. #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.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #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....

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #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.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Only way a variable can be altered in the calling function is by passing its address as an argument to the callee.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    . . . 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, nort, etc.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM

Tags for this Thread