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; }
7/10/2020 7:30:35 PM
C Lover
28 Answers
New AnswerI get you are not modifying. You are starting at index 1, so while s[length - i] gives you the correct character from the string pointed to by s, the code writes s[length - i] character at p[1] (which should have been p[0]) And you are not understanding that in array p if you have 0 or '\0' at p[0] then you will not get the output because the string has finished at p[0] itself. Start with i = 0 in for loop or write p[i-1] = s[length - i]; and your loop terminating condition should be i <= length, not i < length ( if you start from i=1) if starting from i = 0, then i < length is write but s[length - i] should now be s[length-i-1]
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?
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);
~ 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
char *s = string is a special case. The actual type of s is (const char*) which means the string pointed to by "s" cannot be modified. You can copy contents of s char by char to an array or to another char* (dynamic array) or you can use strcpy to copy the data. so char *s = "string"; s[0] = 'c'; // error and crash char p[20]; p[0] = s[0]; // fine
~ 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
codemonkey I know the code fix ... but I want to know the reason why it is printing garbage value? when p[6]
It's by chance only. Your array must be having 0 at p[5] here. Do one thing try this After declaring char p[20]; print the values it contains. for (int i=0;i<6;i++) printf("%d\n", p[i]); (int used for seeing the character value as int, since some characters are unprintable. See what you get at p[5]
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; }
You are circling around the same point. A string declared like char *s = "string" is readonly where as a string declared as char s[] = "string"; is modifiable. The compiler copies the string "string" into char array to allow modification.
Wait, I am confused. Was char p[20] was there since the start or you added it later?
I am sorry i didn't read the code properly. I am deleting my comment, since it is wrong in the context. See rodwynnejones answer. His answer is right. Sorry for the inconvenience.
You are starting from i = 1 and it is likely that p[0] has a value of null terminator ('\0') at sololearn, so the string ends at p[0] itself. you should start the loop from 0, or write p[i-1] if starting from 1 also s[length - i] should be s[length-i-1] since the last character is at the position s[length - 1]; Then once the loop is over you should explicitly add a null terminator s[length] = '\0'; // an array is not a string till the null terminator is added.
~ 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?
There is no char s[] in your code, there is char p[20] in your code. You are iterating over the string (read only) pointed by "s" from the last to the first character and copying each character in p[i] (starting from 1st position onwards). And as i told earlier declaring a string like char *s = "string"; // s is pointing to a readonly string which cannot be modified. In this char s[] = "string"; string "string" is copied into array (including the hidden null terminator, so that it can be modified.
~ 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; }