How to pass a two dimensional string as a parameter of a function? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

How to pass a two dimensional string as a parameter of a function?

So I tried to insert a two dimensional string in a void function but it did not work. How can I fix it? https://code.sololearn.com/czk1ezG5YDi9/?ref=app

4th Nov 2020, 12:01 AM
Hadjer
Hadjer - avatar
4 Antworten
+ 2
A string is a one dimensional structure by definition
4th Nov 2020, 12:06 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
Hadjer I’d try this: /* Flash In: 2 solo learn Out: solo learn */ #include <stdio.h> #include <stdlib.h> #include <string.h> void printc(char** a, int l) { for(int i = 0; i < l; i++) { printf("%s\n", a[i]); } } int main() { int c = 0; scanf("%d ", &c); char buf[20]; char** ss = malloc(sizeof(char*) * c); for (int i = 0; i < c; i++) { ss[i] = malloc(sizeof(char)*20); fgets(buf, 20, stdin); char* n = strchr(buf, '\n'); if (n) { *n = '\0'; } strcpy(ss[i], buf); } printc(ss, c); for (int i = 0; i < c; i++) { free(ss[i]); } free(ss); return 0; } https://code.sololearn.com/cJnth3y3vDgO/?ref=app
4th Nov 2020, 3:36 AM
Flash
+ 1
Martin Taylor I mean one array which contains strings/group of characters. More like this Char *str[2] ={ "Hello" , "World" } ; But the user will be the one that enters the strings.
4th Nov 2020, 12:29 AM
Hadjer
Hadjer - avatar
+ 1
Flash the code is working. Thx 😊
4th Nov 2020, 8:38 AM
Hadjer
Hadjer - avatar