![]() |
|
|
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
Hi,
Thank you so much everybody who has tried to help me in this topic. I learned some new points in programming by looking at your skillfully coded programs in this topic. Sorry but,I still have some problems and I hope you can shed some lingt to clarify the obscure points. I know that,as a convention, the name of an array is the same as the address of its first element. So when you want to use an array in scanf() function you don't need to use ampersand operator(&) with its name and you just type the name of the array. But for the second or other elements of that array you need to use (&) to be able to point at the address of that element. But this rule is not devised for printf() function. In printf() function you can easily type the name that element and the printf() function shows what is inside that element. For example if you have an array witch is named exp your prohgram should work considering following statements: int exp[20]= {0}; exp == &exp[0]; so scanf("%d",exp); // it should work properly but for the second element you should code this statemant: scanf("%d",&exp[1]); but for printf you can just use this statement: printf("%d",exp[0]); //without ampersand operator I hope up to now I'm correct about this rule. But in your code I can see that you have used ampersand operator with both of the functions scanf() and printf(). For example ARC has used these statements: *** scanf("%s",&pi[i*10]); printf("%s\n",&pi[i*10]); *** Again JuneMouse has used these ones: *** scanf("%s%n",&pi[wen],&len[i]); printf("%s",&*(pi + foo )); *** Maybe I'm cionfused because we are using pointers in the program and you can use ampersand in this way while you have pointers. Could you please explain the fact? Also let me know if we can use pointer without the ampersand operator in out code. The second obscure point for me is to know how this part of program works: 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 want to know why we shouldn't increment "wen + len[i]" in the second time. what will happen here that we don't need to add it to one? If you don't mind please explain. Thanks in advance. Best Regards, Zest. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|