-1

Why does this basic C++ program gets stuck after completion, it doesn't returns back to code in TurboC++. However, it works fine for words with 1 and 3 characters. It also runs perfectly in CodeBlocks.

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.h>

void main()
{
    clrscr();
    char * name;
    cout<<"Enter your name : ";
    gets(name);
    int len = strlen(name);
    for(int i=0;i<len;i++)
    {
        cout<<name[i]<<" "<<i<<endl;
    }
    getch();
}

If I change char* to char name[20], it works perfectly. Can anyone please explain the reason for it. If their is problem in char *, then why does CodeBlocks runs it without any problem?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Vaibhav Vats
  • 41
  • 1
  • 7
  • 3
    Why are you using TurboC++? That thing is at least 3 decades old! No wonder it doesn't work correctly, it was developed before C++ was standardized! Please use CodeBlocks with gcc or something else, but **not** TurboC++. – Rakete1111 Jan 11 '17 at 09:17
  • I know bro, but someone came to me with this doubt and I was not able to figure it out. – Vaibhav Vats Jan 11 '17 at 09:18
  • 4
    The variable `name` is a pointer, but where does it point? When you can answer that you know what's wrong. – Some programmer dude Jan 11 '17 at 09:18
  • 1
    Also, don't use `gets`. Never ever use `gets`. It is a bad function, and dangerous. It has also been removed from both the C and C++ standards. – Some programmer dude Jan 11 '17 at 09:19
  • [Why is the gets function so dangerous that it should not be used?](http://stackoverflow.com/q/1694036/995714). And please learn how to indent properly – phuclv Jan 11 '17 at 09:20
  • 2
    I don't understand the downvotes. This question is clear and has a short compilable example. – Bathsheba Jan 11 '17 at 09:21
  • Sorry guys, but even my teacher was Clueless when i asked her, that's why I posted it here. – Vaibhav Vats Jan 11 '17 at 09:29
  • Do feed back the answer *constructively* to your teacher. – Bathsheba Jan 11 '17 at 09:32
  • 3
    I also do not understand downvotes. If question is trivial, it does not mean that it should be downvoted. – Ari0nhh Jan 11 '17 at 10:12
  • Code::Blocks is an IDE, not compiler, and any C++ compilers working with it will not compile your code because there are a lot of problems with it: there's no `iostream.h`, `clrscr()` and `getch()` in C++. There are also no `cout` and `endl` in global namespace in C++ – phuclv Feb 25 '17 at 12:59

1 Answers1

4

You didn't allocate any memory for name.

The behaviour on using a pointer that doesn't point to any memory that you own is undefined.

That's why char name[20]; works. Although gets is unsafe, as you can't control how many characters are read in so your character buffer could overrun. Why not use a std::string, cin, and a more up to date compiler?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    @VaibhavVats please treat other users with respect. Also thank you comments are strongly discouraged. Better leave only the constructive part of your comment – Ivaylo Strandjev Jan 11 '17 at 09:25
  • actually both ;) If I am to be threatened better be with respect :) However I noticed the typo even before your commented – Ivaylo Strandjev Jan 11 '17 at 09:29
  • I am new to stack Overflow, so I am currently learning about the rules here. Sorry for any mistake. – Vaibhav Vats Jan 11 '17 at 09:33