Skip to content
← Back to Community
Exit status -1
Profile icon
Yuvikiit

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;
}

![2019-06-14]
(https://storage.googleapis.com/replit/images/1560491459712_ab9b557011bbd898307349b38c7d2c6d.pn)

Answered by mwilki7 [earned 5 cycles]
View Answer
Voters
Profile icon
JenniferY2
Profile icon
NightBlinky
Profile icon
Nicoooi
Profile icon
NihaalPode
Profile icon
KevinGhobrial
Profile icon
NA13
Profile icon
Gabriele05
Profile icon
Yuvikiit
Comments
hotnewtop
Profile icon
mwilki7

%s is only for strings
char c,d,e
you've got 3 chars

try instead:

char c[20]; char d[20]; char e[20];

and use scanf like so:

scanf("%s", c); scanf("%s", d); scanf("%s", e);

ampersand(&) is only for non-arrays like char, int, float
but not char[], int[], float[] etc...

Profile icon
Yuvikiit

@mwilki7 Sorry I dont know about repl putting of code, I just pasted my code down and that removed * before the c and e thus I am pasting the pic of it as well.
Sorry for that mistake so *c is a character pointer let me know why that didnt work.

Profile icon
mwilki7

@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
Profile icon
mwilki7

Here is a sample program that does this:

Program (allocate with array sharing)

#include <stdio.h> int main() { char *c; // declare pointer char arr[20]; // declare array c = arr; // array shares space with c scanf("%s", c);// write stuff to c printf("c: %s, arr: %s",c,arr); // print results return 0; }

Input

test

Output

c: test, arr: test

Profile icon
Yuvikiit

@mwilki7 Sankyu(Thanks) Sir for the reply, just let me know one more thing
Like why this works for a single string.
and how does it work:
I thought in the manner that being a character pointer
the pointer would store 1 character as I am entering and then will move to next byte for latter entry and as soon as I press enter it puts a '\0' at the end and completes the string,

And I also don't know like why this should be a problem even
is it because that the pointer has some garbage address stored in it and it is not a good programming practice to store our string at some garbage place though, the memory chunks might be continuous (as the char pointer
does that bit by default.)

//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",c); //to print both the strings return 0; }

and
let me know is it some compiler dependant thing because I tried the same thing in Codeblocks and it didn't work.

Thanks once again for the help.

Profile icon
mwilki7

@Yuvikiit
The only thing that's allocated for pointers up front:
char *c; is the address for the variable 'c' itself.

What 'c' is currently pointing at...who knows. For the sake of deterministic behavior, it may be a good idea to initialize it to NULL

Don't do:

char *c = NULL; scanf("%s", c); // bad! writing to NULL

do:

char *c = NULL; c = (char*)malloc(sizeof(char) * 20); // make space first scanf("%s", c); // now I can write to 'c'
Profile icon
Yuvikiit

@mwilki7 Thank you for help I will try to be a good programmer with this practice.

Profile icon
BradlyNoe

Pls help me for this error