LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-17-2006, 05:00 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Rep: Reputation: 30
c: output interpretation


Hi,
i have written this program -i know it's wrong. However, can somebody interpret the following "unpredictable" -as far as my concerns- output? What is it? Does it enter the "object code area" of the binary? If somebody can clear out with a little bit more detailed info...

Thanks
Code:
#include <stdio.h>
#include <dirent.h>


#define NUM_OF(x) (sizeof(x) / sizeof(*x))

main(int argc, char* argv[])
{
	char* s="kaaads";
	printf("%s \n", s);
	char c;

	do
	{		
		c=*s;
		printf("%c", c);
		s++;
	}
	while (s!='\0');
}
and the output is:
Code:
kaaads
kaaads%s

t
T
ooGCC: (GNU) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)GCC: (GNU) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)GCC: (GNU) 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)GCC: (GNU) 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)GCC: (GNU) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)GCC: (GNU) 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)GCC: (GNU) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)"|
                                                                                     $!y_IO_stdin_use../sysdeps/i386/elf/start.S/home/aurel32/tmp/i386/glibc-2.3.6/build-tree/glibc-2.3.6/csuGNU AS 2.16.93jweT:intIj[�OV/home/aurel32/tmp/i386/glibc-2.3.6/build-tree/i386-libc/csu/crti.S/home/aurel32/tmp/i386/glibc-2.3.6/build-tree/glibc-2.3.6/csuGNU AS 2.16.91f)/home/aurel32/tmp/i386/glibc-2.3.6/build-tree/i386-libc/csu/crtn.S/home/aurel32/tmp/i386/glibc-2.3.6/build-tree/glibc-2.3.6/csuGNU AS 2.16.91


                                             >
                                              $

                                              >
                                               4:
                                                 ;
                                                  I?

/home/aurel32/tmp/i386/glibc-2.3.6/build-tree/i386-libc/csucrti.3!/!!Z!#!/=
/home/aurel32/tmp/i386/glibc-2.3.6/build-tree/i386-libc/csucrtn.S!!!    !GNU C 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)init.cshort intlong long intunsigned charlong long unsigned intshort unsigned int/home/aurel32/tmp/i386/glibc-2.3.6/build-tree/glibc-2.3.6/csu_IO_stdin_used.symtab.strtab.shstrtab.interp.note.ABI-tag.hash.dynsym.dynstr.gnu.version.gnu.version_r.rel.dyn.rel.plt.init.text.fini.rodata.eh_frame.ctors.dtors.jcr.dynamic.got.got.plt.data.bss.comment.debug_aranges.debug_pubnames.debug_info.debug_abbrev.debug_line.debug_s( 1H,7
                                                                                            t`?�TGo(
                                                                                                    ELF4�4 (8888
                                                                                                              L��(  Q�d/lib/ld-linux.so.2GNU▒8�"9) __gmU����%%h�%�%h�%h▒�1���TRhhQV���Q�[���X[�U=tsed__libc_start_mainGLIBC_2.0ii
]�}��֍v'G�;}���}�&U�▒]�Ou}  )�E�t41&G�9}��T]�}�$U��t1ۍ'Ћ�u�[]��P�[��$�E�EE$�E��u�$Y]aU�▒]��u}�  )�E�
                                                                       �X[�kaaads%s

t
T
 oo�o4`@�=�MSegmentation fault

Last edited by kpachopoulos; 08-17-2006 at 05:07 AM.
 
Old 08-17-2006, 05:19 AM   #2
cdhgee
Member
 
Registered: Oct 2003
Location: St Paul, MN
Distribution: Fedora 8, Fedora 9
Posts: 513

Rep: Reputation: 30
At first glance, part of the problem is this line:

Quote:
Originally Posted by nocturna_gr
Code:
	char* s="kaaads";
You've declared it as a pointer, not as an array of characters - hence there is no actual space allocated for the string, so the "kaaads" will get dumped into memory at wherever s happens to be pointing. What you should have instead is

Code:
	char s[]="kaaads";
 
Old 08-17-2006, 09:33 AM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
in your while loop you are comparing the value of the ptr (a memory address not what is at that address) to the null char (0) so as long as the ptr is pointing at some valid address it will print.

the most common solution
Code:
while ((*s)!='\0');
 
Old 08-17-2006, 09:34 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> At first glance, part of the problem is this line:

actually that line is ok. its creating a string in the local scope that is pointed to by s..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Command interpretation issue in Sed script file angel115 Programming 9 04-21-2006 07:26 PM
Error message need interpretation Scallawag Linux - Software 3 02-03-2005 05:27 PM
tcpdump interpretation unixbrain Linux - Networking 7 04-28-2004 04:37 AM
sendmail logwatch interpretation jimi_j Red Hat 2 03-07-2004 07:19 PM
2.4.18 Crash interpretation: Known problem? svar Linux - General 2 09-17-2003 12:42 AM


All times are GMT -5. The time now is 02:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration