LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2012, 12:27 PM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869

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.
 
1 members found this post helpful.
Old 08-08-2012, 12:33 PM   #17
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-08-2012, 12:43 PM   #18
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
1 members found this post helpful.
Old 08-08-2012, 12:47 PM   #19
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
So str2 details are stored into a stack which already has some values in it.?. That's why the junk values are produced.?
 
Old 08-08-2012, 12:51 PM   #20
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
So if I give str2="" initially. Can I overcome this problem.?
 
Old 08-08-2012, 12:57 PM   #21
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 08-08-2012, 12:58 PM   #22
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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'; ?
 
Old 08-08-2012, 01:01 PM   #23
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> 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)
 
Old 08-08-2012, 01:05 PM   #24
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 08-08-2012, 01:08 PM   #25
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.".....?
 
Old 08-08-2012, 01:09 PM   #26
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
Old 08-08-2012, 01:18 PM   #27
chaacoali
LQ Newbie
 
Registered: Apr 2012
Posts: 18

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-09-2012, 02:42 AM   #28
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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).
 
Old 08-09-2012, 03:59 AM   #29
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Only executing signed code derchris Linux - Security 5 02-23-2011 03:06 PM
Application sometimes hang when executing this C code archieval Programming 2 11-02-2010 08:05 AM
executing C++ code from within PHP code vineet7kumar Programming 1 06-03-2007 04:13 PM
Executing command line through Code socialjazz Programming 2 10-02-2006 03:09 PM
executing a command as part of printf... thatbloke Linux - Newbie 6 05-06-2004 04:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration