C - Process exited with error code 3221225477 - c

I'm doing a shift-reduce algorithm for our compiler design subject. This is the code.
void shiftReduce(char str[MAX_CHAR], int prodNum, int line)
{
int limit = 5, y=0;
int substrFlag = 1; //0 true 1 false
int ctr,x, counter;
int match, next;
char stack[MAX_CHAR];
clearString(stack);
OUTER:while ((strcmp(stack, prod[0].left) != 0) && (y < limit))
{
addChar(stack, str[0]);
strcpy(str, dequeue(str));
printf("Stack = %s\nQueue = %s\n", stack, str);
for (ctr = 0; ctr < prodNum; ctr++)
{
if (strstr(stack, prod[ctr].right) != NULL)
{ //substring found
substrFlag = 0;
strcpy(stack, replace(stack, prod[ctr].right, prod[ctr].left));
goto OUTER;
}
}
if ((str[0] == '\n') || (str[0] == '\0'))
y++;
}
if (strcmp(stack, prod[0].left) == 0)
;//printf("%s - Accepted.\n", stack);
else
printf("Syntax error on line %i\n", line);
}
When I comment the printf("Stack = %s\nQueue = %s\n", stack, str); line, it works well. But when I uncomment it, it returns the code 3221225477.
BTW. This is the dequeue function:
char * dequeue (char str[MAX_CHAR])
{
int x = 0; char temp;
for (x = 0; x < length(str); x++)
{
if ((x+1) < length(str))
str[x] = str[x+1];
}
return str;
}
and the addChar function:
void addChar (char * str, char letter)
{
int a = 0;
while (str[a] != '\0')
a++;
str[a] = letter;
str[a+1] = '\0';
return;
}
and finally replace function.
char * replace (char orig[MAX_CHAR], char substr[MAX_CHAR], char rep[MAX_CHAR])
{
int match, end=0, next=0;
int flag = 0; //0 true 1 false
char temp [MAX_CHAR];
char store[MAX_CHAR];
if (strstr(orig, substr) == NULL)
return NULL;
int x,y;
for (x = 0; x < length(orig); x++)
{
if (orig[x] == substr[0]) //if current character is equal to first character of substring
{
match = x;
for (y = 0; y < length(substr); y++)
{
if (orig[match+y] != substr[y])
{
flag = 1;
break;
}
}
if (flag == 0)
{
next = match + length(substr);
for (y = 0; y < length(rep); y++)
{
temp[match+y] = rep[y];
end = (match+y);
}
for (y = next; y < length(orig); y++)
{
temp[y] = orig[next+(y-next)];
}
return temp;
}
}
else
{
addChar(temp, orig[x]);
}
}
return temp;
}
PS. The prod array:
struct RULES
{
char left[MAX_CHAR];
char right[MAX_CHAR];
} RULES;
struct RULES prod[MAX_RULES];

When I comment the printf("Stack = %s\nQueue = %s\n", stack, str); line, it works well. But when I uncomment it, it returns the code 3221225477.
Then most likely either stack or str has not been 0-terminated or points to invalid memory.

Related

Storing mac addresses and device names listed in a text file into a struct array using C

