Thread: Pass multiple strings to function C

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    84

    Pass multiple strings to function C

    I'm learning the basics and principles of C.
    I came now to pointers, strings and structs.
    Now I'm working on this code to pass arrays' content to functions.
    I have this code to pass content of different arrays to function.
    What I succeeded to accomplish is:

    1. How to pass one complete string because it's considered as one element of the array.
    2. How to pass array of char and ints.

    The issues I have now:

    1. How to pass arrays of multiple strings to functions.
    2. How to assign a pointer to the arrays to pass them also to functions.

    This is my code so far:
    There are commented parts to show the work I done in the code, I'm actually a beginner in C. I just feel that it's nice to leave arranged commented lines so the readers understand my issue more.


    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdint.h>
    //////////////////////////////////////////////////////////////
    ////////////COMPLETE MOST OF STRINGS ARRAY PRINT /////////////
    //////////////////////////////////////////////////////////////
        void print_array(char *arr,int8_t cnt);
        void print_array(char *arr,int8_t cnt)
        {
            int_fast8_t i;
            printf("Number of elements is: %d\n",cnt);
            for (i=0;i<cnt;i++)
            {
                printf("Elements of array: %s\n",arr);
            }
        }
        void print_len (char *arr,int8_t cnt);
        void print_len (char *arr,int8_t cnt)
        {
            char i,l=strlen(arr);
                for (i=0;i<cnt;i++)
            {
                printf ("%d\n",strlen(arr));
            }
        }
    
    int main()
    {
        char  array_1 [] = {1,2,3,4,5,6,7,8};
        char  array_2 [] = {'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G'};
        char *array_3 [] = {"1st","2nd","3rd","4th","5th","6th"};
        char *array_4 [] = {"Many of the designations used by manufacturers"};
        char *array_5 [] = {"mm","End of Multiple Strings Array","simple bluetooth connection",
        "datalogging purposes and accessing recorded data","THE OPERATING ENVIRONMENT"};
        //int8_t *array_pointer[3]=(char*){&array_1,&array_2,&array_3};
        int8_t cnt1 = sizeof(array_1)/sizeof(array_1[0]);
        int8_t cnt2 = sizeof(array_2)/sizeof(array_2[0]);
        int8_t cnt3 = sizeof(array_3)/sizeof(array_3[0]);
        int8_t cnt4 = sizeof(array_4)/sizeof(array_4[0]);
        int8_t cnt5 = sizeof(array_5)/sizeof(array_5[0]);
        int8_t len1,len2,len3,len4,len5,i,t=0,x=0;
        //print_len(*array_3,cnt3);
        //print_len(*array_5,cnt5);
        //printf("Number of chars int the string#%d is: %d\n",i,t);
    
        /*for (i=0;i<cnt5;i++)
        {
            printf ("%s\n",array_5[i]);
        }*/
    
        //print_array(array_pointer[0],cnt1);
        //print_array(array_1,cnt1);
        //print_array(array_2,cnt2);
        //print_array(*array_3,cnt3);
        //print_array(*array_4,cnt4);
        print_array(*array_5,cnt5);
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    How to pass arrays of multiple strings to functions.
    You just pass it to the function, like any other parameter. Remember though to change the declaration to a double pointer( "char **ar" ).

    How to assign a pointer to the arrays to pass them also to functions.
    You don't need to. The array is already a pointer to its first element.

    As a side-note, you seem to have misunderstood the usage of function declaration vs definition. Here's one of our admin's articles on that:
    What's the difference between declaring and defining in C and C++ - Cprogramming.com
    Devoted my life to programming...

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You are going to need separate functions for each variable type.

    Array_1 and array_2 are simple character arrays, not C strings. Array_3, array_4, and array_5 are arrays of C strings.

    Printing the actual content of the array in the way you want should not be too hard with printf(). Your biggest problem is the function prototypes. Look at this simple program for example:
    Code:
    #include <stdio.h>
    #include <stddef.h>
    
    
    void print_bytes(char arr[], size_t n);
    void print_c_strings(char *arr[], size_t n);
    
    
    int main(void)
    {
        char  array_1[] = {'A', 'B', 'C', 'D', 'E', 'F'};
        char  array_2[] = {1, 2, 3, 4, 5, 6, 7, 8};
        char *array_3[] = {"mm", "End of Multiple Strings Array",
            "simple bluetooth connection",
            "datalogging purposes and accessing recorded data",
            "THE OPERATING ENVIRONMENT"
        };
        
        puts("array_1:");
        print_bytes(array_1, sizeof array_1 / sizeof array_1[0]);
        puts("array_2:");
        print_bytes(array_2, sizeof array_2 / sizeof array_2[0]);
        puts("array_3:");
        print_c_strings(array_3, sizeof array_3 / sizeof array_3[0]);
        
        return 0;
    }
    
    
    void print_bytes(char arr[], size_t n)
    {
        size_t i;
        for (i = 0; i < n; ++i) {
            if (arr[i] >= 0 && arr[i] <= 9) {
                printf("\t%d\n", arr[i]);
            }
            else {
                printf("\t%c\n", arr[i]);
            }
        }
    }
    
    
    void print_c_strings(char *arr[], size_t n) 
    {
        size_t i;
        for (i = 0; i < n; ++i) {
            printf("\t%s\n", arr[i]);
        }
    }
    
    array_1:
        A
        B
        C
        D
        E
        F
    array_2:
        1
        2
        3
        4
        5
        6
        7
        8
    array_3:
        mm
        End of Multiple Strings Array
        simple bluetooth connection
        datalogging purposes and accessing recorded data
        THE OPERATING ENVIRONMENT
    See, it's not that hard, but there are types and function calls to consider.
    Last edited by whiteflags; 08-07-2016 at 06:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  2. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  3. How to pass an array of strings as a function argument?
    By Nazgulled in forum C Programming
    Replies: 7
    Last Post: 04-02-2007, 10:38 AM
  4. C function - Pass by value? Pass By Ref?
    By stevong in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 08:02 AM
  5. How to pass an array of strings by reference?`
    By JamesMI in forum C++ Programming
    Replies: 17
    Last Post: 01-25-2003, 08:23 PM

Tags for this Thread