Program for work with arrays in dynamic memory.
Need equivalent for C. Can anybody help?
const int n = 6;
char **words = (char**) malloc(n *sizeof(char*));
for(int i = 0 ; i < n; i++)
words[i] = (char*)malloc( 50 * sizeof(int));
for(int i = 0; i < n; i++)
{
cin>>words[i];
}
cout<<endl;
for(int i = 0; i < n; i++)
{
if(words[i][0] == 'q')
cout<<words[i]<<endl;
}
The only C++ parts there are
cinandcout; you can change them easily:becomes
or
while
becomes
By the way, in the
cin/scanf/getsyou have a potential buffer overflow, since you are allocating space for 6 characters but you are accepting input of any length. You should do instead:or (more maintainable, since it uses
ndirectly)(although this will include the trailing
\nin the string)