Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,788 answers

573 users

How to initialize 2D char array in C

2 Answers

0 votes
#include <stdio.h>

int main(){
    char arr[][4] = { "abcd", "efgh", "ijkl" };

    int rows = sizeof(arr)/sizeof(arr[0]);

    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < 4; j++) {
            printf("%c ", arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}




/*
run:

a b c d
e f g h
i j k l

*/



 





answered May 5, 2021 by avibootz
edited May 5, 2021 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(){
    const char *arr[3] = { "ab", "efg", "ijkl" };

    for (size_t i = 0; i < 3; i++) {
        for (size_t j = 0; j < strlen(arr[i]); j++) {
            printf("%c ", arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}




/*
run:

a b
e f g
i j k l

*/

 





answered May 5, 2021 by avibootz

Related questions

1 answer 57 views
2 answers 82 views
82 views asked May 5, 2021 by avibootz
...