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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,933 questions

51,870 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 101 views
1 answer 163 views
1 answer 126 views
1 answer 104 views
2 answers 151 views
151 views asked May 5, 2021 by avibootz
...