-3

Experts there is a question given in the book "Let us C" where the author has asked to write the output of the given program. The program is-

#include<stdio.h>
int main()
{
    char s[]="Churchgate: no church no gate";
    char t[40];
    char *ss,*tt;
    ss=s;
    while(*ss!='\0')
        *tt++=*ss++;
    printf("%s\n",t);
    return 0;
}

When I tried it on my gcc compiler, the output was core-dumped. Please explain why.Here ss and tt are character pointers. Here I also don't understand that what's the meaning of the statement ss=s; I mean we can't directly copy a string unless we are copying it character by character. And ss is a character pointer so it points to a character then ss=s means what? Does it mean it will point to the byte whose address is the ASCII value of s? I also don't understand this statement *tt++=*ss++. I don't have any clue about it. Please elaborate its meaning.

Next I don't understand why printf("%s\n",t) is used as though t is of character type but it is not storing anything according to the program.

wonea
  • 4,783
  • 17
  • 86
  • 139
SGG
  • 31
  • 1
  • 2
  • 8
  • 1
    `tt` is pointing nowhere.... – user2736738 Nov 18 '17 at 16:57
  • 2
    Maybe you wanted `tt=t`; and then at the end `*tt='\0'` – user2736738 Nov 18 '17 at 16:58
  • Also, indentation:( – Martin James Nov 18 '17 at 17:04
  • The "warning" section here: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list says the following about the book you are reading: `It is a horribly outdated book that teaches Turbo C and has lot of obsolete, misleading and downright incorrect material.` – babon Nov 18 '17 at 17:57
  • Note: ASCII would not be used. The literal string is encoded with the [execution character set](https://gcc.gnu.org/onlinedocs/cpp/Character-sets.html#Character-sets) by the compiler. [`-fexec-charset`](https://gcc.gnu.org/onlinedocs/cpp/Invocation.html#Invocation). – Tom Blodget Nov 19 '17 at 01:41

1 Answers1

0

First thing first' Array name is nothing but the pointer that holds the address of the first element. So,

char s[]="Churchgate: no church no gate";

here s is the pointer name. And it holds the address of 'C' of the string. Now you have declared another pointer which is also a character type i.e. ss.

so, ss=s; means ss is also holding the address which is held by s also.

As you haven't store anything to t so it will print garbage. Similarly pointer tt is not storing any address initially(Garbage). And lastly and most importantly you can't perform this op: *tt++=*ss++; probably show you error of lvalue recquired.

Suvam Roy
  • 963
  • 1
  • 8
  • 19