manipulating "arg" - function(char* arg) without returning it[C,freebsd7.1,gcc]

Discussion in 'C' started by mali2, Jun 7, 2009.

  1. mali2

    mali2 New Member

    Joined:
    Jun 7, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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...
    ----------------------------------------------------------------------------------------------------
    #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. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > backup=arg

    All you're doing here is setting backup equal to the POINTER to the argument. So strcmp(arg,backup) will ALWAYS be zero even if you've successfully changed the text.

    I don't think you should be trying to change the argv[] text; this is provided so you can know what options the user gave you at the command line. If you want to manipulate these, copy them to your own memory structures then you can do whatever you like with them.
     
  3. mali2

    mali2 New Member

    Joined:
    Jun 7, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    thanks for your reply
    yes actually this foo funciton is in another file; which is used as shared library. but purpose of my post is to manipulating the arg char*, in the function foo as i m not allowed to have any return value from function the only thing, that i can do is to pass the arg as a parameter in the function, as i m passing in my function.
    Note: I m intending to make a change in the same arg, which should be change after calling of foo function in main.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    If it's the arg char* that you want to manipulate then you have to pass in the address of the pointer; C is pass by value only, so when you want to modify the thing passed in then you have to pass its address in. Do you mean you want to change the pointer to the string or the string pointed to?

    What if the pointer passed in is NULL?

    Seems odd you aren't "allowed" to return a value from a function. Why is that? What is the function supposed to do? I'm sure the people preventing you returning a value won't want you to modify argv[] either; this doesn't make a lot of sense. So maybe if you can outline the purpose of the function that would clarify why you want to do what you want to.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice