LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Only part of the code executing (https://www.linuxquestions.org/questions/programming-9/only-part-of-the-code-executing-4175421007/)

NevemTeve 08-08-2012 12:27 PM

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.

chaacoali 08-08-2012 12:33 PM

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.

NevemTeve 08-08-2012 12:43 PM

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.

chaacoali 08-08-2012 12:47 PM

So str2 details are stored into a stack which already has some values in it.?. That's why the junk values are produced.?

chaacoali 08-08-2012 12:51 PM

So if I give str2="" initially. Can I overcome this problem.?

pixellany 08-08-2012 12:57 PM

This goes back to the beginning of the thread. Out of curiousity, I ran a quick test:
Code:

#include <stdio.h>
int main()
{
int i;       
for(i=0; i=0; i++){
        printf ("%d\n", i);
        }
}

This works as expected---i.e. it prints "0" and then exits.

If I change the 2nd loop parameter to "i=0" then---as predicted by others---nothing happens. But, the compiler does not complain.

SO....of what use is the "incorrect" construct? And--if it has no purpose, why does the compiler allow it

NevemTeve 08-08-2012 12:58 PM

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'; ?

NevemTeve 08-08-2012 01:01 PM

> 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)

chaacoali 08-08-2012 01:05 PM

Quote:

Originally Posted by NevemTeve (Post 4749153)
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.

Anyways thanks for your help.

pixellany 08-08-2012 01:08 PM

Thanks!!

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.".....?

NevemTeve 08-08-2012 01:09 PM

It doesn't prove anything: program-errors aren't bound to show themselves at the very first test.
If you want to be sure, do sg like this:

Code:

char str2[80];

memset (str2, '#', sizeof (str2));

/* build str2 */

printf ("str2='%s'\n", str2);

Now if you see any hash-marks, then the string isn't properly terminated.

chaacoali 08-08-2012 01:18 PM

This is the code I was talking about.

Code:

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

Note: There are no junk characters this time.

NevemTeve 08-09-2012 02:42 AM

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).

kauuttt 08-09-2012 03:59 AM

Quote:

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.


All times are GMT -5. The time now is 11:52 PM.