What's wrong with my simple C code?
Hey,
I have two methods - one for getting input for a string array and the other for printing the array. It's for an assignment for school so I can't change the methods signiture.
The code:
Code:
#include <stdio.h>
#include <string.h>
#define TOTAL 5
int Get(char** StrArr,int total);
void Display(char** StrArr,int size);
int main(void)
{
int i;
char* Strings[TOTAL+1]={NULL};
//Get TOTAL dynamic strings from user
if (!Get(Strings,TOTAL)) exit(0);
//display the array of strings
printf("\n\nAfter input, ");
Display(Strings,TOTAL);
}
int Get(char** StrArr,int total)
{
int i, result = 0;;
for(i=0;i<total;i++)
{
printf("string %i:\t", i+1);
gets(StrArr+i);
if(i==4)
{
result = 1;
}
}
return result;
}
void Display(char** StrArr,int size)
{
int i;
printf("The strings are:\n");
for(i=0;i<size;i++)
{
printf("StrArr is: %p", StrArr+i);
printf("%s\n", StrArr+i);
}
}
When the input is something like:
aa
hh
cc
ee
gg
everything works as expected. When its something more complicated like:
aaaa
bb
ccccc
hh
f
The array doesn't print like it should.
It shows the first string as "aaaabb" and the third as "ccccf".
What's wrong with my code
Thanks!