Eliminating this seg fault

A

Albert

As a response to the following problem,

Speed Cameras
Input File: camin.txt
Output File: camout.txt
Time Limit: 1 second

As director of the Safe Driving Ministry, you are faced with a dilemma.
None of your speed cameras are working, but your department desperately
needs the extra revenue for their new Super Espresso Coffee Deluxe. With
determination, you and your co-workers head down to the highway to
balance the budget.

You order your officers Bernard and Peter to stand at either end of a
tunnel running through a hill. Bernard records the order in which cars
enter the tunnel, and Peter records the order in which cars exit the
tunnel. There is a strict no-overtaking rule in the tunnel, so given
this information you can reliably pull over and fine a certain number of
drivers at your roadblock further down the highway.

Given Bernard's and Peter's lists, you must write a program that
determines how many cars you can claim made an illegal overtake with
certainty.

Input

The first line of input will contain a single integer N representing the
number of cars that drove through the tunnel ( 1 <= N <= 1000).

Following this will be N lines describing Bernard's list. Each line will
contain the number plate of a car that entered the tunnel, in order from
first entry to last entry. Following this will be N lines describing
Peter's list, each line containing the number plate of a car that exited
the tunnel in order from first exit to last exit.

Each number plate consists of at least six and at most eight characters.
Only capital letters (A-Z) and digits (0-9) will be used. No two cars
will have the same number plate.

Output

Output should consist of a single integer representing how many drivers
you know with certainty have made an overtake.

Sample Input 1

4
109DLY
SSH2ANU
VOLVO76
REDCAR
REDCAR
109DLY
SSH2ANU
VOLVO76

Sample Output 1

1

Sample Input 2

5
TRS80MCX
IOI2004
DEB18N
REDCAR
REGINA41
IOI2004
REGINA41
REDCAR
TRS80MCX
DEB18N

Sample Output 2

3

Sample Input 3

5
REDCAR
L0LL1P0P
UQ9396
109DLY
B1LKENT
B1LKENT
UQ9396
REDCAR
L0LL1P0P
109DLY

Sample Output 3

2

I'm getting a seg fault with Input 2 and

/* This program reads in two lists of car licence plates and uses this
to determine how many cars have overtaken *with certainty*. It simulates
the overtakings starting with the car at the top of out list, modifying
the in list until the bottom of the out list is reached. */

#include <stdio.h>
#include <string.h>

enum { MAXCARS = 1000 };

FILE *in, *out;
int ncars;
char *inlist[MAXCARS], *outlist[MAXCARS]; /* stores the list of license
plates before the cars enter the tunnel and after. */

int find(char *s)
{
int i;
for (i = 1; i <= ncars; i++)
if (strcmp(inlist, s) == 0)
return i;
}

int main(void)
{
in = fopen("speedin.txt", "r"); /* CHECK */
out = fopen("speedout.txt", "w");

fscanf(in, "%d", &ncars);
int i;
for (i = 1; i <= ncars; i++)
fscanf(in, "%s", inlist);
printf("%d\n", find("TRS80MCX"));
return 0;
}

Running it under GDB reveals an issue with strcmp...What is wrong?
 
B

Ben Bacarisse

Albert said:
As a response to the following problem,
#include <stdio.h>
#include <string.h>

enum { MAXCARS = 1000 };

FILE *in, *out;
int ncars;
char *inlist[MAXCARS], *outlist[MAXCARS]; /* stores the list of
license plates before the cars enter the tunnel and after. */
int main(void)
{
in = fopen("speedin.txt", "r"); /* CHECK */
out = fopen("speedout.txt", "w");

fscanf(in, "%d", &ncars);
int i;
for (i = 1; i <= ncars; i++)
fscanf(in, "%s", inlist);
printf("%d\n", find("TRS80MCX"));
return 0;
}

Running it under GDB reveals an issue with strcmp...What is wrong?


The problem is that there is nowhere to put the strings. inlist is
NULL so the fscanf has nowhere to put what it reads. Since the
strings are small and memory seems not to be a constraint in this
case, you can just make the lists be arrays of char arrays:

char inlist[MAXCARS][9];

You can get away with 8 if you are very careful about unterminated
"strings". I have to put it in quotes because it is not really a
string if it has no null character at the end.
 
A

Albert

Chris said:
Array indicies start at 0 in C ?

I believe all the inputs of strings are written to each element of the
in array starting at 0 though. My 1 based indexing in the for loops is
consistent...
 
A

Albert

Ben said:
<snip>
The problem is that there is nowhere to put the strings. inlist is
NULL so the fscanf has nowhere to put what it reads. Since the
strings are small and memory seems not to be a constraint in this
case, you can just make the lists be arrays of char arrays:

char inlist[MAXCARS][9];
<snip>


Thanks - have a 100% solution now.
 
P

Phil Carmody

Albert said:
int main(void)
{
in = fopen("speedin.txt", "r"); /* CHECK */
out = fopen("speedout.txt", "w");

fscanf(in, "%d", &ncars);
int i;
for (i = 1; i <= ncars; i++)
fscanf(in, "%s", inlist);
printf("%d\n", find("TRS80MCX"));
return 0;
}

Running it under GDB reveals an issue with strcmp...What is wrong?


Your code.

Quite horrifically.

Try making your pointers actually point to something before using
them.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,086
Latest member
ChelseaAmi

Latest Threads

Top