ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If you forget to put the terminating zero to the end of the string, then it will depend on the current context what will be there after the desired characters.
Your cycle transfers every character of the string, but doesn't transfer the terminating zero, that has to be done with a separate instruction.
I partially understood. What do you mean by the current context.? What is the current context in this program. Sorry for asking too many questions. Just want to get this clear so that I dont repeat such mistakes.
The local variables are stored on the stack. And they are not initialized automatically. So their content will depend on what was on the stack earlier. For example, if you add another puts("Hello") into your program, the content of the stack might be different afterwards.
> SO....of what use is the "incorrect" construct? And--if it has no purpose, why does the compiler allow it
The syntax of for is:
Code:
for (expression; expression; expression) statement
Your 'incorrect' statement is perfectly correct... but is still generates a warning if you enable compiler warnings. (For gcc use -W -Wall -Wextra -pedantic just to be on the safe side)
Initialization with "" means putting zero into the first byte (as in str2[0]= '\0'). It doesn't mean that the k-th character will be zero.
PS: To tell the truth, I don't see why you keep bargaining; is it problematic to write a single statement: str2[k]= '\0'; ?
ha ha not any bargaining. I'm just asking if I did something like this would the error have occured. I have a similar program extracting a substring, in that case I haven't included the '/0' and it didn't show any junk characters.
My hangup was that the 2nd expression is supposed to be a TEST and not an assignment. So the compiler is saying (with no warnings turned on): "That statement is not illegal, but your loop won't work.".....?
int main()
{
int i,j,k=0;
char s[]="hi how are you";
char s1[20]="";
printf("from which pos 2 be copied\n");
scanf("%d",&j);
for(i=j;i<10;i++)
{
s1[k++]=s[i];
}
printf("length of s1 is %d\n",strlen(s1));
printf("%s\n",s1);
return 0;
}
and the output
Code:
./a.out
from which pos 2 be copied
2
length of s1 is 8
how are
I've just checked with gcc; I was wrong, initialization with "" does fill the whole str1 with zeroes (with five machine instructions).
Still, putting an 's1[k]='\0'; after the cycle would be a good idea (two machine instruction).
So this problem, is it caused because of my str2[i++] statement or is it because of something else.?
The above statement of your increments i one time extra and thats one of the issue with your code.
Secondly, you are allocation the array str[10] statically, which means 10 garbage values. Plus you are not appending with '\0', hence you should expect what the output you are getting.
In comparison to the output what you get, check the one what I am getting with the same piece of code.
Yours:
Code:
enter de string
james
length of de string 5
semaj��
length of reversed string 7
Mine:
Code:
enter de string
shit
length of de string 4
Reversed String: tihs����shit
length of reversed string 14
Hope it helps.
Lastly, please make it a habit of using proper indentation of your code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.