Thread: Having trouble passing an array of pointer to a function

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

    Having trouble passing an array of pointer to a function

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_CMD_LINE 500
    
    
    void tokenize(char *cmd_ln, char *fun_tknzd[], int *argument_cnt);
    
    int  main(void)
    {
    
            
             char cmd_ln[MAX_CMD_LINE], *str_tknzd[MAX_CMD_LINE];
             int argument_cnt = 0;
          
             //I think my problem is here
             tokenize(cmd_ln, &str_tknzd[MAX_CMD_LINE], &argument_cnt);
    
              /* I am getting a null here i am not sure why, but i think it has something to do with how I am passing the array to the function. It does however print fine inside of the function */
              printf("Out of function %s\n", str_tknzd[0]);
    
              return 0;
    }
    
    
    void tokenize(char *cmd_ln, char *fun_tknzd[], int *argument_cnt)
    {
        int i = 0;
    
    
        fun_tknzd[0] = strtok (cmd_ln," ");
    
        //Just testing to see if the strtok is working correctly
        printf("Test: %s\n", fun_tknzd[0]);
        argument_cnt++;
        printf("Argument count: %d\n", *argument_cnt);
    
    
        while (fun_tknzd[i] != NULL){
    
            /*Just testing to see if the strtok is working correctly and I enter the loop*/
            printf("Entered Loop: %d\n", i);
            printf("Got %s\n", fun_tknzd[i]);
            i++;
    
    
            fun_tknzd[i] = strtok (NULL, " ");
            argument_cnt++;
        }
    I am trying to pass the value of fun_tknzd to str_tknzd

    Thanks in advance for the help
    Last edited by DeeMan; 09-07-2013 at 05:13 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    All you need to do is pass the name 'str_tknzd'. Why are you trying to pass the address? str_tknzd is already defined as an array of pointers to char.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Thank you that was awesome how fast you saw that. I hope you have a great day!!!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by nonpuz View Post
    All you need to do is pass the name 'str_tknzd'. Why are you trying to pass the address? str_tknzd is already defined as an array of pointers to char.
    well you can pass address, but address of the first element &str_tknzd[0] which is what the str_tknzd is converted to anyway after it is passed to the function

    using &str_tknzd[MAX_CMD_LINE] you get address of the element next to the last in the array - so it is actually out of bounds access to the allocated memory.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Thank you for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing two dimensional array to function using pointer
    By Burns111 in forum C Programming
    Replies: 3
    Last Post: 11-27-2012, 10:55 AM
  2. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  3. Trouble passing char/pointer array
    By -Prime- in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2007, 03:38 AM
  4. Replies: 4
    Last Post: 06-15-2005, 08:30 PM
  5. Passing pointer to array to a function
    By Tojam in forum C Programming
    Replies: 1
    Last Post: 10-09-2002, 09:24 PM