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,945 questions

51,887 answers

573 users

How to dynamically create a 2d array of strings in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 5
#define LEN 6

int main(void)
{
	char** array2d = malloc(SIZE * sizeof(char*));

	for (size_t i = 0; i < SIZE; i++) {
		array2d[i] = malloc(LEN * sizeof(char));
	}

	for (size_t i = 0; i < SIZE; i++) {
		strcpy(array2d[i], "abcde");
	}

	for (size_t i = 0; i < SIZE; i++) {
		puts(array2d[i]);
	}

	for (size_t i = 0; i < SIZE; i++){
		free(array2d[i]);
	}

	free(array2d);

	return 0;
}





/*
run:
 
abcde
abcde
abcde
abcde
abcde
 
*/

 



answered Nov 17, 2022 by avibootz

Related questions

2 answers 171 views
1 answer 209 views
1 answer 172 views
...