I am trying to take a list of MAC Addresses and Device names from a text file and store them in an array called list;
struct list {
char ch;
char a[2], b[2], c[2], d[2], e[2], f[2], g[2], alias[32]; //chars a-g are supposed to be a maximum of two characters long to store the different segments of the mac addresses, and the alias stores the device name up to 32 characters.
};
The main function here, as of right now is supposed to open the file "Inet.txt" and read each character individually using "char cur = fgetc." The function then assigns the different parts of the MAC address to its corresponding position in chars a-g of the list struct, and the alias char if the function goes more than 2 chars without reaching a ":" or a " ". The length of the current char is represented by the variable k, which increases every time the program detects a letter or a number, and is reset to -1 every time variable 'cur' is assigned to something. There is also an array "struct list *head[32]; " which stores each line separately, the line number being identified by the variable "int i", which increases by one every time "cur == '\n'" starting at "int = 0." The main function is as follows;
int main()
{
FILE *fp;
char cur, temp[32], temp2[32], p;
struct list *head[32];
head[0]=(struct list*)malloc(sizeof(struct list));
int num = 0, d, data, devices, i = 0, j = -1, k = -1, l = 0;
char arr[100][2];
int count = 0;
//head = current = NULL;
fp = fopen("Inet.txt", "r");
if(fp==NULL)
{
printf("Error opening file.");
}
while((cur = fgetc(fp)) != EOF)
{
//stringchecker(cur)!=0
if((cur >= 48 && cur <= 57)||(cur >= 97 && cur <= 122)||(cur >= 65 && cur <= 90))
{
k++; //counter for temp array size
if(cur >= 97 && cur <= 122)
{
temp[k] = cur-32;
}
else
{
temp[k] = cur;
}
if(k>1)
{
strncpy(temp2, temp, k+1);
temp2[k+1] = '\0';
aloc(&head[i],temp2,7);
// k = -1;
}
}
else if(cur == ':')
{
if(count == 0)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 1)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 2)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 3)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 4)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 5)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
count++;
k = -1;
}
else if(cur == ' ')
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,6);
k = -1;
}
else if(cur == '\n')
{
printf("\n%s:%s:%s:%s:%s%s\nALIAS: %s", (*head[i]).a,(*head[i]).b,(*head[i]).c,(*head[i]).d,(*head[i]).e,(*head[i]).f,(*head[i]).alias);
exit(0);
devices++;
data++;
count = 0;
num = -1;
i++;
j = -1;
k = -1;
head[i]=(struct list*)malloc(sizeof(struct list));
//exit(0);
}
}
fclose(fp);
return 0;
}
The "aloc()" function assigns the current char up to 16 characters to a-g or alias depending on the value of the variable count, which is a parameter of this function. The aloc() function is as follows;
void aloc(struct list **head, char ch[16], int count) //assigns ch value to specific variable of the current head based on the value of count 1-7
{
if(count == 0)
{
strncpy((*head)->a,ch, 2);
}
else if(count == 1)
{
strncpy((*head)->b,ch, 2);
}
else if(count == 2)
{
strncpy((*head)->c,ch, 2);
}
else if(count == 3)
{
strncpy((*head)->d,ch, 2);
}
else if(count == 4)
{
strncpy((*head)->e,ch, 2);
}
else if(count == 5)
{
strncpy((*head)->f,ch, 2);
}
else if(count == 6)
{
strncpy((*head)->g,ch, 2);
}
else if(count == 7)
{
strncpy((*head)->alias,ch, 16);
}
}
The input text file "Inet.txt" is as follows;
A0:FB:C5:44:b8:45 PLATTE
58:24:29:0f:c8:ee JET
F1:C0:11:16:53:1F Wabash
A0:FB:C5:32:15:10 GREEN
33:68:29:a1:b2:3c Charlie
58:24:29:0A:0B:C0 BAKER
GG:01:X0:99:1A:45 FOXTROT
The main problem I am having with this code is the variables a-g are not being assigned correctly. When I run the program to only read the first line, I get the following output:
A0FBC544B8:FBC544B8:C544B8:44B8:B8
ALIAS: PLATTE%
When the output should be:
A0:FB:C5:44:B8
ALIAS: PLATTE
I am not sure which line is causing the entire mac address to be assigned to char a of the current list. I will post the code as I have it in its entirety here to avoid confusion.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct list {
char ch;
char a[2], b[2], c[2], d[2], e[2], f[2], g[2], alias[32];
};
void aloc(struct list **head, char ch[16], int count)
{
if(count == 0)
{
strncpy((*head)->a,ch, 2);
}
else if(count == 1)
{
strncpy((*head)->b,ch, 2);
}
else if(count == 2)
{
strncpy((*head)->c,ch, 2);
}
else if(count == 3)
{
strncpy((*head)->d,ch, 2);
}
else if(count == 4)
{
strncpy((*head)->e,ch, 2);
}
else if(count == 5)
{
strncpy((*head)->f,ch, 2);
}
else if(count == 6)
{
strncpy((*head)->g,ch, 2);
}
else if(count == 7)
{
strncpy((*head)->alias,ch, 16);
}
}
int main()
{
FILE *fp;
char cur, temp[32], temp2[32], p;
struct list *head[32];
head[0]=(struct list*)malloc(sizeof(struct list));
int num = 0, d, data, devices, i = 0, j = -1, k = -1, l = 0;
char arr[100][2];
int count = 0;
//head = current = NULL;
fp = fopen("Inet.txt", "r");
if(fp==NULL)
{
printf("Error opening file.");
}
while((cur = fgetc(fp)) != EOF)
{
//stringchecker(cur)!=0
if((cur >= 48 && cur <= 57)||(cur >= 97 && cur <= 122)||(cur >= 65 && cur <= 90))
{
k++; //counter for temp array size
if(cur >= 97 && cur <= 122)
{
temp[k] = cur-32;
}
else
{
temp[k] = cur;
}
if(k>1)
{
strncpy(temp2, temp, k+1);
temp2[k+1] = '\0';
aloc(&head[i],temp2,7);
// k = -1;
}
}
else if(cur == ':')
{
if(count == 0)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 1)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 2)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 3)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 4)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
else if(count == 5)
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,count);
}
count++;
k = -1;
}
else if(cur == ' ')
{
strncpy(temp2, temp, 2);
temp2[2] = '\0';
aloc(&head[i],temp2,6);
k = -1;
}
else if(cur == '\n')
{
printf("\n%s:%s:%s:%s:%s%s\nALIAS: %s", (*head[i]).a,(*head[i]).b,(*head[i]).c,(*head[i]).d,(*head[i]).e,(*head[i]).f,(*head[i]).alias);
exit(0);
devices++;
data++;
count = 0;
num = -1;
i++;
j = -1;
k = -1;
head[i]=(struct list*)malloc(sizeof(struct list));
//exit(0);
}
}
fclose(fp);
return 0;
}
I initially tried writing this program using linked lists, but I thought it would be easier to keep track of an array of list structs for use later in my program. However I keep getting the same problem with my output. Any help is appreciated.
If you remove exit(0); from the block here
else if(cur == '\n')
{
printf(/* ... */);
exit(0);
devices++;
data++;
count = 0;
/* ... */
then this program appears to work1.
I say "appears" because this program invokes Undefined Behaviour by printing non null-terminated buffers with the printf specifier %s.
You need to either specify a precision, being the maximum number of bytes to print, with each %s specifier. For example:
#include <stdio.h>
int main(void)
{
char buf[2] = "AB"; /* the null byte is not stored */
printf("%2s\n", buf);
}
Or, you need to ensure your buffers are large enough to store a desired string length plus the null-terminating byte. If you want to store a string of length 2, your buffer must be at least of size 3.
#include <stdio.h>
int main(void)
{
char buf[3] = "AB"; /* the null byte IS stored */
printf("%s\n", buf);
}
Note that strncpy is notoriously hard to use, as it does not null-terminate the buffer if the length of the source string is greater than or equal to the size provided.
1. You must also change char cur to int cur. On platforms when char is an unsigned type, you will not be able to reliably test against the negative int value of EOF. fgetc returns an int for this reason.
As pointed out in the comments, avoid magic numbers and instead use the functions found in <ctype.h>.
If your file contents are predictably formatted, you can just use fgets + sscanf to read each line. For example:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ADDRS 256
struct address {
char a[3];
char b[3];
char c[3];
char d[3];
char e[3];
char f[3];
char alias[32];
};
size_t read_macs(struct address *addrs, size_t limit, FILE *f)
{
char buffer[512];
size_t n = 0;
while (n < limit && fgets(buffer, sizeof buffer, f)) {
int cv = sscanf(buffer, "%2s:%2s:%2s:%2s:%2s:%2s%31s",
addrs[n].a, addrs[n].b, addrs[n].c,
addrs[n].d, addrs[n].e, addrs[n].f,
addrs[n].alias);
if (7 == cv)
n++;
}
return n;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: %s FILENAME\n", *argv);
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1], "r");
if (!file) {
perror(argv[1]);
return EXIT_FAILURE;
}
struct address store[MAX_ADDRS];
size_t length = read_macs(store, MAX_ADDRS, file);
fclose(file);
for (size_t i = 0; i < length; i++)
printf("%s (%s:....:%s)\n",
store[i].alias, store[i].a, store[i].f);
}
$ ./a.out Inet.txt
PLATTE (A0:....:45)
JET (58:....:ee)
Wabash (F1:....:1F)
GREEN (A0:....:10)
Charlie (33:....:3c)
BAKER (58:....:C0)
FOXTROT (GG:....:45)

In C , what can I use instead of strlen if I don't want to use a string function?

I recently started learning basic C and I am still noobie with it, I started doing some projects for learning and I am using library functions but I am interested in other methods...
So I have an email validation, it works fine, but I want to do it without strlen, any suggestion what can I do instead of strlen?
void mail(char e[]) {
int count = 0;
int countb = 0;
int i, j;
int t, t2;
int p = 0;
for (i = 0; i < strlen(e); i++) {
if (e[i] == '#') {
count++;
t = i;
}
}
if (count == 1) {
for (j = 0; j < t; j++) {
if (!(e[j] == '_' || e[j] == '.' || isalpha(e[j]) || isdigit(e[j]))) {
p = -1;
printf("\nwrong\n");
break;
}
}
if (p == 0) {
for (i = t; i < strlen(e); i++) {
if (e[i] == '.') {
t2 = i;
countb++;
}
}
if (countb == 1) {
for (i = 0; i < t2 && i > t2; i++) {
if (!(isalpha(e[i]))) {
p = -1;
printf("\nwrong\n");
break;
} else {
p = 1;
}
}
if (p == 1) {
if (e[t2 + 3] != '\0') {
p = -1;
printf("\nwrong\n");
}
}
} else {
p =- 1;
printf("\nwrong\n");
}
}
} else {
p = -1;
printf("\nwrong\n");
}
return;
}
Instead of comparing i < strlen(e) you can test if the byte at offset i is not the null terminator: e[i] != '\0'.
You can also compute the length of the string just once at the beginning of the function and store it into a len variable.
The only thing you can do is to implement your own strlen function.
In C, strings are null-terminated char arrays, that means, the last character of an array is '\0' (zero). The strlen function iterates over the character array until a null-character is found, it returns the length of that array, the null-character not included.
To duplicate (create) a c-string, you have to do the following:
char *str = malloc(strlen(another_string) + 1);
str[strlen(another_string)] = '\0';
Example for strlen:
size_t my_strlen(const char *str)
{
if (!str) return 0;
size_t n = 0;
for (; *str; ++n, ++str);
return n;
}
You can create you own my_strlen :)
unsigned int my_strlen(char *str)
OR
you can loop while your char is different of '\0' (null byte). So just replace strlen(e) with e[i] != '\0'
It should work just fine

