Click here to Skip to main content
15,900,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


    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_5 [] = {"mm","End of Multiple Strings Array","simple bluetooth connection",
    "datalogging purposes and accessing recorded data","THE OPERATING ENVIRONMENT"};

    int8_t cnt5 = sizeof(array_5)/sizeof(array_5[0]);
    int8_t len5;

    return 0;

What I have tried:

Pass multiple strings array pointer to a function, with name of the array as a start and then I think to develop array of arrays pointers to pass them consequently to a function pointer.

I have changed the asterisk in the function call, function declaration, definition and printf of the array pointer.</stdint.h></string.h></stdio.h>
Updated 8-Aug-16 1:46am
Patrice T 7-Aug-16 16:06pm    
Where is the code ?
Use Improve question to update your question.

A few things, first of all if you are going to do this,
void print_array(char *arr,int8_t cnt);
void print_array(char *arr,int8_t cnt) {}

It is better to just do the later one, remove the declaration statement and keep the definition (and declaration) on only. Secondly, since you already know how to pass strings (which are array of characters). Like this,
print_array(char *arr, int8_t cnt) { }

What stops you from doing this?
print_arrays(char **arr, int8_t cnt) { } // cnt won't make much sense now.

Notice that I used a pointer to pointer[^] here. Which means, or at least I can say, in the C language context, an array of array. You can do the following on it, then,
char **array = { "Afzaal", "Ahmad", "Zeeshan" };
// print_arrays(array, 3);

Finally, I didn't actually get the part where you wanted to assign the pointer to the array, because you actually get the pointer to the array. However, that would now take us in the realm of C++ where references are supported and as far as I can conclude, C doesn't support pass-by-reference, only pass-by-value and that is exactly why the C functions look like this,
void func(char *obj) {
   // Do something

int main() {
   int a = 25;

I would move you onwards to learn something, keep trying. My personal recommendation is to use GCC compiler. That is a good one and I personally like it.

Passing value by reference in C and C++ - C++ Forum[^]
Strings in C[^]
C Strings -[^]
Share this answer
Thank you very much for the answer, I freaked out last night because I was searching and looking into the eBooks for the exercise or example about this specific action. And I couldn't, then this morning I told the trainer about it and he solve it right away! It's very simple.

    void print_array(char *arr[],int8_t cnt);
    void print_array(char *arr[],int8_t cnt)
        int8_t i;
        printf("Number of elements is: %d\n",cnt);
        for (i=0;i<cnt;i++)
            printf("Elements of array: %s\n",arr[i]);

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900