Thread: Having trouble passing a value from a function

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    17

    Having trouble passing a value from a function

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_CMD_LINE 500
    
    
    void chk_for_redirect(char *fun_tknzd,  char *opt_rdrct_2, char *inpt_rdrct_2);
    
    
    int main(void)
    {
    
    
            char *str_tknzd[MAX_CMD_LINE], *opt_rdrct = NULL, *inpt_rdrct = NULL, test[] = ">tester";
    
            str_tknzd[1] = test;
    
            // this function is returning NULL for opt_rdrct and I am not sure why
            // I think my problem is here
            chk_for_redirect(str_tknzd[1], opt_rdrct, inpt_rdrct);
    
            // this is how I know it is returning NULL
            printf("Output redirect out of function: %s\n", opt_rdrct);
            printf("Input redirect out of function: %s\n", inpt_rdrct);
    
    
             return 0;
    
    
    }
    
    
    void chk_for_redirect(char *fun_tknzd,  char *opt_rdrct_2, char *inpt_rdrct_2)
    {
               if(strchr (fun_tknzd, '>') == fun_tknzd){
                  opt_rdrct_2 = strchr (fun_tknzd, '>');
                  printf("Output redirect: %s\n", opt_rdrct_2);
                }
    
                else if(strchr (fun_tknzd, '<') == fun_tknzd){
                    inpt_rdrct_2 = strchr (fun_tknzd, '<');
                    printf("Input redirect: %s\n", inpt_rdrct_2);
                }
    
    
    }
    The value that opt_rdrct_2 & inpt_rdrct_2 point to are not being passed to opt_rdrct and inpt_rdrct.

    Thanks in advance for the help
    Last edited by DeeMan; 09-08-2013 at 06:34 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Please, indent your code.

    I also inform that you do not need to append _2 to the name of the argument. Arguments can have their very own names and it is ok to have the same name as another variable in main().

    You have a char pointer and you want to modify it. How do you expect it to be modified, if you pass a char pointer? It's - pretty much - like the swap example (which I think you have been taught) . You should pass a pointer to what you want to modify. So you should pass a pointer to a char pointer, which is a double char pointer.

    Moreover, notice that only of your arguments are going to be set to something different than NULL, because of the if-else statement.
    Notice also, that actually, non of them might be modified, because the condition at the if and if else statements may result to false.

    EDIT:

    Code:
    str_tknzd[1] = test;
    You think that copies strings? No, strcpy does.
    Last edited by std10093; 09-08-2013 at 06:34 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Sorry about the indentation. I was aware that the arguments can be the same, I just like to name them differently. I also was aware of the if else statement and that one of the arguments will be set to NULL. However in the above example I passed it a value. I set, str_tknzd[1] = test( this is an edit, I missed, "You think that copies strings? No, strcpy does.", I thought that it sets the pointer to the address of test.) This means that opt_rdrct_2 should be equal to test. As to your statement, "You should pass a pointer to what you want to modify. So you should pass a pointer to a char pointer, which is a double char pointer." I don't understand what you are saying. I have tried to pass the function the the pointers. I am afraid I am missing something you are trying to convey to me. Can you clarify? Thank you for your help.

    I also checked your website for some additional information on passing pointers to function, but was unable to locate any material on the subject.
    Last edited by DeeMan; 09-08-2013 at 07:31 PM.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Thank you so much I see what you were saying, I had to make the function to receive a double pointer. Thanks for all of you support. Also thank you for the website.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-08-2013, 06:17 PM
  2. trouble passing parameter to function
    By cover2 in forum C Programming
    Replies: 3
    Last Post: 12-14-2012, 03:01 PM
  3. Replies: 1
    Last Post: 09-04-2007, 10:00 PM
  4. Replies: 6
    Last Post: 06-01-2007, 07:02 PM
  5. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM