1
Exit status -1
Yuvikiit (0)

This is my code and I am not getting why is it showing exit status -1.
Please answer:)
#include<stdio.h>
#include<string.h>
int main()
{
int i;
char c,d,e;
scanf("%s",c); //to take in the 1st string
scanf("%c",&d);//after string the newline char needs to get in the buffer
scanf("%s",e); //to take in another string
printf("%s\n%s",c,e); //to print both the strings
return 0;
}

You are viewing a single comment. View All
Answered by mwilki7 (111) [earned 5 cycles]
View Answer
1
mwilki7 (111)

@Yuvikiit I see, repl formatted your asterisks out.
Try one backwards tick mark ` for inline code, or three for code with multiple lines.

As for your code, when you declare a char pointer:
char *c;

You need space to store stuff in it.
Arrays do this automatically because you tell it from the start how much space it should have like so:
char c[20]; // space for 20 chars already made at compile

To do this with pointers:

char *c; // declare pointer
c = (char*)malloc(sizeof(char) * 20); // make space for 20 chars for this pointer

scanf("%s", c); // now I can use it, just make sure to limit your input to 20 or 
                // whatever you decide to change 20 to

OR you can steal the space made by an array if you don't want to use malloc

like so:

char *c;
char arr[20];

c = arr; // use arr's space for c

scanf("%s", c); // read to c