EXC BAD ACCESS TREAD in c - c

it gives me EXC_BAD_ACCESS (code=1, address=0xc) at the strcmp in the while (strcmp(parola, temp->next->parola) picture of the xcode debugging session
if (temp->next != NULL){
if (strcmp(temp->next->parola, "\0") != 0 && strcmp(parola, "\0") != 0){
while (strcmp(parola, temp->next->parola) != 0){
if(temp->next != NULL){
if (strcmp(temp->next->parola, "\0") != 0 && strcmp(parola, "\0") != 0){
temp = temp->next;
} else {break;}
} else {break;}
}
}
}

if (temp->next != NULL){
if (strcmp(temp->next->parola, "\0") != 0 && strcmp(parola, "\0") != 0){
while (strcmp(parola, temp->next->parola) != 0){
if (strcmp(temp->next->parola, "\0") != 0 && strcmp(parola, "\0") != 0){
temp = temp->next;
if(temp->next != NULL){
} else {break;}
} else {break;}
}
}
}

Related

How to skip NULL file

My code traverses through command line arguments. if the command line contains "alice.txt" index zero of an empty array is filled with "alice.txt" This also applies to "anh.txt" if it is present, then index 1 is filled with "anh.txt". When i type in the command line ./wc alice.txt anh.txt it will print out words line and characters of both files. however if I just do /wc anh.txt i will get "NULL FILE" I suspect this is because of index zero being NULL, I wrote it so it would print "NULL FILE" and then exit the program. How can I modify my code so it doesn't exit, rather skips to the next index? This is what I have for code:
int main(int argc, char **argv)
{
int argArray[argc];
char *nameArray[argc];
for (int i = 1; i < argc; i ++)
{
if (strcmp(argv[i], "-l") == 0)
{
argArray[0] = 1;
}
if (strcmp(argv[i], "-w") == 0)
{
argArray[1] = 1;
}
if (strcmp(argv[i], "-c") == 0)
{
argArray[2] = 1;
}
if (strcmp(argv[i], "alice.txt") == 0)
{
nameArray[0] = "alice.txt";
}
if (strcmp(argv[i], "anh.txt") == 0)
{
nameArray[1] = "anh.txt";
}
if ((strcmp(argv[i], "-l") != 0) && (strcmp(argv[i], "-c") != 0 ) && (strcmp(argv[i], "-w") != 0 ) && (strcmp(argv[i], "-w") != 0) && (strcmp(argv[i], "alice.txt") != 0) && (strcmp(argv[i], "anh.txt") != 0))
{
printf("%s is an invalid argument or file\n", argv[i]);
}
}
if ((argArray[0] != 1) && (argArray[1] != 1) && (argArray[2] != 1))
{
argArray[0] = 1;
argArray[1] = 1;
argArray[2] = 1;
}
int *myArrayAlice = get_counts2(nameArray[0]);
print_counts(argArray, myArrayAlice, *nameArray);
printf(" alice.txt\n");
int *myArrayAnh = get_counts(nameArray[1]);
print_counts(argArray,myArrayAnh, *nameArray);
printf(" anh.txt\n");
int *myArrayTotal2 = get_counts2(nameArray[1]);
print_counts(argArray, myArrayTotal2, *nameArray);
printf(" total\n");
return 0;
}
int *get_counts(char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL)
{
printf("NULL FILE");
//exit(1);
}
int c;
bool whitespace = true;
int *arr = calloc(3,sizeof(int));
for(;;)
{
c = fgetc(file);
if (c == EOF)
{
break;
}
else if (c == '\n')
{
arr[0]++;
}
else if (whitespace && !isspace(c))
{
arr[1]++;
whitespace = false;
}
else if (!whitespace && isspace(c))
{
whitespace = true;
}
arr[2]++;
}
fclose(file);
return arr;
}
int *get_counts2(char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL)
{
printf("NULL FILE");
//exit(1);
}
int c;
bool whitespace = true;
static int arr[3] = {0,0,0};
for(;;)
{
c = fgetc(file);
if (c == EOF)
{
break;
}
else if (c == '\n')
{
arr[0]++;
}
else if (whitespace && !isspace(c))
{
arr[1]++;
whitespace = false;
}
else if (!whitespace && isspace(c))
{
whitespace = true;
}
arr[2]++;
}
fclose(file);
return arr;
}

