![]() |
|
#5
|
|||
|
|||
|
well basically the design is not good but you can make it work like that if you just
assign the increment values correctly no need to have fixed string length you can do variable string length as long as you take care not to overflow your malloc size Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
char * pi;
int i,r,len[5]= {0},wen =0 ,foo=0;
puts("start");
pi = (char *) malloc(50 * sizeof(char));
for(i = 0; i < 5; i++)
{
r = scanf("%s%n",&pi[wen],&len[i]); //len[i] will hold the length of input
if(r != 1)
break;
if(i==0)
wen = wen + len[i]+ 1; //first time we need to add a 0 terminator
else
wen = wen + len[i]; // from second time no need coz it will point right
}
i = 0;
while(i < 5)
{
printf("%s",&*(pi + foo ));
putchar('\n');
if(i==0)
foo = foo + len[i] + 1; //same as above skip 0 terminator
else
foo = foo + len[i]; // no need second time onwards
i++;
}
free(pi);
puts("Done!");
getch();
return 0;
}
Code:
start rrrrrrrrrrrrrrrrrrrrr ttttttttttttttttttttttt yyyyyy zest malloc rrrrrrrrrrrrrrrrrrrrr ttttttttttttttttttttttt yyyyyy zest malloc Done! have fun |
| Thread Tools | |
| Display Modes | |
|
|