I get error when i use fgets instead of gets

This is a picture when I use fgets. (it does not work properly, insanely slow !!)
and This is a picture when it comes to gets. (works fine)
# define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct sNode {
struct sNode* tNode;
int G[20];
};
struct tNode {
struct tNode* prev;
struct tNode* next;
int data;
int used;
};
int convert_input(struct sNode** t_ArrayRef, char string[200])
{
int j = 0, i = 0, temp = 0;
int K[20];
while (string[i] != '\0')
{
temp = 0;
while (string[i] != ' ' && string[i] != '\0')
temp = temp * 10 + (string[i++] - '0');
if (string[i] == ' ')
{
i++;
}
(*t_ArrayRef)->G[j++] = temp;
}
return j - 1;
}
int main() {
int i;
char string[200];
char* str[5];
struct sNode* t = (struct sNode*)malloc(sizeof(struct sNode));
str[0] = string;
//fgets(str[0], sizeof(str[0]), stdin); // doesn't works !!!
gets(str[0]); // works !!!
int c = convert_input(&t, str[0]);
int num = t->G[0];
const int a = num;
struct tNode* tNod[6000];
for (i = 0; i<num; i++) {
tNod[i] = (struct tNode*)malloc(sizeof(struct tNode));
}i = 0;
for (i = 1; i<num; i++) {
tNod[i - 1]->data = i;
tNod[i - 1]->used = 0;
if (i != num - 1) {
tNod[i - 1]->next = tNod[i];
}
else {
tNod[i - 1]->next = tNod[i];
tNod[i]->data = i + 1;
tNod[i]->next = tNod[0];
}
}i = 0;
struct tNode* current;
i = 1; int j = 0; int fCount = 0; int zCount = 0;
current = tNod[i - 1];
printf("<");
while (fCount == 0) {
while (current->used == 1) {
current = current->next;
j++;
if (j > num) {
fCount = 1;
break;
}
}
j = 0;
if (i % t->G[1] == 0 && fCount == 0) {
zCount++;
if (zCount != t->G[0]) {
printf("%d, ", current->data, i);
current->used = 1;
}
else {
printf("%d", current->data, i);
current->used = 1;
}
}
i++;
current = current->next;
}
printf(">");
return 0;
}
Could anyone explain to me why I can't get it working using fgets ?
When you use
fgets(str[0], sizeof(str[0]), stdin);
you do not pass the correct size: sizeof(str[0]) is the size of a pointer to char, not the size of the 200-byte char array that you stored in it.
The compiler resolves this sizeof operator at compile time. It has no idea about the value that you put into element zero. In fact, it ignores zero altogether, replacing with sizeof(*str).
Fix this problem by passing the proper size:
fgets(str[0], sizeof(string), stdin);

linked lists and stacks and a segmentation fault [closed]

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
First time posting a question on stack overflow so be nice.
I'm trying to write a program for school. This program is suppose to take a data set and turn it into a maze. The error I'm getting is a segmentation fault in putty but not in the IDE I'm using. Not sure what to do or how to handle it. I tried putting printf statements everywhere but none of them really show up doesnt make sense. Maybe because the functions themselves cause the fault not sure though what part.
//CODE BEGINS****************************************************************
#include <stdio.h>
#include <stdlib.h>
typedef int bool;
#define FALSE 0
#define TRUE 1
typedef struct mazeStruct
{
char **arr; /* allows for a dynamic 2-D maze of any size */
int xsize, ysize;
int xstart, ystart;
int xend, yend;
bool end;
} maze;
struct linkedStruct
{
int x;
int y;
bool Unvisited;
struct linkedStruct* next;
};
typedef struct linkedStruct linked;
typedef linked* linkedPtr;
void push(linkedPtr* hd, int Xval, int Yval)
{
linkedPtr ptr = (linkedPtr) malloc(sizeof(linked));
ptr->x = Xval;
ptr->y = Yval;
ptr->Unvisited = FALSE;
ptr->next = *hd;
*hd = ptr;
}
int isEmpty(linkedPtr hd)
{
if (hd == NULL)
return TRUE;
else
return FALSE;
}
int top(linkedPtr hd)
{
return (hd->x && hd->y);
}
void pop(linkedPtr* hd)
{
linkedPtr ptr = (linkedPtr) malloc(sizeof(linked));
ptr->x = NULL;
ptr->y = NULL;
ptr->Unvisited = TRUE;
ptr->next = *hd;
*hd = ptr;
free(ptr);
}
int main(int argc, char **argv)
{
maze m1;
linkedPtr head = NULL;
int xpos, ypos;
int i, j;
m1.end = FALSE;
FILE *src;
//FILE *src = fopen ("mazeData1.txt",'r');
/* verify the proper number of command line arguments were given */
if (argc != 2)
{
printf("Usage: %s <input file name>\n", argv[0]);
exit(-1);
}
/* Try to open the input file. */
if ((src = fopen(argv[1], "r")) == NULL)
{
printf("Can't open input file: %s", argv[1]);
printf("Standard Error.\n");
exit(-1);
}
/* read in the size, starting and ending positions in the maze */
fscanf(src, "%d %d", &m1.xsize, &m1.ysize);
if (m1.xsize < 1 || m1.ysize < 1)
{
printf("Size has to be 1 or above.\n");
fscanf(src, "%d %d", &m1.xsize, &m1.ysize);
}
fscanf(src, "%d %d", &m1.xstart, &m1.ystart);
if (m1.xstart > m1.xsize || m1.ystart > m1.ysize || m1.xstart < 1
|| m1.ystart < 1)
{
printf("The start has to be within the maze.\n");
fscanf(src, "%d %d", &m1.xstart, &m1.ystart);
}
fscanf(src, "%d %d", &m1.xend, &m1.yend);
if (m1.xend > m1.xsize || m1.yend > m1.ysize || m1.xend < 1 || m1.yend < 1)
{
printf("The end has to be within the maze.\n");
fscanf(src, "%d %d", &m1.xend, &m1.yend);
}
if (m1.xend == NULL || m1.yend == NULL)
{
printf("Error: Need at least three lines of input");
exit(-1);
}
/* print them out to verify the input */
printf("size: %d, %d\n", m1.xsize, m1.ysize);
printf("start: %d, %d\n", m1.xstart, m1.ystart);
printf("end: %d, %d\n", m1.xend, m1.yend);
/* allocate the maze */
m1.arr = (char **) malloc(sizeof(char *) * (m1.xsize + 2));
for (i = 0; i < m1.xsize + 2; i++)
m1.arr[i] = (char *) malloc(sizeof(char) * (m1.ysize + 2));
/* initialize the maze to empty */
for (i = 0; i < m1.xsize + 2; i++)
for (j = 0; j < m1.ysize + 2; j++)
m1.arr[i][j] = '.';
/* mark the borders of the maze with *'s */
for (i = 0; i < m1.xsize + 2; i++)
{
m1.arr[i][0] = '*';
m1.arr[i][m1.ysize + 1] = '*';
}
for (i = 0; i < m1.ysize + 2; i++)
{
m1.arr[0][i] = '*';
m1.arr[m1.xsize + 1][i] = '*';
}
/* mark the starting and ending positions in the maze */
m1.arr[m1.xstart][m1.ystart] = 's';
m1.arr[m1.xend][m1.yend] = 'e';
/* mark the blocked positions in the maze with *'s */
while (fscanf(src, "%d %d", &xpos, &ypos) != EOF)
{
if (xpos > m1.xsize || ypos > m1.ysize || xpos < 1 || ypos < 1
|| (xpos == m1.xstart && ypos == m1.ystart)
|| (xpos == m1.xend && ypos == m1.yend))
{
printf(
"Error: X or Y is: out of range or is on the end or is on the start\n");
continue;
}
m1.arr[xpos][ypos] = '*';
}
/* print out the initial maze */
for (i = 0; i < m1.xsize + 2; i++)
{
for (j = 0; j < m1.ysize + 2; j++)
printf("%c", m1.arr[i][j]);
printf("\n");
}
// THE START OF THE DEPTH FIRST SEARCH METHOD
for (i = 0; i < m1.xsize + 2; i++)
{
for (j = 0; j < m1.ysize + 2; j++)
{
if (m1.arr[i][j] != '*')
{
head->Unvisited = FALSE;
head->next = head->next + 1; //MAYBE
}
}
}
head->x = m1.xstart;
head->y = m1.ystart;
head->Unvisited = FALSE;
while ((isEmpty(head) == FALSE) && (m1.end == FALSE))
{
if ((m1.xend == head->x) && (m1.yend == head->y))
{
printf("The END has be found!\n");
m1.end = TRUE;
}
if ((head->x + 1 && head->y) == TRUE)
{
push(&head, head->x + 1, head->y);
}
else if ((head->x - 1 && head->y) == TRUE)
{
push(&head, head->x - 1, head->y);
}
else if ((head->x && head->y + 1) == TRUE)
{
push(&head, head->x, head->y + 1);
}
else if ((head->x && head->y) == TRUE)
{
push(&head, head->x, head->y - 1);
}
else
{
pop(head);
}
}
if (isEmpty(head) == TRUE)
{
printf("Maze has no solution");
exit(0);
}
else
{
printf("%d %d", &head);
}
printf("%d", top(head));
free(m1.arr);
m1.arr = NULL;
return 1;
}
The main problem here is that you are hiding pointer with typedef:
typedef linked* linkedPtr;
In main you are declaring
linkedPtr head = NULL;
but you never allocate/mallocate space for that variable and the first piece of code that dereference it invokes Undefined Behavior because you are dereferencing a null pointer
// THE START OF THE DEPTH FIRST SEARCH METHOD
for (i = 0; i < m1.xsize + 2; i++)
{
for (j = 0; j < m1.ysize + 2; j++)
{
if (m1.arr[i][j] != '*')
{
head->Unvisited = FALSE; <----------BOOOOOOOOOOOOOOM-------
head->next = head->next + 1;
}
}
}
Moreover you have a type mismatch calling pop function, change
pop(head);
to
pop(&head);