Problem when trying to remove a node in a tree

I have a binary tree. I'm trying to delete one of the node and all of his "children". But, my function deletes just the children and the "father" is still remaining there. whats wrong with my code? why the "father" isnt Deleted??
BinTree* familyGotVaccin(BinTree* root)
{
int isFather = 1;
BinTree* father = NULL;
BinTree* gotVaccinated = NULL;
int ID;
if (root == NULL)
return NULL;
printf("Who got vaccinated (ID)?\n");
scanf("%d", &ID);
if (checkIfExist(root, ID) == 0)
{
printf("There is no ID %d\n", ID);
return root;
}
if (root->ID == ID)
{
gotVaccinated = root;
isFather=0;
}
else
{
father = findNode(root, ID);
if (father == NULL)
return root;
if (father->left != NULL && father->left->ID == ID)
gotVaccinated = father->left;
if (father->middle != NULL && father->middle->ID == ID)
gotVaccinated = father->middle;
if (father->right != NULL && father->right->ID == ID)
gotVaccinated = father->right;
}
gotVaccinated=deleteVaccinated(gotVaccinated);
free(gotVaccinated);
if (isFather == 0)
root = NULL;
return root;
}
BinTree* deleteVaccinated(BinTree* root)
{
if (root == NULL)
return NULL;
if (root->left == NULL && root->middle == NULL && root->right == NULL)
{
printf("%s ID: %d Survived!\n", root->name, root->ID);
root = NULL;
free(root);
return NULL;
}
if (root->left != NULL)
root->left=deleteVaccinated(root->left);
if (root->middle != NULL)
root->middle=deleteVaccinated(root->middle);
if (root->right != NULL)
root->right=deleteVaccinated(root->right);
printf("%s ID: %d Survived!\n", root->name, root->ID);
root = NULL;
free(root);
return NULL;
}

bin node search in map

I try to search in a map I created. Each cell have 3 branches. When A is the closest, B after and C is the farthest. I'm trying to find the shortest route.
It is possible, for instance, that b->c->c->a is closer than a->a->a->a->a.
What should I do?
gag *pathfinder(gag *root, int choice)
{
if (root == NULL)
{
printf_s("the map didnt exits.");
getchar();
exit(1);
}
if (choice == root->index)
{
printf_s("(%d,%d)", root->x, root->y);
return root;
}
if (choice != root->index)
if (root->A != NULL)
{
if (root->A->index != NULL && root->A->index == choice)
;
else
{
pathfinder(root->A, choice);
}
}
else if (choice != root->index)
if (root->B != NULL)
{
if (root->B->index != NULL && root->B->index == choice)
; // printf_s("(%d,%d)->", root->B->x, root->B->y);
else
{
pathfinder(root->B, choice);
}
}
else if (choice != root->index)
if (root->C != NULL)
{
if (root->C->index != NULL && root->C->index == choice)
; //
printf_s("(%d,%d)->", root->C->x, root->C->y);
else
{
pathfinder(root->C, choice);
printf_s("(%d,%d)->", root->C->x, root->C->y);
}
}
}
gag* path(gag *map, gag* tree, int len) {
char temp = map->index;
gag* hold;
tree->index = temp;
static int c = -1;
int *close;
c++;
(map)->index = 'v'; // Mark as visited.
(map)->entries++;
//go A
if (!map->A || map->A->index == 'v' || map->A->entries == 3)
tree->A = NULL;
else {
hold = malloc(sizeof(gag)*len);
d_p_s(map, hold);
tree->A = path(map->A, hold, len);
}
//go B
if (!map->B || map->B->index == 'v' || map->B->entries == 3)
tree->B = NULL;
else {
hold = malloc(sizeof(gag)*len);
d_p_s(map, hold);
tree->B = path(map->B, hold, len);
}
//go C
if (!map->C || map->C->index == 'v' || map->C->entries == 3)
tree->C = NULL;
else {
hold = malloc(sizeof(gag*)*len);
d_p_s(map, hold);
tree->C = path(map->C, hold, len);
}
map->index = temp;
return tree;
}

