0

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

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Ancient Dragon
0

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.

1

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.

Edited by Ancient Dragon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.