0

Im trying to get a programme im writing to compare what a user inputs to a word stored in a pointer. The code is below

int c_s(char*, char*);

int main()
{
printf("Hate crime reporting system\n\n\n");

printf("If the crime you are reporting is an emergency,\nplease call 999, do 
not proceed any further with this form\n\n\n\nPlease press enter to confirm 
you have read the above and continue\n");
char enter = 0;
while (enter != '\r' && enter != '\n') { enter = getchar(); }


int a;
long long int g;
char *b, *e;
char *c = "witness";
char *d = "yes";
*c = (long long int) &g;


 printf("Are you a victim or witness of the crime?\nPlease answer 
 victim/witness\n");}
 scanf("%s", b);  
    int r = strcmp (b, c);
    if(r == 0){    
    printf("Do you know who the witness is? Please answer yes/no\n");}
    scanf("%s", d);
       int f = strcmp(e, d);
       if (d = "no") goto NEXT;
       if(e == 0){
         printf("Please enter their details including phone number and 
address");}

NEXT:

When a user answers "witness" to the question "are you a victim or witness", i want the code to continue and ask the next question, and then the same again if they answer "yes or "no" to the question "do you know who the witness is?". When i run this code i get an overflow error. Im new to coding so if anyone could provide sample code on how to make this work i would greatly appreciate it. Im not sure whether im using the pointer wrong or whether i should be using an array?

Can someone also please explain how i would make this an if statement, meaning if the user types in "victim" as the answer to the above question, the programme continues to "NEXT:"

2
  • 2
    There is no memory associated with pointer b. You cannot put data into that pointer until there is memory for it. Commented Apr 3, 2018 at 14:24
  • 3
    *c = (long long int) &g; - yikes Commented Apr 3, 2018 at 14:25

5 Answers 5

1

Before we even get to your string problems, look at this line:

printf("Are you a victim or witness of the crime?\nPlease answer victim/witness\n");}

It ends with a }, which seems to match the start of main. At this point, you've ended the main function, and the rest of your code is not even part of a function. This likely won't even compile.


To get string input, you must make sure there is memory available for the string:

char *b;     // WRONG: Uninitialized pointer.
char b[500]; // CORRECT: b can hold up to 499 bytes (+ one \0 at the end)
scanf("%s", b); // Get input into the memory you declared.

Also, you must make sure the memory is writable:
this problem is more subtle, and many C programmers have a hard time with this

char *d = "yes";     // WRONG: "yes" is a constant string in your program, it cannot be changed.
char d[200] = "yes"; // CORRECT: d is 200 bytes, and is initially set to "yes".  It can be changed later.

Even after using strcmp correctly in parts of your program, you still tried to do:

if (d = "no") goto NEXT;
  • You cannot do string comparisons this way in C.
  • Even if you could, it would use a == not a single =
  • Never use goto (unless you wish to be attacked by a velociraptor).

On this line, you reference variable e, but you never set it to a value.

int f = strcmp(e, d);

On this line, you treat e like it is an integer, but it was declared char*
(and it still does not have a valid value)

if(e == 0) {
Sign up to request clarification or add additional context in comments.

Comments

0

A string can never be "stored in a pointer", and that's exactly your problem. You just feed scanf() your uninitialized pointer b. This has a random value and doesn't point to any usable memory, so when scanf() tries to write there, your program crashes.

Solution: provide memory to write to for scanf(). Either declare b as an array (e.g. char b[256];) or use malloc() to dynamically allocate memory. Then make sure you don't write more bytes than you reserved. scanf("%s", ...) will always overflow any buffer if the input is larger, so use a field width with scanf() -- see the manual page.

Better yet, don't use scanf(). You might want to read my beginners' guide away from scanf for more information.

Comments

0

b is an uninitialised pointer. The effect of your scanf is therefore undefined behaviour. Something like

char b[100];

Will allocate 100 bytes of space on the stack to b. You will then need to take steps to make sure that no more than 99 bytes are read from stdin by the scanf.

c is first initialised to point to the string "witness" which is good, but this line:

*c = (long long int) &g;

Sets the first byte to the lowest byte of the address of g. It's completely unnecessary, get rid of it.

The next scanf scansd into d which is set to a literal string"yes" I think this is undefined behaviour too, but it's wrong, in any case. You should be allocating some space to e (as per b) and scanning into that.

Your comparison after the strcmp should use f not e and the printf should really have a \n on it.

By the way, this example shows the value of using descriptive variable names. In two places, you have used the wrong variable but if you had named them descriptively e.g. f => comparisonResult you might have avoided confusion.

Comments

0

A few notes to keep you going: 1. When declraing a string, you must specify it's size. A string is a an array of chars, and an declaration of an array in c should be given with a size (with at least the number of chars+1), as so:

char c[10] = "witness";
  1. strcmp returns 0 if strings match.

Comments

0

this line is wrong as string cannot be stored in the pointer

char *c = "witness";
char *d = "yes";

to do so you need to first assign the memory for that pointer

char *c=(char*)malloc(strlen("witness")*sizeof(char));
char *d=(char*)malloc(strlen("yes")*sizeof(char));
strcpy(c,"witness");
strcpy(c,"yes");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.