Anybody know a more efficient way to do this?

I know this is simple. But that's what is bothering me. Is there a better way to do this?
The arrays are generally unimportant just the function of what this code's goal is what I'm trying to condense.
for(int p=0; p<28;p++){
if(p == 0){
Pile1[0] = deck[p];
}
if(p == 1 || p == 7){
if(p == 1){
Pile2[0] = deck[p];
}
if(p == 7){
Pile2[1] = deck[p];
}
}
if(p == 2 || p == 8 || p == 13){
if(p == 2){
Pile3[0] = deck[p];
}
if(p == 8){
Pile3[1] = deck[p];
}
if(p == 13){
Pile3[2] = deck[p];
}
}
if(p == 3 || p == 9 || p == 14 || p == 18){
if(p == 3){
Pile4[0] = deck[p];
}
if(p == 9){
Pile4[1] = deck[p];
}
if(p == 14){
Pile4[2] = deck[p];
}
if(p == 18){
Pile4[3] = deck[p];
}
}
if(p == 4 || p == 10 || p == 15 || p == 19 || p == 22){
if(p == 4){
Pile5[0] = deck[p];
}
if(p == 10){
Pile5[1] = deck[p];
}
if(p == 15){
Pile5[2] = deck[p];
}
if(p == 19){
Pile5[3] = deck[p];
}
if(p == 22){
Pile5[4] = deck[p];
}
}
if(p == 5 || p == 11 || p == 16 || p == 20 || p == 23 || p == 25){
if(p == 5){
Pile6[0] = deck[p];
}
if(p == 11){
Pile6[1] = deck[p];
}
if(p == 16){
Pile6[2] = deck[p];
}
if(p == 20){
Pile6[3] = deck[p];
}
if(p == 23){
Pile6[4] = deck[p];
}
if(p == 25){
Pile6[5] = deck[p];
}
}
if(p == 6 || p == 12 || p == 17 || p == 21 || p == 24 || p == 26 || p == 27){
if(p == 6){
Pile7[0] = deck[p];
}
if(p == 12){
Pile7[1] = deck[p];
}
if(p == 17){
Pile7[2] = deck[p];
}
if(p == 21){
Pile7[3] = deck[p];
}
if(p == 24){
Pile7[4] = deck[p];
}
if(p == 26){
Pile7[5] = deck[p];
}
if(p == 27){
Pile7[6] = deck[p];
}
}
}
For of all, you can get rid of the loop. The following code is equivalent.
Pile1[0] = deck[0];
Pile2[0] = deck[1];
Pile2[1] = deck[7];
Pile3[0] = deck[2];
Pile3[1] = deck[8];
Pile3[2] = deck[13];
Pile4[0] = deck[3];
Pile4[1] = deck[9];
Pile4[2] = deck[14];
Pile4[3] = deck[18];
Pile5[0] = deck[4];
Pile5[1] = deck[10];
Pile5[2] = deck[15];
Pile5[3] = deck[19];
Pile5[4] = deck[22];
Pile6[0] = deck[5];
Pile6[1] = deck[11];
Pile6[2] = deck[16];
Pile6[3] = deck[20];
Pile6[4] = deck[23];
Pile6[5] = deck[25];
Pile7[0] = deck[6];
Pile7[1] = deck[12];
Pile7[2] = deck[17];
Pile7[3] = deck[21];
Pile7[4] = deck[24];
Pile7[5] = deck[26];
Pile7[6] = deck[27];
Next, instead of using Pile1...Pile7, a 2D array would be better. So, just figure out the math and patterns.
int Pile[7][7];
for (int p=1; p<=6; p++) {
for (int i=0, j=p-1; i<p; i++, j+=(7-i)) {
Pile[p][i] = deck[j];
}
}
Generates the same result, where Pile[X] is used instead of PileX.

Errors in my Tic-Tac-Toe Game

