+ 1

why it prints nothing in output?? please clarify me this doubt

#include <stdio.h> #include<string.h> int main() { char p[20]; char *s = "string"; int length = strlen(s); int i; for (i = 1; i < length; i++) { Ā Ā Ā Ā Ā p[i] = s[length - i]; printf("%s",p); } return 0; }

10th Jul 2020, 7:30 PM
C Lover
19 Respostas
+ 2
why it is not working with *s ?? please clarify me and what is difference between char []s and char *s? am not modify I'm Just storing it to another array and printing then how it crashes?
10th Jul 2020, 7:38 PM
C Lover
+ 2
char p[20]; char *s = "string"; int length = strlen(s); int i; for (i = 0; i < length; i++) p[i] = s[length -1 - i]; p[i] = '\0'; printf("%s",p);
10th Jul 2020, 7:47 PM
rodwynnejones
rodwynnejones - avatar
+ 2
becoz you did not consider that array Index starts counting from ZERO.
12th Jul 2020, 2:02 PM
bagheb00n
bagheb00n - avatar
+ 1
~ swim ~ I'm not changing the value of s but I'm storing it on another variable right but still why it is not modified
10th Jul 2020, 8:18 PM
C Lover
+ 1
~ swim ~ since from start it was there
10th Jul 2020, 8:31 PM
C Lover
+ 1
~ swim ~ in my case I'm not modifying s I'm reversing and storing on p only .. but why not printing p[i]=s[length-i] ....I'm not modifying s ... I'm storing in p
10th Jul 2020, 9:13 PM
C Lover
+ 1
codemonkey I know the code fix ... but I want to know the reason why it is printing garbage value? when p[6]
10th Jul 2020, 9:16 PM
C Lover
+ 1
codemonkey but I have given %s only...now it not giving garbage why #include <stdio.h> #include<string.h> int main() { char p[6]; char *s = "string"; for (int i=0;i<5;i++) { p[i]=s[i]; } printf("%s\n",p); return 0; }
10th Jul 2020, 9:43 PM
C Lover
0
~ swim ~ I want to know why it prints nothing... I don't want code fix
10th Jul 2020, 8:33 PM
C Lover
0
~ swim ~ but instead of char *s I'm giving char s[] but now it is printing in output.. how it works with char s[] and not working with char *s?
10th Jul 2020, 8:44 PM
C Lover
0
~ swim ~ so char *s cannot be copied to any other variables??
10th Jul 2020, 8:59 PM
C Lover
0
~ swim ~ please check the below code char *s can be copied to another variable but I don't know why it is putting some garbage value at last int main() { char p[6]; char *s = "string"; for (int i=0;i<6;i++) { p[i]=s[i]; } printf("%s\n",p); return 0; }
10th Jul 2020, 9:08 PM
C Lover
0
codemonkey yes we can copy but I'm not modifying *s ... I'm storing s[0] to p[0] but why it prints nothing?? I don't want to fix the code.... I want to know the reason why it prints nothing
10th Jul 2020, 9:10 PM
C Lover
0
codemonkey here also I'm not copying null but it's not printing garbage why?? #include <stdio.h> #include<string.h> int main() { char p[6]; char *s = "string"; for (int i=0;i<5;i++) { p[i]=s[i]; } printf("%s\n",p); return 0; }
10th Jul 2020, 9:20 PM
C Lover
0
~ swim ~ in my case I'm not modifying s I'm reversing and storing on p only .. but why not printing p[i]=s[length-i] ....I'm not modifying s ... I'm storing in p
10th Jul 2020, 9:44 PM
C Lover
0
~ swim ~ okay see the below scenario .. I'm not storing anything in p[0] ... but it is printed as tring ... please explain this you told if p[0] is 0 or null then it will not print anything for p int main() { char p[7]; char *s = "string"; for (int i=1;i<7;i++) { p[i]=s[i]; } printf("%s\n",p); return 0; }
10th Jul 2020, 10:10 PM
C Lover
0
As you use %s which is used for whole string, u can use %c as you are printing each character Or After the for loop: End the string p[i] ='\0' ; then print the string using %s
11th Jul 2020, 3:53 PM
Samba_Chinta
Samba_Chinta - avatar
0
Hola buenas tengo un proyecto para hacer y pues me decidĆ­ por investigar una app y lo malo es que no se de progracion ni lenguajes para poder investigar el como fue su cĆ³digo fuente y desarrollo del cĆ³digo alguien que me pueda ayudar
11th Jul 2020, 6:54 PM
Alejandro MuƱoz
Alejandro MuƱoz - avatar
0
As string is collection of characters, we use %s specifier to print whole string, but u r printing each character in string. So it not through the error
12th Jul 2020, 1:33 AM
Samba_Chinta
Samba_Chinta - avatar