Reverse Polish converter

I am trying to make a reverse Polish printer which can perform the following operation-
Inputs:
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))
Outputs:
abc*+
ab+zx+*
at+bac++cd+^*
This is my code:
#include <stdio.h>
#include <string.h>
char pop(int t);
void push(int c, int t);
int main()
{
int z;
scanf("%d", &z);
char *a[100];
int i = 0;
int q = z;
while (q-- > 0)
{
char v[400];
scanf("%s", &v);
int t;
for (t = 0; t < strlen(v); t++) //loop to put the values and signs in the 2 stacks
{
if ((v[t] == '*') || (v[t] == '+') || (v[t] == '-') || (v[t] == '^'))
{
push(v[t], 2);
}
else if (v[t] == ')')
{
int y = pop(2);
push(y, 1);
}
else
{
push(v[t], 1);
}
}
int k = 0;
char c;
while ((c = pop(1)) !='\0') //loop to put elements in the array v
{
if (c != '(')
{
v[k++] = c;
}
}
v[k--] = '\0';
int m;
for (m=0; m != k; m++, k--) //for reversing the string
{
char t = v[m];
v[m] = v[k];
v[k] = t;
}
a[i++] =v;
printf("%s",a[i - 1]);
}
int p;
for (p = 0; p <z ; p++) //printing the elements
printf("%s\n",*a[p]);
return 0;
}
char ac[400];
char as[400];
int ic = 0;
int is = 0;
void push(int c,int t)
{
if (t == 1 && ic != 400)
ac[ic++] = c;
else if (t == 2 && is != 400)
as[is++] = c;
}
char pop(int t)
{
if (t == 1 && ic != 0)
return ac[--ic];
if (t == 2 && is != 0)
return as[--is];
return '\0';
}
But it is not even inputting properly and I am not able to figure out what are the mistakes in this code.Please help to figure out what are the problems.
after inputing the no of test cases i.e.int z and first line if input
it crashes
This is due to the
printf("%s\n",*a[p]);
as BLUEPIXY noticed, *a[p] is a char; but %s expects a char *, thus you need
printf("%s\n", a[p]);
and regarding v is out of scope, the crucial factor is not the scope (visibility), but the storage duration (lifetime) of v - its lifetime ends when execution of the block with which it is associated ends, and the value of a pointer a[i] to it becomes indeterminate; by changing
a[i++] =v;
to
a[i++] = strdup(v);
you can remedy that.

Resources