Here's my code for my tic-tac-toe game:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
int main (void)
{
int const user1 = 1;
int const user2 = 2;
char move[10];
while (! all_locations_filled()) {
printf("User-1, please enter your move:");
scanf("%s", move);
if(valid_location(move)) {
mark_location(user1, move);
display_board(board[3][3]);
}
else if(won_the_game(user1)) {
printf("Congratulations User-1, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
printf("User-2, please enter your move:");
scanf("%s", move);
if(valid_location(move)) {
mark_location(user2, move);
display_board();
}
else if(won_the_game(user2) {
printf("Congratulations User-2, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
return 0;
}
bool valid_location(char str[10]) {
int strcmp(x, y);
if (strcmp(str[10], "upperLeft") == 0 || strcmp(str[10], "up") == 0 || strcmp(str[10], "upperRight") == 0 || strcmp(str[10], "left") == 0 || strcmp(str[10], "center") == 0 || strcmp(str[10], "right") == 0 || strcmp(str[10], "lowerLeft") == 0 || strcmp(str[10], "down") == 0 || strcmp(str[10], "lowerRight") == 0)
return true;
}
void mark_location(int userU, char str[10]) {
int strcmp(x, y);
if (strcmp(str[10], "upperLeft") == 0)
board[0][0] = userU;
else if (strcmp(str[10], "up") == 0)
board[0][1] = userU;
else if (strcmp(str[10], "upperRight") == 0)
board[0][2] = userU;
else if (strcmp(str[10], "left") == 0)
board[1][0] = userU;
else if (strcmp(str[10], "center") == 0)
board[1][1] = userU;
else if (strcmp(str[10], "right") == 0)
board[1][2] = userU;
else if (strcmp(str[10], "lowerLeft") == 0)
board[2][0] = userU;
else if (strcmp(str[10], "down") == 0)
board[2][1] = userU;
else if (strcmp(str[10], "lowerRight") == 0)
board [2][2] = userU;
}
char display_board(int array[][]) {
int i, j;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
if (array[i][j] == 0)
print("-");
else if (array[i][j] == 1)
print("x");
else if (array[i][j] == 2)
print("o");
}
bool all_locations_filled() {
int i, j;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
if board[i][j] == 0
return false;
return true;
}
bool won_the_game(userU) {
int i, j;
if (board[0][0] == userU && board[0][1] == userU && board[0][2] == userU)
return true;
else if (board[1][0] == userU && board[1][1] == userU && board[1][2] == userU)
return true;
else if (board[2][0] == userU && board[2][1] == userU && board[2][2] == userU)
return true;
else if (board[0][0] == userU && board[1][0] == userU && board[2][0] == userU)
return true;
else if (board[0][1] == userU && board[1][1] == userU && board[2][1] == userU)
return true;
else if (board[0][2] == userU && board[1][2] == userU && board[2][2] == userU)
return true;
else if (board[0][0] == userU && board[1][1] == userU && board[2][2] == userU)
return true;
else if (board[2][2] == userU && board[1][1] == userU && board[2][0] == userU)
return true;
else
return false;
}
There are a few errors that I don't understand, here they are:
tictactoe.c:50: error: expected expression before ‘}’ token
This error is at the end of the main function but I'm not sure what I did wrong.
tictactoe.c:52: error: nested functions are disabled, use -fnested-functions to re-enable
I didn't know I used a nested function.
tictactoe.c:53: warning: parameter names (without types) in function declaration
This is referring to int strcmp(x, y)
tictactoe.c:55: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
What did I do wrong with strcmp?
If someone could help me out I'd greatly appreciate it.
You're missing a closing parenthesis here (line #40):
else if(won_the_game(user2) {
Should be:
else if(won_the_game(user2)) {
You have a couple or problems with the strcmp as well.
strcmp(str[10], "upperRight")
The compiler is complaining about the first parameter str[10]. One problem is that this selects a single character from the string, and not the whole string. Another problem is that in an array of size 10, the positions are numbered 0..9 so there isn't even a position 10!
Also, a string literal like "upperRight" contains 10 visible characters plus an extra zero character as a terminator. So it needs 11 positions when stored in the str.

Resources