i want to know why the runtime error is coming in this code?????

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char *a,*b;
    scanf("%s",a);
    printf("%s",a);
    return 0;
}

there was no error when a declared first pointer i.e. a but as soon as i declared the 2nd pointer(b) i am getting a runtime error....above code is just an example.... i have a problem in which i want to declare two pointers of undefined length....so i don't want to use malloc....do help me in this matter asap!!!!!

Recommended Answers

All 2 Replies

Probably because you are reading into a pointer but there is no storage space so you are overwriting some unknown memory you shouldn't be touching.

When asking for help, be sure to give better detail. There are hundreds of "runtime errors". Which one? And what statement?

And don't use anything in conio.h. You'll be sorry when you move on to a real compiler and your functions no longer work.

ave a problem in which i want to declare two pointers of undefined length....so i don't want to use malloc...

you have to use malloc() to allocate space in which to store the characters received from scanf(). If you don't want to use malloc then you have to declare the array as normal, such as char a[20]. However you want to do it the pointer you send to scanf() must point to valid memory large enough to hold all the characters you want to type at the keyboard. A better solution is to call fgets() so that you can limit keyboard input to a specific size.

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.