LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c: output interpretation (https://www.linuxquestions.org/questions/programming-9/c-output-interpretation-474693/)

kpachopoulos 08-17-2006 05:00 AM

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�▒]�Oêu}  )�E�t41&G�9}��T]�}�Ë$ÐU��t1ۍ'Ћ�u�[]��P�[��$�E�EE$�E��u�$Y]aÐU�▒]��u}�  )�E�
                                                                      �X[�kaaads%s

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


cdhgee 08-17-2006 05:19 AM

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";

xhi 08-17-2006 09:33 AM

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

xhi 08-17-2006 09:34 AM

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


All times are GMT -5. The time now is 09:44 AM.