My program crashes and I can't find where. I tried debugging with printf on almost every line, but I just can't find what's wrong. I THINK that it may be at the readLine function, but I'm just completely lost.
The input file that I'm using is
*HallStudyCellarKitchen*StudyHallGarden*CellarHall*KitchenHallGarden*GardenStudyKitchen
Which means that every '*' separates a new room and then it shows where doors in that room lead to.
Code of my program
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
#define BMAX 100
struct room * rooms[MAX];
int rp; // room count
// struct room - name, array of up to 4 doors, number of doors
struct room {char * name; struct door * doors[4]; int dp;};
// struct door - name for the room it connects to, & a pointer to that room
struct door {char * name; struct room * room;};
struct door * newDoor(char * name){
struct door * d; // pointer d to the address of door
d = (struct door *) malloc(sizeof(struct door));
d->name = name; // name of new door is name
d->room = NULL; // NULL room pointer
return d;
};
struct room * newRoom(char * name){
struct room * r; // pointer r to the address of room
printf("New room is %s\n",name);
r = (struct room *) malloc(sizeof(struct room));
r->name = name; // name of new room is name
r->dp = 0; // no doors
return r;
};
showRoom(struct room * r){
int i;
printf("room name: %s\n", r->name);
for (i = 0; i < (r->dp); r++){
printf("%d %s\n", i,r->doors[i]->name);
}
}
showRooms(){
int i;
for (i = 0; i < rp; i++){
showRoom(rooms[i]);
}
}
char * readLine(FILE * fin){
char buffer[BMAX];
int i,j;
char ch;
char * l;
i = 0;
ch = getc(fin);
if (ch == EOF)
return NULL;
while (ch!='\n' && i < (BMAX - 1)){
buffer[i] = ch;
i++;
ch = getc(fin);
}
if (ch != '\n')
while (ch != '\n')
ch = getc(fin);
buffer[i] = '\0';
l = malloc((i+1) * sizeof(char));
for (j = 0; j <= i; j++)
l[j] = buffer[j];
l[j] = '\0';
return l;
}
readRooms(FILE * fin)
{ char * l;
rp = 0;
// printf("3"); fflush(stdout);
while((l = readLine(fin)) != NULL)
{
if(rp > MAX)
{
printf("it's too many rooms\n");
exit(0);
}
//printf("%s",l);
rooms[rp] = newRoom(l);
//l = readLine(fin);
if (strncmp(l,"*")==0){
//printf("2"); fflush(stdout);
rp++;
}
while(strncmp(l,"*")!=0)
{
//printf("1"); fflush(stdout);
if((rooms[rp] -> dp) > 4)
{ printf("it's too many doors\n");
exit(0);
}
rooms[rp] -> doors[rooms[rp] -> dp] = newDoor(l);
rooms[rp] -> dp++;
l = readLine(fin);
}
//rooms[rp] -> dp = 0;
//rp++;
//l = readLine(fin);
}
}
connect()
{ int i,j,k;
for(i = 0; i < rp; i++)
for(j = 0; j < rooms[i]->dp; j++)
{ for(k = 0; k < rp; k++)
if(strcmp(rooms[k]->name,rooms[i]->doors[j]->name) == 0)
{ rooms[i]->doors[j]->room = rooms[k];
break;
}
if(k == rp)
{ printf("can't find %s\n",rooms[i]->doors[j]->name);
exit(0);
}
}
}
int main(int argc,char ** argv){
FILE * fin;
struct room * r; // current room
// struct door * d;
int d;
if((fin=fopen(argv[1],"r"))==NULL)
{ printf("cannot open %s\n",argv[1]);
exit(EXIT_FAILURE);
}
printf("11"); fflush(stdout);
readRooms(fin);
printf("22");
fclose(fin);
showRooms();
connect();
r = rooms[0];
while(1)
{ showRoom(r);
printf("enter door number> ");
scanf("%d",&d);
if(d >= (r->dp))
printf("bad door number\n");
else
r = r->doors[d]->room;
}
return EXIT_SUCCESS;
}
What might be causing the crash, and how can I resolve it?
The readLine function does look a bit error prone:
char * readLine(FILE * fin){
char buffer[BMAX];
int i,j;
char ch;
char * l;
i = 0;
ch = getc(fin);
if (ch == EOF)
return NULL;
while (ch!='\n' && i < (BMAX - 1)){
buffer[i] = ch;
i++;
ch = getc(fin);
}
// The test on the next line is not necessary: it will be caught
// by the first run of the following while loop.
if (ch != '\n')
while (ch != '\n')
ch = getc(fin);
buffer[i] = '\0';
// Allocate a region of memory of (probably) i+1 bytes
l = malloc((i+1) * sizeof(char));
// j will range from zero to i, terminating when j = i+1
for (j = 0; j <= i; j++)
l[j] = buffer[j];
// j now equals i+1 and l[j] is one beyond the size of the memory allocated at l.
l[j] = '\0';
return l;
}
Fix this by changing the condition on the loop
// j will range from zero to i-1, terminating when j = i
for (j = 0; j < i; j++)
l[j] = buffer[j];
// j now equals i and l[j] is the last element of the memory allocated at l.
l[j] = '\0';
Notes:
1) You should always check the return value from malloc.
2) The actual size of memory allocated by malloc may be more than that requested, depending on the heap implementation, the processor architecture and the actual library function used.
Also, you should be nice and call free on every pointer that you have assigned using malloc. The memory will be cleaned up eventually when the process terminates (if it's running in a fairly common OS), but it's good practice to do housekeeping, especially on those temporary buffers returned by readLine.
You may also want to take a look at the if(rp > MAX) line in readRooms and see if could cause an over run for the 11th room.
Incorrect usage of int strncmp(const char *s1, const char *s2, size_t n); in 2 places. Suggest replacing with strcmp().
// if (strncmp(l,"*")==0)
if (strcmp(l,"*")==0)
If your compiler did not warning about this, either enable more warnings or get a new compiler.
Suspect off by 1
// if (rp > MAX)
if (rp >= MAX)
// if((rooms[rp] -> dp) > 4)
if ((rooms[rp] -> dp) >= 4)
Minor fixes to readLine() follow
char * readLine(FILE * fin){
char buffer[BMAX];
int i,j;
// char ch;
int ch;
char * l;
i = 0;
ch = getc(fin);
if (ch == EOF)
return NULL;
// while (ch!='\n' && i < (BMAX - 1)){
while (ch!='\n' && ch!= EOF && i < (BMAX - 1)){
buffer[i] = ch;
i++;
ch = getc(fin);
}
if (ch != '\n')
// while (ch != '\n')
while (ch != '\n' && ch!= EOF)
ch = getc(fin);
buffer[i] = '\0';
// l = malloc((i+1) * sizeof(char));
l = malloc(i+1); // sizeof(char) is always 1
if (l == NULL) Handle_OOM();
for (j = 0; j <= i; j++)
l[j] = buffer[j];
l[j] = '\0';
return l;
}
What might be causing the crash, and how can I resolve it?
One possible (and the probable) cause of the crash is this loop in readRooms():
while(strncmp(l,"*")!=0)
{
//printf("1"); fflush(stdout);
if((rooms[rp] -> dp) > 4)
{ printf("it's too many doors\n");
exit(0);
}
rooms[rp] -> doors[rooms[rp] -> dp] = newDoor(l);
rooms[rp] -> dp++;
l = readLine(fin);
}
The l = readLine(fin) at the end of the loop sets l to NULL when EOF is reached, and this null pointer is wrongly passed to strncmp() (also which is, as chux noted, missing the third argument).
Before you can resolve that, you must make up your mind about the input file format, that is to say whether the rooms go on one and the same line, as in your question - then you can't read a single room with readLine() in its current form - or the rooms go on separate lines.
Related
I've been given a task that requires a dynamic 2D array in C, but we haven't even covered pointers yet, so I'm kind of at a loss here. I have to read some text input and store it in a 2D array, without limiting its size.
Unfortunately, Valgrind keeps throwing me an error saying that there's an uninitialised value, when the puts() function executes and sometimes it prints out some random signs. I understand that I must have omitted some indexes, but I just can't find where the issue stems from. Additionally, all advices regarding the quality of my code are very much appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#define MULT 3
#define DIV 2
char **read(int *row, int *col) {
char **input = NULL;
int row_size = 0;
int col_size = 0;
int i = 0;
int c;
while ((c = getchar()) != EOF) {
if (c != '\n') { // skip empty lines
assert(i < INT_MAX);
if (i == row_size) { // if not enough row memory, allocate more
row_size = 1 + row_size * MULT / DIV;
input = realloc(input, row_size * sizeof *input);
assert(input != NULL);
}
char *line = NULL;
int j = 0;
// I need all the rows to be of the same size (see last loop)
line = malloc(col_size * sizeof *line);
// do while, so as to not skip the first character
do {
assert(j < INT_MAX-1);
if (j == col_size) {
col_size = 1 + col_size * MULT / DIV;
line = realloc(line, col_size * sizeof *line);
assert(line != NULL);
}
line[j++] = c;
} while(((c = getchar()) != '\n') && (c != EOF));
// zero-terminate the string
if (j == col_size) {
++col_size;
line = realloc(line, col_size * sizeof *line);
line[j] = '\0';
}
input[i++] = line;
}
}
// Here I give all the lines the same length
for (int j = 0; j < i; ++j)
input[j] = realloc(input[j], col_size * sizeof *(input+j));
*row = i;
*col = col_size;
return input;
}
int main(void) {
int row_size, col_size, i, j;
char **board = read(&row_size, &col_size);
// Initialize the remaining elements of each array
for (i = 0; i < row_size; ++i) {
j = 0;
while (board[i][j] != '\0')
++j;
while (j < col_size-1)
board[i][++j] = ' ';
}
for (i = 0; i < row_size; ++i) {
puts(board[i]);
}
for (i = 0; i < row_size; ++i)
free(board[i]);
free(board);
return 0;
}
I wrote this code which reads every char of my text and puts it into my char array. My Problem is that the end of the file is not detected and so the fscanf() returns after the end of the text every time the last char until my array is filled. How can I prevent that? I am programming in C.
My Code:
int main() {
char array[50][50];
char buff;
FILE *cola = fopen("C:/Users/danie/Desktop/cola.txt", "r");
for (int i = 0; i < 50; i++) {
for (int k = 0; k < 50; k++) {
fscanf(cola, "%c", &buff);
array[i][k] = buff;
}
}
fclose(cola);
for (int i = 0; i < 50; i++) {
for (int k = 0; k < 50; k++) {
printf("%c", array[i][k]);
}
}
return 0;
}
Thank you for your help.
fscanf() returns the number of successful conversions. You should test the return value and also handle newline characters specifically:
#include <stdio.h>
int main(void) {
char array[50][50];
char buff;
FILE *cola = fopen("C:/Users/danie/Desktop/cola.txt", "r");
if (cola == NULL) {
return 1;
}
for (int i = 0; i < 50; i++) {
for (int k = 0; k < 50; k++) {
if (fscanf(cola, "%c", &buff) != 1 || buff == '\n') {
array[i][k] = '\0';
break;
}
array[i][k] = buff;
}
}
fclose(cola);
for (int i = 0; i < 50; i++) {
for (int k = 0; k < 50 && array[i][k] != '\0'; k++) {
printf("%c", array[i][k]);
}
printf("\n");
}
return 0;
}
The code can be simplified if you use getc() instead of fscanf() to read bytes from the file:
#include <stdio.h>
int main(void) {
char array[50][51];
int c, i, k, n;
FILE *cola = fopen("C:/Users/danie/Desktop/cola.txt", "r");
if (cola == NULL) {
return 1;
}
for (n = 0; n < 50; n++) {
for (k = 0; k < 50; k++) {
if ((c = getc(cola)) == EOF || c == '\n') {
break;
}
array[n][k] = c;
}
array[n][k] = '\0';
if (c == EOF && k == 0)
break;
}
fclose(cola);
for (i = 0; i < n; i++) {
puts(array[i]);
}
return 0;
}
Replace:
for (int i = 0; i < 50; i++) {
for (int k = 0; k < 50; k++) {
fscanf(cola, "%c", &buff);
array[i][k] = buff;
}
}
with:
for (int i = 0; i < 50; i++) {
for (int k = 0; k < 50; k++) {
int c = getc(cola);
if (c == EOF)
break;
array[i][k] = c;
}
}
Since buff is then unused, don't define it. Note that the return type of getc() is an int, not just a char. Always check the I/O function for success/failure. In your original code, you don't even check whether the I/O operation succeeds, which makes detecting EOF impossible.
Note that this code makes a number of assumptions that may or may not be justifiable. For example, you assume each line in the file consists of 49 characters plus a newline; you also assume you'll never need to print the information as a 'string' (your existing code does not; it prints character by character, so it is 'safe').
You might want to describe the input as:
Read up to 50 lines with up to 49 characters plus a newline in each line, storing the result in the variable array with each line being a null-terminated string.
This is more resilient to common problems (short lines, long lines, not enough lines). The code for that might be:
enum { LINE_LEN = 50, NUM_LINES = 50 };
char array[NUM_LINES][LINE_LEN];
int i;
for (i = 0; i < LINE_LEN; i++)
{
int c;
int k;
for (k = 0; k < LINE_LEN; k++)
{
c = getc(cola);
if (c == EOF || c == '\n')
break;
if (k == LINE_LEN - 1)
{
/* Too long - gobble excess */
while ((c = getc(cola)) != EOF && c != '\n')
;
break;
}
array[i][k] = c;
}
array[i][k] = '\0';
if (c == EOF)
break;
}
int num_lines = i; // You have num_lines lines of data in your array
I found one version of the Coca Cola™ ASCII art image at https://www.ascii-code.com/ascii-art/logos/coca-cola.php which looks similar to what you have in your images, but there are many other sources and variants:
__ ___ __ .ama ,
,d888a ,d88888888888ba. ,88"I) d
a88']8i a88".8"8) `"8888:88 " _a8'
.d8P' PP .d8P'.8 d) "8:88:baad8P'
,d8P' ,ama, .aa, .ama.g ,mmm d8P' 8 .8' 88):888P'
,d88' d8[ "8..a8"88 ,8I"88[ I88' d88 ]IaI" d8[
a88' dP "bm8mP8'(8'.8I 8[ d88' `" .88
,88I ]8' .d'.8 88' ,8' I[ ,88P ,ama ,ama, d8[ .ama.g
[88' I8, .d' ]8, ,88B ,d8 aI (88',88"8) d8[ "8. 88 ,8I"88[
]88 `888P' `8888" "88P"8m" I88 88[ 8[ dP "bm8m88[.8I 8[
]88, _,,aaaaaa,_ I88 8" 8 ]P' .d' 88 88' ,8' I[
`888a,. ,aadd88888888888bma. )88, ,]I I8, .d' )88a8B ,d8 aI
"888888PP"' `8""""""8 "888PP' `888P' `88P"88P"8m"
This file's longest line is the first at 67 characters plus newline; the shortest is 61 characters plus newline. The file only has 13 lines and 845 characters (LF line endings) in total. Thus, your program is ill-equipped to deal with this particular data file. It looks for 2,500 characters, and won't get them.
My complete test code was rigged to read from standard input, rather than a fixed file name.
#include <stdio.h>
int main(void)
{
FILE *cola = stdin;
enum { LINE_LEN = 80, NUM_LINES = 50 };
char array[NUM_LINES][LINE_LEN];
int i; // Need value of i after loop
for (i = 0; i < NUM_LINES; i++)
{
int c; // Need value of c after loop
int k;
for (k = 0; k < LINE_LEN; k++)
{
c = getc(cola);
if (c == EOF || c == '\n')
break;
if (k == LINE_LEN - 1)
{
/* Too long - gobble excess */
while ((c = getc(cola)) != EOF && c != '\n')
;
break;
}
array[i][k] = c;
}
array[i][k] = '\0';
if (c == EOF)
break;
}
int num_lines = i; // You have num_lines lines of data in your array
for (i = 0; i < num_lines; i++)
puts(array[i]);
return 0;
}
I tested it on the data file shown, with an empty line at the end, and with a couple of lines containing more than 79 characters after the blank line. It handled all those special cases correctly. Note that handling user input is hard; handling perverse user input is harder. The code is less compact. You could change the rules and then change the code to match. I'm not sure this is the most minimal way to code this; it does work, however. It might be better to have a function to handle the inner input loop; the outer loop could test the return value from that function. This would cut down on the special case handling.
#include <assert.h>
#include <limits.h>
#include <stdio.h>
static int read_line(FILE *fp, size_t buflen, char *buffer)
{
assert(buflen < INT_MAX);
int c; // Need value of c after loop
size_t k; // Need value of k after loop
for (k = 0; k < buflen; k++)
{
if ((c = getc(fp)) == EOF || c == '\n')
break;
if (k == buflen - 1)
{
/* Too long - gobble excess */
while ((c = getc(fp)) != EOF && c != '\n')
;
break;
}
buffer[k] = c;
}
buffer[k] = '\0';
return (k == 0 && c == EOF) ? EOF : (int)k;
}
int main(void)
{
enum { LINE_LEN = 80, NUM_LINES = 50 };
char array[NUM_LINES][LINE_LEN];
int i;
for (i = 0; i < NUM_LINES; i++)
{
if (read_line(stdin, LINE_LEN, array[i]) == EOF)
break;
}
int num_lines = i;
for (i = 0; i < num_lines; i++)
puts(array[i]);
return 0;
}
This produces the same output from the same input as the previous version.
int main() {
//char array[50][50];
char buff;
int t;
FILE *cola = fopen("C:/Users/danie/Desktop/cola.txt", "r");
if (cola == NULL)
{
printf("Cannot open file \n");
exit(0);
}
while (1) {
t = fgetc(cola);
if (t == EOF)
break;
buff = t;
printf("%c", buff);
}
fclose(cola);
return 0;
}
I am trying to read in the Constitution as a text file from the command line into my program to print out the lines in reverse order. My for loop looks like this:
for(int i = 0; i >= 0; i--) {
if(strings[i] == '\0') //counts through array until it finds a line break
{
break;
}
printf("%s", strings[i]);
}
When the program runs, the only thing that prints is the first line of the Constitution. If I modify my for loop to increment i, the program runs smoothly and outputs the Constitution like normal, and therefore I believe my entire problem is summed up in this for loop. This is the rest of my program for reference.
int clearBuffer() {
char junk;
while((junk = getchar()) != feof(stdin) && junk != '\n');
return 0;
}
int getAline(char ** bufferPointer, int * sizePointer){
char * buffer = *bufferPointer;
int count = 0;
int size = *sizePointer;
while(!feof(stdin)){
if(count >= size - 1){
char * tempBuffer = (char * )malloc(size * 10);
//strcpy(tempBuffer, buffer );
for (int i = 0; i < size; i++){
tempBuffer[i] = buffer[i];
//putchar(tempBuffer[i]);
}
free(buffer);
buffer = tempBuffer;
size *= 10;
}
buffer[count] = getchar();
if(buffer[count] == '\n'){
break;
}
if(buffer[count] == EOF){
buffer[count] = '\0';
break;
}
count++;
}
*bufferPointer = buffer;
*sizePointer = size;
return count-1;
}
int main(){
char * buffer;
char * strings[1000];
int arrayCount =0;
int size = 10;
while(!feof(stdin))
{
buffer= (char*) malloc(size);
getAline(&buffer, &size);
strings[arrayCount++] = buffer;
}
for(int i = 0; i >= 0; i--) {
if(strings[i] == '\0'){
break;
}
printf("%s", strings[i]);
}
return 0;
}
When you reverse a loop's iteration direction, you also have to reverse the beginning and ending values.
for(int i = arrayCount-1; i >= 0; i--)
Now, this loop starts at the end, then works back down to the beginning of the array.
Make int i equal the number of lines in the Constitution instead of 0 that should do it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I was trying to solve CountAndSay problem at one of the online coding site but I am not able to get why my program is printing NULL. I am sure I am doing some conceptual mistake but not getting it.
Here is my code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* countAndSay(int A) {
int i,j,k,f,count;
char a;
char *c = (char*)malloc(sizeof(char)*100);
char *temp = (char*)malloc(sizeof(char)*100);
c[0] = 1;c[1] = '\0';
for(k=2; k<=A; k++)
{
for(i=0, j=0; i<strlen(c); i++)
{
a = c[i];
count = 1;
i++;
while(c[i] != '\0')
{
if(c[i]==a)
{
count++;
i++;
}
else if(c[i] != a)
{
i--;
break;
}
else
{
break;
}
}
temp[j] = count;
temp[j+1] = a;
j += 2;
}
*(temp+j) = '\0';
if(k<A)
{
for(j=0; j<strlen(temp); j++)
{
c[j] = temp[j];
}
c[j] = '\0';
}
}
return temp;
}
int main(void) {
// your code goes here
char *c = countAndSay(8);
printf("%s\n",c);
return 0;
}
The idea is not that bad, the main errors are the mix-up of numerical digits and characters as shown in the comments.
Also: if you use dynamic memory, than use dynamic memory. If you only want to use a fixed small amount you should use the stack instead, e.g.: c[100], but that came up in the comments, too. You also need only one piece of memory. Here is a working example based on your code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// ALL CHECKS OMMITTED!
char *countAndSay(int A)
{
int k, count, j;
// "i" gets compared against the output of
// strlen() which is of type size_t
size_t i;
char a;
// Seed needs two bytes of memory
char *c = malloc(2);
// Another pointer, pointing to the same memory later.
// Set to NULL to avoid an extra malloc()
char *temp = NULL;
// a temporary pointer needed for realloc()-ing
char *cp;
// fill c with seed
c[0] = '1';
c[1] = '\0';
if (A == 1) {
return c;
}
// assuming 1-based input, that is: the first
// entry of the sequence is numbered 1 (one)
for (k = 2; k <= A; k++) {
// Memory needed is twice the size of
// the former entry at most.
// (Averages to Conway's constant but that
// number is not usable here, it is only a limit)
cp = realloc(temp, strlen(c) * 2 + 1);
temp = cp;
for (i = 0, j = 0; i < strlen(c); i++) {
//printf("A i = %zu, j = %zu\n",i,j);
a = c[i];
count = 1;
i++;
while (c[i] != '\0') {
if (c[i] == a) {
count++;
i++;
} else {
i--;
break;
}
}
temp[j++] = count + '0';
temp[j++] = a;
//printf("B i = %zu, j = %zu\n",i,j-1)
//printf("B i = %zu, j = %zu\n",i,j);
}
temp[j] = '\0';
if (k < A) {
// Just point "c" to the new sequence in "temp".
// Why does this work and temp doesn't overwrite c later?
// Or does it *not* always work and fails at one point?
// A mystery! Try to find it out! Some hints in the code.
c = temp;
temp = NULL;
}
// intermediate results:
//printf("%s\n\n",c);
}
return temp;
}
int main(int argc, char **argv)
{
// your code goes here
char *c = countAndSay(atoi(argv[1]));
printf("%s\n", c);
free(c);
return 0;
}
To get a way to check for sequences not in the list over at OEIS, I rummaged around in my attic and found this little "gem":
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
char *conway(char *s)
{
char *seq;
char c;
size_t len, count, i = 0;
len = strlen(s);
/*
* Worst case is twice as large as the input, e.g.:
* 1 -> 11
* 21 -> 1211
*/
seq = malloc(len * 2 + 1);
if (seq == NULL) {
return NULL;
}
while (len) {
// counter for occurrences of ...
count = 0;
// ... this character
c = s[0];
// as long as the string "s"
while (*s != '\0' && *s == c) {
// move pointer to next character
s++;
// increment counter
count++;
// decrement the length of the string
len--;
}
// to keep it simple, fail if c > 9
// but that cannot happen with a seed of 1
// which is used here.
// For other seeds it might be necessary to
// use a map with the higher digits as characters.
// If it is not possible to fit it into a
// character, the approach with a C-string is
// obviously not reasonable anymore.
if (count > 9) {
free(seq);
return NULL;
}
// append counter as a character
seq[i++] = (char) (count + '0');
// append character "c" from above
seq[i++] = c;
}
// return a proper C-string
seq[i] = '\0';
return seq;
}
int main(int argc, char **argv)
{
long i, n;
char *seq0, *seq1;
if (argc != 2) {
fprintf(stderr, "Usage: %s n>0\n", argv[0]);
exit(EXIT_FAILURE);
}
// reset errno, just in case
errno = 0;
// get amount from commandline
n = strtol(argv[1], NULL, 0);
if ((errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))
|| (errno != 0 && n == 0)) {
fprintf(stderr, "strtol failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (n <= 0) {
fprintf(stderr, "Usage: %s n>0\n", argv[0]);
exit(EXIT_FAILURE);
}
// allocate space for seed value "1" plus '\0'
// If the seed is changed the limit in the conway() function
// above might need a change.
seq0 = malloc(2);
if (seq0 == NULL) {
fprintf(stderr, "malloc() failed to allocate a measly 2 bytes!?\n");
exit(EXIT_FAILURE);
}
// put the initial value into the freshly allocated memory
strcpy(seq0, "1");
// print it, nicely formatted
/*
* putc('1', stdout);
* if (n == 1) {
* putc('\n', stdout);
* free(seq0);
* exit(EXIT_SUCCESS);
* } else {
* printf(", ");
* }
*/
if (n == 1) {
puts("1");
free(seq0);
exit(EXIT_SUCCESS);
}
// adjust count
n--;
for (i = 0; i < n; i++) {
// compute conway sequence as a recursion
seq1 = conway(seq0);
if (seq1 == NULL) {
fprintf(stderr, "conway() failed, probably because malloc() failed\n");
exit(EXIT_FAILURE);
}
// make room
free(seq0);
seq0 = NULL;
// print sequence, comma separated
// printf("%s%s", seq1, (i < n - 1) ? "," : "\n");
// or print sequence and length of sequence, line separated
// printf("%zu: %s%s", strlen(seq1), seq1, (i < n-1) ? "\n\n" : "\n");
// print the endresult only
if (i == n - 1) {
printf("%s\n", seq1);
}
// reuse seq0
seq0 = seq1;
// not necessary but deemed good style by some
// although frowned upon by others
seq1 = NULL;
}
// free the last memory
free(seq0);
exit(EXIT_SUCCESS);
}
I use the following code to load a large hashtable on the heap.
However i dont know the right syntax to search the whole array after loading.
I suppose i can add a strcmp in the last J loop??
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int lines_allocated = 128;
int max_line_len = 100;
/* Allocate lines of text */
char **words = (char **)malloc(sizeof(char*)*lines_allocated);
if (words==NULL)
{
fprintf(stderr,"Out of memory (1).\n");
exit(1);
}
FILE *fp = fopen("hashtable.txt", "r");
if (fp == NULL)
{
fprintf(stderr,"Error opening file.\n");
exit(2);
}
int i;
for (i = 0; 1; i++)
{
int j;
/* Have we gone over our line allocation? */
if (i >= lines_allocated)
{
int new_size;
/* Double our allocation and re-allocate */
new_size = lines_allocated*2;
words = (char **)realloc(words,sizeof(char*)*new_size);
if (words == NULL)
{
fprintf(stderr,"Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
/* Allocate space for the next line */
words[i] = malloc(max_line_len);
if (words[i] == NULL)
{
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
if (fgets(words[i], max_line_len-1,fp) == NULL)
break;
/* Get rid of CR or LF at end of line */
for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r')j--);
words[i][j] = '\0';
}
int j;
for(j = 0; j < i; j++)
printf("%s\n", words[j]);
// Search for a string e.g "ffffffffff999999999922222222227777777777" in words[]
//
//strcmp ( string, words[j])????
//
//
//
/* Good practice to free memory */
for (;i>=0;i--)
free(words[i]);
free(words);
return 0;
}
i have tried to implement strcmp in the loop but then the program segfaults.
Used this example :
/* what is i? the number of items used in the array? */
for(x = 0; x < i; x++) {
if ( strcmp( new_name, names[x] ) == 0 ){
/* match, x is the index */
return x;
}
}
/* here with no match */
return -1;
As i was indenting your code i saw:
for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r')j--);
I think you meant:
for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r'); j--)
----^^^^^^^
That while would never execute what it had between braces.
You have a problem here:
for (j=strlen(words[i]) - 1; j>=0 && (words[i][j]=='\n' || words[i][j]=='\r'); j--);
words[i][j]='\0';
j is off by one. You shoud increment jjust after the loop:
for (j=strlen(words[i])-1; j>=0 && (words[i][j]=='\n' || words[i][j]=='\r'); j--)
{
}
words[i][j + 1] = '\0';
The extra { } is there only for readibility purposes. Otherwise if you forget the ; after the for, your code will compile correctly but with words[i][j +1 ]='\0'; being part of the loop.
Other off by one problem:
You must decrement i here:
/* Good practice to free memory */
i-- ; // <<<< decrement i here
for (;i>=0;i--)
free(words[i]);
Problem with strcmp:
Concerning your strcmp problem you probably want this:
int j;
for(j = 0; j < i; j++)
{
printf("%s\n", words[j]);
if (strcmp (words[j], "word we are lookong for") == 0)
{
// found it
}
}