Originally Posted by
std10093
How many strings should you copy inside your array?
- I should copy inside be array as many strings as the user gives in the cmd
I'm using j for that purpose (to start argv at index 1) . I fixed the for:
Code:
for(j = 1, i = 0; i < MAXPOINTERS; i++, j++)
{
Strcpy(s[i], argv[j]);
}
But I'm still getting the same error.
edit:
okey-dokey, I fixed this else as you told me:
Code:
else if(argc >= MAXPOINTERS)
{
printf("You can write up to %d words", MAXPOINTERS);
exit(0);
}
The current code:
Code:
#include <stdio.h>
#include <stdlib.h> /* for exit and malloc*/
#include <string.h>
#ifndef MAXPOINTERS
#define MAXPOINTERS 20
#endif
#ifndef MAXCHARS
#define MAXCHARS 100
#endif
void Strcpy(char*, char*);
char** Malloc2D(int, int);
void Strcpy(char* s, char* t)
{
while( (*s++ = *t++) )
;
}
char** Malloc2D(int nPointers, int nChars)
{
int i;
char** strings;
strings = malloc(nPointers * sizeof(char*));
for(i = 0; i < nPointers; i++)
{
*(strings + i) = malloc(nChars * sizeof(char));
}
return strings;
}
int main(int argc, char** argv)
{
int i, j;
char** s;
if(argc < 2)
{
printf("Please enter [Executable][string]\n");
exit(0);
}
else if(argc >= MAXPOINTERS)
{
printf("You can write up to %d words", MAXPOINTERS);
exit(0);
}
s = Malloc2D(MAXPOINTERS, MAXCHARS);
for(j = 1, i = 0; i < MAXPOINTERS; i++, j++)
{
Strcpy(s[i], argv[j]);
}
printf("The strings which were input are:\n");
for(i = 0; i < MAXPOINTERS; i++)
{
printf("%d.\t%s", i, s[i]);
}
return 0;
}