c

This is a C program meant to run the string characters in the array


void str1(char *str1, int size);

int main()
{
    char *str[2][15] = {
        "You know what",
        "C is powerful"
    };
    char *ptr;

    ptr = &str[0][0];
    str1(*ptr, 2);

    return 0;
}

void str1(char *str1, int size)
{
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++)
            printf("%s", *str1[i][j]);
    }
}

I am expecting it to print out each string character in the array after each execution of the for loop. I am still trying to understand how arrays, pointers and functions work in C programs.


Solution

  • Let's point out the issues in your code one by one.

    This

    char *str[2][15] = ....
    

    will create a 2D array of type char *. The in-memory view would be something like this:

    str:
    +--------------------------------------------+
    |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
    +--------------------------------------------+
    |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
    +--------------------------------------------+
    

    Each member of this array is a pointer to char type.

    Because str is a 2D array, the way you are trying to initialize it is incorrect. Compiler must be throwing error on it.

    After that, you are doing this:

    ptr = &str[0][0];
    

    The type of str[0][0] is char * and type of &str[0][0] is char ** and you are attempting to assign it to ptr whose type is char *. You must be getting assignment to char * from incompatible pointer type char ** error on this.

    After that, you are doing:

    str1(*ptr, 2);
    

    The type of ptr is char * and the type of *ptr is char. The type of the first parameter of str1() is char *.

    In str1(), the first parameter str1 is of type char * and in printf() you are passing *str1[i][j] which is wrong.

    Looks like you want to create an array of 2 members of char * type and initialize them by pointing them to the strings providing in the initializer. You should do it like this:

    char *str[2] = {"You know what", "C is powerful"};
    

    It is a 1D array of type char *. The in-memory view would be something like this:

    str
    [0]  [1]
    +---+---+
    |   |   |----------> "C is powerful"    
    +---+---+
      |
      +----------------> "You know what"
    

    Create a pointer and make it point to str:

    char **ptr;
    
    ptr = str;
    

    Why is the type of ptr char **?
    Because when you access an array, it is converted to a pointer to first element (there are few exceptions to this rule), and the type of the first element of the str array is char *, so the type of a pointer to its first element will be char **. Hence, the type of ptr is char **.

    The str1() function prototype will be:

    void str1(char **str1, int size);
    

    and its definition will be:

    void str1 (char **str1, int size) {
        for (int i = 0; i < size; i++){
            printf("%s\n", str1[i]);
        }
    }
    

    Putting these all together:

    #include <stdio.h>
    
    int main (void) {
        char *str[2] = {"You know what", "C is powerful"};
        char **ptr;
    
        ptr = str;
        str1 (ptr, 2); // Instead of passing 2, you can also do sizeof (str)/sizeof (str[0])
    
        return 0;
    }
    
    void str1 (char **str1, int size) {
        for (int i = 0; i < size; i++) {
            printf("%s\n", str1[i]);
        }
    }
    

    Output:

    You know what
    C is powerful
    

    Hope this helps.