Thread: Pass multiple strings to function C

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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