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
5 odpowiedzi
+ 2
A string is a one dimensional structure by definition
+ 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
+ 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.
+ 1
Flash the code is working.
Thx 😊