Member Avatar for Rahul47

Here is my nifty program.

#include<stdio.h>

int main(){
    char *str;
    str="Hello World!!";
    puts(str);
    printf("\nEnter a new string: ");
    gets(str);                          // Is this right ?                      
    printf("\nString after entering: "); 
    puts(str);                          // And this one ?
    getch();
    return 0;
}

The program did printed "Hello World!!" but failed to take user input.

1 - for use getch(), i must include the conio.h ;
2 - you did some mistakes when you inicializade the c-style string:

char str[]="Hello World!!";

heres your code correted:

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

int main()
{
    char str[]="Hello World!!";
    puts(str);
    printf("\nEnter a new string: ");
    gets(str); // Is this right ?
    printf("\nString after entering: ");
    puts(str); // And this one ?
    getch();
    return 0;
}

instead use '*', use the '[]'(c strings are 1 array of chars) ;)
i hope these helps you more or someone give you more information

Member Avatar for Rahul47

1 - for use getch(), i must include the conio.h ;

There is no problem with getch(), it works fine. You should check your compiler. I am using Dev C++.

2 - you did some mistakes when you inicializade the c-style string

C supports an alternative method to create strings using pointer variables of type char.
For example, char *str="Hello World!!";.
And we can also use the run-time assignment for giving values to a string pointer.
For example, char *str; str="Hello World!!";
Note that it is not string copy, because the variable str is a pointer, not a string.

depending on compiler options... i'm using Code Blocks... honestly it's better than Dev C++ and i had used it before, but by some information, i give up on it (isn't updated and have some errors);)
i'm trying test more your code, but i can't find the solution... maybe another person can help you more.
but why not use '[]' instead '*'? at least your code will be working like a sharm ;)
sorry that i can't help more

Member Avatar for Rahul47

but why not use '[]' instead '*'?

Am experimenting :-)

The difference between char *str="Hello World!!";. and char str[]="Hello World!!";. is that char* str is just a pointer into read-only memory where the string "Hello World" is actually stored, it's contents can not be changed. char str[] makes a copy of the string and puts it into read/write memory where you can change the string nowever you wish -- you just can't change the length of the string. Using gets() on char str[] works just fine as long as you don't enter more characters then there are in the original string.

You can't use char* str with gets() because char* str had no memory allocated to it. Where is gets() supposed to put the characters you type? It is YOUR responsibility to entire that the string you pass to gets() has enough memory allocated to it to hold all the characters you want to type. And that is a big big problem with gets() and why everyone discourages it's use in most programs. instead of gets() use fgets() instead because fgets() limits the characters you type to the size of the buffer.

commented: thanks +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.