user_id
stringlengths
24
24
time
stringlengths
24
24
feature_type
stringclasses
5 values
feature_version
stringclasses
2 values
input_question
stringlengths
1
1.24k
input_code
stringlengths
2
18.5k
input_intention
stringlengths
1
2.48k
input_task_description
stringlengths
18
3.32k
63c648665f004bf4898cfc20
2023-03-09T17:43:07.447Z
Help Fix Code
v2
This code gives me Input/output error. Fix that
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } printf("HELLO00"); // TODO int ret; int fd[2]; ret = fork(); if (ret < 0) { perror("pipe"); } if (pipe(fd) == -1) { perror("pipe"); exit(1); } printf("HELLO1"); if (ret == 0) { // child process close(fd[1]); dup2(fd[0], STDOUT_FILENO); execl("./validate", "validate", NULL); close(fd[0]); } else { // parent process printf("HELLO2"); close(fd[0]); dup2(fd[1], STDIN_FILENO); write(fd[1], user_id, 10); write(fd[1], password, 10); close(fd[1]); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); return -1; } if (WIFEXITED(status)) { int exit_status = WEXITSTATUS(status); if(exit_status == 0) { printf("%s", SUCCESS); } else if(exit_status == 2) { printf("%s", INVALID); } else { printf("%s", NO_USER); } } } return 0; }
null
null
63c648665f004bf4898cfc20
2023-03-09T17:45:30.263Z
Help Fix Code
v2
This code gives me Input/output error. Fix that
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } // TODO int ret; int fd[2]; ret = fork(); if (ret < 0) { perror("pipe"); } if (pipe(fd) == -1) { perror("pipe"); exit(1); } if (ret == 0) { // child process close(fd[1]); dup2(fd[0], STDOUT_FILENO); execl("./validate", "validate", NULL); close(fd[0]); } else { // parent process close(fd[0]); dup2(fd[1], STDIN_FILENO); write(fd[1], user_id, strlen(user_id)); write(fd[1], password, strlen(user_id)); close(fd[1]); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); return -1; } if (WIFEXITED(status)) { int exit_status = WEXITSTATUS(status); if(exit_status == 0) { printf("%s", SUCCESS); } else if(exit_status == 2) { printf("%s", INVALID); } else { printf("%s", NO_USER); } } } return 0; }
null
null
63c648665f004bf4898cfc20
2023-03-09T17:51:10.877Z
Help Fix Code
v2
I got could not read from stdin error. fix that
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } // TODO int ret; int fd[2]; ret = fork(); if (ret < 0) { perror("pipe"); } if (pipe(fd) == -1) { perror("pipe"); exit(1); } if (ret == 0) { // child process close(fd[1]); dup2(fd[0], STDIN_FILENO); execl("./validate", "validate", NULL); close(fd[0]); } else { // parent process close(fd[0]); dup2(fd[1], STDOUT_FILENO); write(fd[1], user_id, strlen(user_id)); write(fd[1], password, strlen(user_id)); close(fd[1]); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); return -1; } if (WIFEXITED(status)) { int exit_status = WEXITSTATUS(status); if(exit_status == 0) { printf("%s", SUCCESS); } else if(exit_status == 2) { printf("%s", INVALID); } else { printf("%s", NO_USER); } } } return 0; }
null
null
63c648665f004bf4898cfc20
2023-03-09T17:52:39.058Z
Help Fix Code
v2
This code gives me could not read from stdin error. Fix that
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } // TODO int ret; int fd[2]; ret = fork(); if (ret < 0) { perror("pipe"); } if (pipe(fd) == -1) { perror("pipe"); exit(1); } if (ret == 0) { // child process close(fd[1]); dup2(fd[0], STDIN_FILENO); execl("./validate", "validate", NULL); close(fd[0]); } else { // parent process close(fd[0]); dup2(fd[1], STDOUT_FILENO); write(fd[1], user_id, strlen(user_id)); write(fd[1], password, strlen(password)); close(fd[1]); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); return -1; } if (WIFEXITED(status)) { int exit_status = WEXITSTATUS(status); if(exit_status == 0) { printf("%s", SUCCESS); } else if(exit_status == 2) { printf("%s", INVALID); } else { printf("%s", NO_USER); } } } return 0; }
null
null
63c648665f004bf4898cfc20
2023-03-13T21:39:10.549Z
General Question
v2
I have segmentation fault from strlen. How can I fix this?
null
null
null
63c648675f004bf4898cfc2a
2023-02-09T20:53:57.423Z
General Question
v1
How to read a bitmap file in C?
null
null
null
63c648675f004bf4898cfc2a
2023-02-09T20:54:38.090Z
Help Write Code
v1
null
null
null
Display a bitmap image in C
63c648675f004bf4898cfc2a
2023-02-14T00:16:23.667Z
Question from Code
v1
what will this evaluate to in C
char name["32"];
null
null
63c648675f004bf4898cfc2a
2023-02-14T00:19:20.548Z
Question from Code
v1
What will the declaration on line 2 become after the program has passed through the C pre-processor?
#define MAXNAME = 32; char name[MAXNAME];
null
null
63c648685f004bf4898cfc2f
2023-02-15T20:14:59.309Z
Help Fix Code
v1
null
int delete_user(const char *name, User **user_ptr_del) { User *currUser = *user_ptr_del; if (currUser==NULL){ return 1; } if(strcmp(currUser->name,name)==0){ //delete the first user *user_ptr_del = currUser->next; free_user(currUser); return 0; } while (currUser -> next != NULL){ if(strcmp((currUser->next)->name,name)==0){ free_user(currUser->next); currUser->next= (currUser->next)->next; return 0; } currUser = currUser->next; } return 1; } /* * frees the user pointed to by currUser from the * heap and all its attributes. */ void free_user(User *currUser){ //iterate to free all posts Post *currpost = currUser->first_post; Post *currpost2 = currUser->first_post; while(currpost!= NULL){ currpost2 = currpost; currpost = currpost->next; //free contents, date and post free(currpost2->contents); free(currpost2->date); free(currpost2); // currpost2 = currpost; // free(currpost); // currpost = currpost2->next; } //free the user from its friends' friend arrays for (int i=0; i<MAX_FRIENDS;i++){ User *friend = (currUser->friends)[i]; if (friend!=NULL){ for (int j=0; j<MAX_FRIENDS;j++){ User *current = (friend->friends)[j]; if (current!=NULL && strcmp(current->name, currUser->name)==0){ (friend->friends)[j] = NULL; } } } } free(currUser); }
From the list pointed to by *user_ptr_del, delete the user with the given name. Remove the deleted user from any lists of friends. Return: 0 on success and 1 if a user with this name does not exist.
null
63c648685f004bf4898cfc2f
2023-02-15T20:17:41.432Z
General Question
v1
why goes valgrind say Invalid read of size 8
null
null
null
63c648685f004bf4898cfc2f
2023-02-15T20:24:28.607Z
Help Fix Code
v1
null
int delete_user(const char *name, User **user_ptr_del) { User *currUser = *user_ptr_del; if (currUser==NULL){ return 1; } if(strcmp(currUser->name,name)==0){ //delete the first user *user_ptr_del = currUser->next; free_user(currUser); return 0; } while (currUser -> next != NULL){ if(strcmp((currUser->next)->name,name)==0){ free_user(currUser->next); currUser->next= (currUser->next)->next; return 0; } currUser = currUser->next; } return 1; } /* * frees the user pointed to by currUser from the * heap and all its attributes. */ void free_user(User *currUser){ //iterate to free all posts Post *currpost = currUser->first_post; Post *currpost2 = currUser->first_post; while(currpost!= NULL){ currpost2 = currpost; currpost = currpost->next; //free contents, date and post free(currpost2->contents); free(currpost2->date); free(currpost2); // currpost2 = currpost; // free(currpost); // currpost = currpost2->next; } //free the user from its friends' friend arrays for (int i=0; i<MAX_FRIENDS;i++){ User *friend = (currUser->friends)[i]; if (friend!=NULL){ for (int j=0; j<MAX_FRIENDS;j++){ User *current = (friend->friends)[j]; if (current!=NULL && strcmp(current->name, currUser->name)==0){ (friend->friends)[j] = NULL; } } } } free(currUser); }
remove user with name name from user_ptr_del
null
63c648685f004bf4898cfc2f
2023-02-15T20:34:11.397Z
Help Fix Code
v1
null
for (int i=0; i<MAX_FRIENDS;i++){ User *friend = (currUser->friends)[i]; if (friend!=NULL){ for (int j=0; j<MAX_FRIENDS;j++){ User *current = (friend->friends)[j]; if (current!=NULL){ if (strcmp(current->name, currUser->name)==0){ (friend->friends)[j] = NULL; } } } } } free(currUser);
remove the currUser from its friends' friend arrays
null
63c6486c5f004bf4898cfc5c
2023-01-17T18:21:14.916Z
General Question
v1
how to I create a function in C?
null
null
null
63c6486c5f004bf4898cfc5c
2023-01-17T18:22:11.129Z
General Question
v1
How would I write a function in C that calculates 2 to the power of n for an integer input n?
null
null
null
63c6486c5f004bf4898cfc5c
2023-01-18T03:43:04.733Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> /* * Write a void function invest that takes your money and multiplies it by the given rate. */ void invest(double *principal, double rate) { *principal = *principal * rate; } /* * NOTE: don't change the main function! * Sample usage: * $ gcc -Wall -std=gnu99 -g -o invest invest.c * $ ./invest 10000 1.05 * 10500.00 */ int main(int argc, char **argv) { // Read in the command-line arguments and convert the strings to doubles double principal = strtod(argv[1], NULL); double rate = strtod(argv[2], NULL); // Call invest to make you more money invest(&principal, rate); printf("%.2f\n", principal); return 0; }
The function invest should multiply the principle input by the rate input
null
63c6486c5f004bf4898cfc5c
2023-01-22T19:58:01.569Z
Help Fix Code
v1
null
int main() { char **last_names; last_names = (char **) malloc(sizeof(char) * 4); last_names[0] = (char *) malloc(sizeof(char) * 20); last_names[1] = (char *) malloc(sizeof(char) * 20); last_names[2] = (char *) malloc(sizeof(char) * 20); last_names[3] = (char *) malloc(sizeof(char) * 20); return 0; }
Assign a dynamically allocated char * array of length 4 to last_names. Then, allocate a character array of length 20 for each element of the array pointed to by last_names.
null
63c6486c5f004bf4898cfc5c
2023-01-22T20:00:32.309Z
Help Fix Code
v1
null
int main(int argc, char **argv) { float *rainfall; float rain_today; rain_today = rainfall; return 0; }
rainfall has been dynamically allocated space for a floating point number. Both rainfall and rain_today have been initialized in hidden code. Assign the amount in rain_today to the space rainfall points to
null
63c6486c5f004bf4898cfc5c
2023-01-22T20:01:09.749Z
Help Fix Code
v1
null
int main(int argc, char **argv) { float *rainfall; float rain_today; rain_today = *rainfall; return 0; }
rainfall has been dynamically allocated space for a floating point number. Both rainfall and rain_today have been initialized in hidden code. Assign the amount in rain_today to the space rainfall points to
null
63c6486c5f004bf4898cfc5c
2023-01-22T20:02:41.851Z
Question from Code
v1
why does this code give a warning
int main(int argc, char **argv) { float *rainfall; float rain_today; rain_today = *rainfall; return 0; }
null
null
63c6486c5f004bf4898cfc5c
2023-01-25T01:36:40.577Z
Help Fix Code
v1
null
void fib(int **point, int n) { *point = malloc(sizeof(int) * n); int curr = 1; int prev = 0; if (n == 1) { *point[0] = prev; } else if (n == 2) { *point[0] = prev; *point[0] = curr; } else { int temp; *point[0] = prev; *point[1] = curr; for (int e = 2, e<n, e++) { temp = curr; curr = curr + prev; prev = temp; *point[e] = curr; } } }
Should set point to the first n elements of the Fibonacci sequence
null
63c6486c5f004bf4898cfc5c
2023-01-25T01:46:54.567Z
Help Fix Code
v1
null
void fib(int **point, int n) { *point = malloc(sizeof(int) * n); if (n == 1) { *point = {0}; } else { *point = {0, 1}; for (int e = 2, e < n, e++) { *point[e] = *point[e-1] + point[e-2]; } } }
Should set point, which has already been declared, to the first n elements of the Fibonacci sequence
null
63c648705f004bf4898cfc7a
2023-01-20T20:00:06.456Z
Help Fix Code
v1
null
int element; /*assigned a hidden value */ /* In the following line, write what <cond> should be in: "switch(<cond>){" */ switch(element) /* The body of the switch statement goes here. The first case is written for you below. */ case 1 : printf("earth"); break; /* Write case 2 (water) here. */ case 2 : printf("water"); break; /* Write case 3 (air) here. */ case 3 : printf("air"); break; /* Write case 4 (fire) here. */ case 4 : printf("fire"); break; /* Write the default case here. */ daufault : printf("not a valid element"); break;
An integer variable, element is initialized as a hidden value. Write a switch statement with cases 1-4 that prints one of the four classical elements based on the hidden integer value. Write the cases in the following order (beginning with case 1): earth, water, air, and fire. The default case should print "not a valid element".
null
63c648705f004bf4898cfc7a
2023-01-20T20:01:43.398Z
Help Fix Code
v1
null
int element; /*assigned a hidden value */ /* In the following line, write what <cond> should be in: "switch(<cond>){" */ switch(element) /* The body of the switch statement goes here. The first case is written for you below. */ case 1 : printf("earth"); break; /* Write case 2 (water) here. */ case 2 : printf("water"); break; /* Write case 3 (air) here. */ case 3 : printf("air"); break; /* Write case 4 (fire) here. */ case 4 : printf("fire"); break; /* Write the default case here. */ default : printf("not a valid element"); break;
An integer variable, element is initialized as a hidden value. Write a switch statement with cases 1-4 that prints one of the four classical elements based on the hidden integer value. Write the cases in the following order (beginning with case 1): earth, water, air, and fire. The default case should print "not a valid element".
null
63c648705f004bf4898cfc7a
2023-01-20T20:06:27.482Z
Help Fix Code
v1
null
int element; /*assigned a hidden value */ /* In the following line, write what <cond> should be in: "switch(<cond>){" */ switch(element) { /* The body of the switch statement goes here. The first case is written for you below. */ case 1 : printf("earth"); break; /* Write case 2 (water) here. */ case 2 : printf("water"); break; /* Write case 3 (air) here. */ case 3 : printf("air"); break; /* Write case 4 (fire) here. */ case 4 : printf("fire"); break; /* Write the default case here. */ default : printf("not a valid element"); break; }
An integer variable, element is initialized as a hidden value. Write a switch statement with cases 1-4 that prints one of the four classical elements based on the hidden integer value. Write the cases in the following order (beginning with case 1): earth, water, air, and fire. The default case should print "not a valid element".
null
63c648705f004bf4898cfc7a
2023-01-27T20:27:44.349Z
Help Fix Code
v1
null
typedef struct Node { int value; struct Node *next; }
Create a node struct with the following struct members: - value, an integer - next, a pointer to another node Typedef your structure as Node.
null
63c648705f004bf4898cfc7a
2023-01-27T20:52:46.905Z
Help Fix Code
v1
null
#include <stdlib.h> int **validity = malloc(sizeof(*int) * 2); validity[0] = malloc(sizeof(int) * BOARD_SIZE); validity[1]= malloc (sizeof(int) * BOARD_SIZE); for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { int current_val = board[i][j]; int valid = 1; for (int k = 0; k < BOARD_SIZE; k++){ if (board[i][k] == current_val || board[k][j] == current_val){ valid = 0; } } validity[i][j] = valid; } }
Your task is to write a code fragment that creates a new array of int arrays iff the same size as board. Each element in the sub-array of ints is set to 1 iff the number at the same index in board is valid (unique in the row and column it is located in), or 0 if it is invalid. Name this new board validity.
null
63c648705f004bf4898cfc7a
2023-01-27T21:00:47.205Z
Help Fix Code
v1
null
#include <stdlib.h> int **validity = malloc(sizeof(int*) * BOARD_SIZE); for (int a = 0; a < BOARD_SIZE; a++){ validity[a] = malloc(sizeof(int) * BOARD_SIZE); } for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { int current_val = board[i][j]; int valid = 1; for (int k = 0; k < BOARD_SIZE; k++){ if (board[i][k] == current_val || board[k][j] == current_val){ valid = 0; } } validity[i][j] = valid; } }
Your task is to write a code fragment that creates a new array of int arrays iff the same size as board. Each element in the sub-array of ints is set to 1 iff the number at the same index in board is valid (unique in the row and column it is located in), or 0 if it is invalid. Name this new board validity.
null
63c648705f004bf4898cfc7a
2023-03-14T07:28:12.873Z
Question from Code
v2
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position B?
#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648705f004bf4898cfc7f
2023-01-24T16:21:47.657Z
Question from Code
v1
what is wrong
int **split_array(const int *s, int length) { int **new_split_array = malloc(sizeof(int*) * 2); if (length % 2 == 0) { new_split_array[0] = malloc(sizeof(int) * length / 2); new_split_array[1] = malloc(sizeof(int) * length / 2); } else { new_split_array[0] = malloc(sizeof(int) * length / 2); new_split_array[1] = malloc(length - (sizeof(int) * length / 2)); } int even_index = 0; int odd_index = 0; for (int i = 0; i < length ; i++) { if (i % 2 == 0) { new_split_array[0][even_index] = s[i]; even_index++; } else { new_split_array[1][odd_index] = s[i]; odd_index++; } } return new_split_array; }
null
null
63c648705f004bf4898cfc7f
2023-01-24T16:28:20.904Z
Question from Code
v1
segmentation fault
int **split_array(const int *s, int length) { int *new_split_array = malloc(sizeof(int*) * 2); if (length % 2 == 0) { new_split_array[0] = malloc(sizeof(int) * length / 2); new_split_array[1] = malloc(sizeof(int) * length / 2); } else { new_split_array[0] = malloc(sizeof(int) * (length / 2 + 1)); new_split_array[1] = malloc(length - (sizeof(int) * (length / 2 + 1))); } int even_index = 0; int odd_index = 0; for (int i = 0; i < length ; i++) { if (i % 2 == 0) { new_split_array[0][even_index] = s[i]; even_index++; } else { new_split_array[1][odd_index] = s[i]; odd_index++; } } return new_split_array; }
null
null
63c648705f004bf4898cfc7f
2023-01-24T16:33:29.900Z
Question from Code
v1
what is wrong
#include <stdio.h> #include <stdlib.h> /* Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division. */ int **split_array(const int *s, int length) { int **new_split_array = malloc(sizeof(int*) * 2); if (length % 2 == 0) { new_split_array[0] = malloc(sizeof(int) * (length / 2)); new_split_array[1] = malloc(sizeof(int) * (length / 2)); } else { new_split_array[0] = malloc(sizeof(int) * (length / 2 + 1)); new_split_array[1] = malloc(length - (sizeof(int) * (length / 2 + 1))); } int even_index = 0; int odd_index = 0; for (int i = 0; i < length ; i++) { if (i % 2 == 0) { new_split_array[0][even_index] = s[i]; even_index++; } else { new_split_array[1][odd_index] = s[i]; odd_index++; } } return new_split_array; } /* Return a pointer to an array of ints with size elements. - strs is an array of strings where each element is the string representation of an integer. - size is the size of the array */ int *build_array(char **strs, int size) { int *new_build_array = malloc(sizeof(int) * size); for (int i = 0 ; i < size ; i++) { new_build_array[i] = strtol(strs[i], NULL, 10); } return new_build_array; } int main(int argc, char **argv) { /* Replace the comments in the next two lines with the appropriate arguments. Do not add any additional lines of code to the main function or make other changes. */ int *full_array = build_array(argv + 1, argc - 1); int **result = split_array(full_array, argc - 1); printf("Original array:\n"); for (int i = 0; i < argc - 1; i++) { printf("%d ", full_array[i]); } printf("\n"); printf("result[0]:\n"); for (int i = 0; i < argc / 2; i++) { printf("%d ", result[0][i]); } printf("\n"); printf("result[1]:\n"); for (int i = 0; i < (argc - 1) / 2; i++) { printf("%d ", result[1][i]); } printf("\n"); free(full_array); free(result[0]); free(result[1]); free(result); return 0; }
null
null
63c648705f004bf4898cfc7f
2023-01-27T03:25:02.128Z
Help Fix Code
v1
null
struct Node { int value; struct Node *next; };
Create a node struct with the following struct members: - value, an integer - next, a pointer to another node Typedef your structure as Node.
null
63c648705f004bf4898cfc7f
2023-01-27T03:26:28.575Z
Explain Code
v1
null
// Please insert your code below typedef struct Node { int value; struct Node *next; };
null
null
63c648705f004bf4898cfc7f
2023-01-27T03:27:56.714Z
Help Fix Code
v1
null
// Please insert your code below typedef struct Node { int value; struct Node *next; };
Create a node struct with the following struct members: - value, an integer - next, a pointer to another node Typedef your structure as Node.
null
63c648705f004bf4898cfc7f
2023-02-06T23:54:32.651Z
Question from Code
v1
why segmentation fault
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" /* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */ void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 18, 0); fread(width, sizeof(int), 1, image); fseek(image, 22, 0); fread(height, sizeof(int), 1, image); fseek(image, 10, 0); fread(pixel_array_offset, sizeof(int), 1, image); } /* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel ***pixel_array = malloc(height * sizeof(struct pixel *) * width); fseek(image, pixel_array_offset, 0); for (int i = 0; i < height; i++) { for (int j = 0 ; i < width ; j++) { struct pixel new_pixel; pixel_array[i][j] = &new_pixel; fread(&new_pixel.blue, 1, 1, image); fread(&new_pixel.green, 1, 1, image); fread(&new_pixel.red, 1, 1, image); } } return pixel_array[0]; } /* * Print the blue, green, and red colour values of a pixel. * You don't need to change this function. */ void print_pixel(struct pixel p) { printf("(%u, %u, %u)\n", p.blue, p.green, p.red); }
null
null
63c648705f004bf4898cfc7f
2023-02-07T00:04:24.975Z
Question from Code
v1
what is wrong
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" /* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */ void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 18, 0); fread(width, sizeof(int), 1, image); fseek(image, 22, 0); fread(height, sizeof(int), 1, image); fseek(image, 10, 0); fread(pixel_array_offset, sizeof(int), 1, image); } /* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel ***pixel_array = malloc(height * sizeof(struct pixel *)); fseek(image, pixel_array_offset, 0); for (int i = 0; i < height; i++) { pixel_array[i] = malloc(sizeof(struct pixel *) * width); for (int j = 0 ; i < width ; j++) { struct pixel new_pixel; pixel_array[i][j] = &new_pixel; fread(&new_pixel.blue, 1, 1, image); fread(&new_pixel.green, 1, 1, image); fread(&new_pixel.red, 1, 1, image); } } return pixel_array[0]; } /* * Print the blue, green, and red colour values of a pixel. * You don't need to change this function. */ void print_pixel(struct pixel p) { printf("(%u, %u, %u)\n", p.blue, p.green, p.red); }
null
null
63c648705f004bf4898cfc7f
2023-02-07T00:10:08.909Z
Question from Code
v1
why segmentation fault
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" /* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */ void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 18, 0); fread(width, sizeof(int), 1, image); fseek(image, 22, 0); fread(height, sizeof(int), 1, image); fseek(image, 10, 0); fread(pixel_array_offset, sizeof(int), 1, image); } /* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel ****pixel_array = malloc(height * sizeof(struct pixel *)); fseek(image, pixel_array_offset, 0); for (int i = 0; i < height; i++) { pixel_array[i] = malloc(sizeof(struct pixel *) * width); for (int j = 0 ; i < width ; j++) { struct pixel new_pixel; *pixel_array[i][j] = &new_pixel; fread(&new_pixel.blue, 1, 1, image); fread(&new_pixel.green, 1, 1, image); fread(&new_pixel.red, 1, 1, image); } } return **pixel_array; } /* * Print the blue, green, and red colour values of a pixel. * You don't need to change this function. */ void print_pixel(struct pixel p) { printf("(%u, %u, %u)\n", p.blue, p.green, p.red); }
null
null
63c648705f004bf4898cfc7f
2023-02-07T00:21:18.253Z
Question from Code
v1
why segmentation fault
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" /* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */ void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 18, 0); fread(width, sizeof(int), 1, image); fseek(image, 22, 0); fread(height, sizeof(int), 1, image); fseek(image, 10, 0); fread(pixel_array_offset, sizeof(int), 1, image); } /* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **pixel_array = malloc(height * sizeof(struct pixel *)); fseek(image, pixel_array_offset, 0); for (int i = 0; i < height; i++) { pixel_array[i] = malloc(sizeof(struct pixel) * width); for (int j = 0 ; i < width ; j++) { struct pixel new_pixel; pixel_array[i][j] = new_pixel; fread(&new_pixel.blue, 1, 1, image); fread(&new_pixel.green, 1, 1, image); fread(&new_pixel.red, 1, 1, image); } } return pixel_array; } /* * Print the blue, green, and red colour values of a pixel. * You don't need to change this function. */ void print_pixel(struct pixel p) { printf("(%u, %u, %u)\n", p.blue, p.green, p.red); }
null
null
63c648705f004bf4898cfc7f
2023-02-07T01:49:49.827Z
Question from Code
v1
what is wrong
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" /* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */ void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 18, 0); fread(width, sizeof(int), 1, image); fseek(image, 22, 0); fread(height, sizeof(int), 1, image); fseek(image, 10, 0); fread(pixel_array_offset, sizeof(int), 1, image); } /* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **pixel_array = malloc(height * sizeof(struct pixel *)); fseek(image, pixel_array_offset, 0); for (int i = 0; i < height; i++) { pixel_array[i] = malloc(sizeof(struct pixel) * width); for (int j = 0 ; j < width ; j++) { struct pixel new_pixel; pixel_array[i][j] = new_pixel; fread(&new_pixel.blue, 1, 1, image); fread(&new_pixel.green, 1, 1, image); fread(&new_pixel.red, 1, 1, image); } } return pixel_array; } /* * Print the blue, green, and red colour values of a pixel. * You don't need to change this function. */ void print_pixel(struct pixel p) { printf("(%u, %u, %u)\n", p.blue, p.green, p.red); } int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); // Print out some pixels from each of the image's corners. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { print_pixel(pixels[i][j]); print_pixel(pixels[i][width - 1 - j]); print_pixel(pixels[height - 1 - i][j]); print_pixel(pixels[height - 1 - i][width - 1 - j]); } } // Clean up: you need to do this! fclose(image); for (int i = 0 ; i < height ; i++) { free(pixels[i]); } free(pixels); return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-07T01:51:01.834Z
Question from Code
v1
How to properly free pixel_array
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" /* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */ void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 18, 0); fread(width, sizeof(int), 1, image); fseek(image, 22, 0); fread(height, sizeof(int), 1, image); fseek(image, 10, 0); fread(pixel_array_offset, sizeof(int), 1, image); } /* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **pixel_array = malloc(height * sizeof(struct pixel *)); fseek(image, pixel_array_offset, 0); for (int i = 0; i < height; i++) { pixel_array[i] = malloc(sizeof(struct pixel) * width); for (int j = 0 ; j < width ; j++) { struct pixel new_pixel; pixel_array[i][j] = new_pixel; fread(&new_pixel.blue, 1, 1, image); fread(&new_pixel.green, 1, 1, image); fread(&new_pixel.red, 1, 1, image); } } return pixel_array; } /* * Print the blue, green, and red colour values of a pixel. * You don't need to change this function. */ void print_pixel(struct pixel p) { printf("(%u, %u, %u)\n", p.blue, p.green, p.red); } int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); // Print out some pixels from each of the image's corners. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { print_pixel(pixels[i][j]); print_pixel(pixels[i][width - 1 - j]); print_pixel(pixels[height - 1 - i][j]); print_pixel(pixels[height - 1 - i][width - 1 - j]); } } // Clean up: you need to do this! fclose(image); for (int i = 0 ; i < height ; i++) { free(pixels[i]); } free(pixels); return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-07T01:52:27.966Z
Question from Code
v1
How to initialize pixel_array properly
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" /* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */ void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 18, 0); fread(width, sizeof(int), 1, image); fseek(image, 22, 0); fread(height, sizeof(int), 1, image); fseek(image, 10, 0); fread(pixel_array_offset, sizeof(int), 1, image); } /* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **pixel_array = malloc(height * sizeof(struct pixel *)); fseek(image, pixel_array_offset, 0); for (int i = 0; i < height; i++) { pixel_array[i] = malloc(sizeof(struct pixel) * width); for (int j = 0 ; j < width ; j++) { struct pixel new_pixel; pixel_array[i][j] = new_pixel; fread(&new_pixel.blue, 1, 1, image); fread(&new_pixel.green, 1, 1, image); fread(&new_pixel.red, 1, 1, image); } } return pixel_array; } /* * Print the blue, green, and red colour values of a pixel. * You don't need to change this function. */ void print_pixel(struct pixel p) { printf("(%u, %u, %u)\n", p.blue, p.green, p.red); } int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); // Print out some pixels from each of the image's corners. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { print_pixel(pixels[i][j]); print_pixel(pixels[i][width - 1 - j]); print_pixel(pixels[height - 1 - i][j]); print_pixel(pixels[height - 1 - i][width - 1 - j]); } } // Clean up: you need to do this! fclose(image); for (int i = 0 ; i < height ; i++) { free(pixels[i]); } free(pixels); return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-07T02:00:45.898Z
Question from Code
v1
are there redundant brackets
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" /* * Read in the location of the pixel array, the image width, and the image * height in the given bitmap file. */ void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 18, 0); fread(width, sizeof(int), 1, image); fseek(image, 22, 0); fread(height, sizeof(int), 1, image); fseek(image, 10, 0); fread(pixel_array_offset, sizeof(int), 1, image); } /* * Read in pixel array by following these instructions: * * 1. First, allocate space for m `struct pixel *` values, where m is the * height of the image. Each pointer will eventually point to one row of * pixel data. * 2. For each pointer you just allocated, initialize it to point to * heap-allocated space for an entire row of pixel data. * 3. Use the given file and pixel_array_offset to initialize the actual * struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is * consistent with the bitmap file format. * NOTE: We've tested this assumption on the Teaching Lab machines, but * if you're trying to work on your own computer, we strongly recommend * checking this assumption! * 4. Return the address of the first `struct pixel *` you initialized. */ struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **pixel_array = malloc(height * sizeof(struct pixel *)); fseek(image, pixel_array_offset, 0); for (int i = 0; i < height; i++) { pixel_array[i] = malloc(sizeof(struct pixel) * width); for (int j = 0; j < width; j++) { struct pixel new_pixel; pixel_array[i][j] = new_pixel; fread(&(new_pixel.blue), 1, 1, image); fread(&(new_pixel.green), 1, 1, image); fread(&(new_pixel.red), 1, 1, image); } } return pixel_array; } /* * Print the blue, green, and red colour values of a pixel. * You don't need to change this function. */ void print_pixel(struct pixel p) { printf("(%u, %u, %u)\n", p.blue, p.green, p.red); }
null
null
63c648705f004bf4898cfc7f
2023-02-07T03:29:00.179Z
Question from Code
v1
Why may new_post be uninitialized
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } User *curr = *user_ptr_add; while (curr != NULL) { if (strcmp(curr->name, name)) { return 1; } curr = curr->next; } User new_user; strncpy(new_user.name, name, MAX_NAME); new_user.name[MAX_NAME-1] = '\0'; new_user.next = NULL; new_user.first_post = NULL; for (int i = 0 ; i < MAX_FRIENDS ; i++) { new_user.friends[i] = NULL; } *curr = new_user; return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr->name, name)) { return curr; } curr = curr->next; } return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { printf("User List"); while (curr != NULL) { printf("\t%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { char filename_str[MAX_NAME]; strncpy(filename_str, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; if (strcmp(filename, filename_str) != 1) { return 2; } FILE *profile_pic_file = fopen(filename, "r"); if (profile_pic_file == NULL) { return 1; } fclose(profile_pic_file); strncpy(user->profile_pic, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if (user1 == NULL || user2 == NULL) { return 4; } else if (user1 == user2) { return 3; } User *curr1 = user1->friends[0]; User *curr2 = user2->friends[0]; int user1_friends = 0; int user2_friends = 0; int i = 0; int alr_friends = 0; while (i < MAX_FRIENDS) { if (curr1 == user2 || curr2 == user1) { alr_friends = 1; } if (curr1 != NULL) { user1_friends++; curr1 = curr1->next; } if (curr2 != NULL) { user2_friends++; curr2 = curr2->next; } i++; } if ((user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) && alr_friends == 0) { return 2; } else if (alr_friends != 0) { return 1; } return 0; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if (user == NULL) { return 1; } FILE *profile_pic = fopen(user->profile_pic, "r"); char line[30]; if (profile_pic != NULL) { while (fgets(line, 30, profile_pic) != NULL) { line[29] = '\0'; printf("%s\n", line); } } fclose(profile_pic); printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *curr = user->first_post; while (curr != NULL) { printf("From: %s\n", curr->author); printf("Date: %s\n", ctime(curr->date)); printf("\n"); printf("%s\n", curr->contents); if (curr->next == NULL) { printf("------------------------------------------\n"); } else { printf("\n"); printf("===\n"); printf("\n"); } curr = curr->next; } return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (target == NULL || author == NULL) { return 2; } int friends = 0; for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (author->friends[i] == NULL) { break; } else if (author->friends[i] == target) { friends = 1; } } if (!friends) { return 1; } Post new_post; strncpy(new_post.author, author->name, MAX_NAME); new_post.author[MAX_NAME-1] = '\0'; strcpy(new_post.contents, contents); time(new_post.date); new_post.next = target->first_post; target->first_post = &new_post; return 0; } /* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { // User *curr = *user_ptr_del; // while (curr != NULL) { // for (int i = 0 ; i < MAX_FRIENDS ; i++) { // if (curr->friends[i] == NULL) { // break; // } else if (curr->friends[i]->name == name) { // for (int j = i ; j < MAX_FRIENDS ; j++) { // if (j == MAX_FRIENDS - 1) { // curr->friends[j] = NULL; // } else { // curr->friends[j] = curr->friends[j+1]; // } // } // } // } // curr = curr->next; // } User *prev = NULL; User *curr = *user_ptr_del; int found_user = 0; User *del_user; while (curr != NULL) { if (curr->name == name) { del_user = curr; found_user = 1; } if (found_user) { del_user = curr; prev->next = curr->next; } prev = curr; curr = curr->next; } for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (del_user->friends[i] == NULL) { break; } else { User *friend = del_user->friends[i]; int shift = 0; for (int j = 0 ; j < MAX_FRIENDS ; j++) { if (friend->friends[j] == del_user) { shift = 1; } if (shift) { if (j == MAX_FRIENDS - 1) { friend->friends[j] = NULL; } else { friend->friends[j] = friend->friends[j+1]; } } } } } return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-07T04:09:43.842Z
Question from Code
v1
why segmentation fault
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } User *prev = NULL; User *curr = *user_ptr_add; while (curr != NULL) { if (strcmp(curr->name, name)) { return 1; } prev = curr; curr = curr->next; } User *new_user_ptr = malloc(sizeof(User)); User new_user; strncpy(new_user.name, name, MAX_NAME); new_user.name[MAX_NAME-1] = '\0'; new_user.next = NULL; new_user.first_post = NULL; for (int i = 0 ; i < MAX_FRIENDS ; i++) { new_user.friends[i] = NULL; } *new_user_ptr = new_user; prev->next = new_user_ptr; return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr->name, name)) { return curr; } curr = curr->next; } return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { printf("User List"); while (curr != NULL) { printf("\t%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { char filename_str[MAX_NAME]; strncpy(filename_str, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; if (strcmp(filename, filename_str) != 1) { return 2; } FILE *profile_pic_file = fopen(filename, "r"); if (profile_pic_file == NULL) { return 1; } fclose(profile_pic_file); strncpy(user->profile_pic, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if (user1 == NULL || user2 == NULL) { return 4; } else if (user1 == user2) { return 3; } User *curr1 = user1->friends[0]; User *curr2 = user2->friends[0]; int user1_friends = 0; int user2_friends = 0; int i = 0; int alr_friends = 0; while (i < MAX_FRIENDS) { if (curr1 == user2 || curr2 == user1) { alr_friends = 1; } if (curr1 != NULL) { user1_friends++; curr1 = curr1->next; } if (curr2 != NULL) { user2_friends++; curr2 = curr2->next; } i++; } if ((user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) && alr_friends == 0) { return 2; } else if (alr_friends != 0) { return 1; } return 0; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if (user == NULL) { return 1; } FILE *profile_pic = fopen(user->profile_pic, "r"); char line[30]; if (profile_pic != NULL) { while (fgets(line, 30, profile_pic) != NULL) { line[29] = '\0'; printf("%s\n", line); } } fclose(profile_pic); printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *curr = user->first_post; while (curr != NULL) { printf("From: %s\n", curr->author); printf("Date: %s\n", ctime(curr->date)); printf("\n"); printf("%s\n", curr->contents); if (curr->next == NULL) { printf("------------------------------------------\n"); } else { printf("\n"); printf("===\n"); printf("\n"); } curr = curr->next; } return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (target == NULL || author == NULL) { return 2; } int friends = 0; for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (author->friends[i] == NULL) { break; } else if (author->friends[i] == target) { friends = 1; } } if (!friends) { return 1; } Post *new_post_ptr = malloc(sizeof(Post)); Post new_post; strncpy(new_post.author, author->name, MAX_NAME); new_post.author[MAX_NAME-1] = '\0'; new_post.contents = contents; new_post.date = NULL; time(new_post.date); new_post.next = target->first_post; *new_post_ptr = new_post; target->first_post = new_post_ptr; return 0; } /* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { // User *curr = *user_ptr_del; // while (curr != NULL) { // for (int i = 0 ; i < MAX_FRIENDS ; i++) { // if (curr->friends[i] == NULL) { // break; // } else if (curr->friends[i]->name == name) { // for (int j = i ; j < MAX_FRIENDS ; j++) { // if (j == MAX_FRIENDS - 1) { // curr->friends[j] = NULL; // } else { // curr->friends[j] = curr->friends[j+1]; // } // } // } // } // curr = curr->next; // } User *prev = NULL; User *curr = *user_ptr_del; int found_user = 0; User *del_user; while (curr != NULL) { if (strcmp(curr->name, name)) { del_user = curr; found_user = 1; } if (found_user) { prev->next = curr->next; } prev = curr; curr = curr->next; } for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (del_user->friends[i] == NULL) { break; } else { User *friend = del_user->friends[i]; int shift = 0; for (int j = 0 ; j < MAX_FRIENDS ; j++) { if (friend->friends[j] == del_user) { shift = 1; } if (shift) { if (j == MAX_FRIENDS - 1) { friend->friends[j] = NULL; } else { friend->friends[j] = friend->friends[j+1]; } } } } } free(del_user); return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> #include "friends.h" #define INPUT_BUFFER_SIZE 256 #define INPUT_ARG_MAX_NUM 12 #define DELIM " \n" /* * Print a formatted error message to stderr. */ void error(char *msg) { fprintf(stderr, "Error: %s\n", msg); } /* * Read and process commands * Return: -1 for quit command * 0 otherwise */ int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { User *user_list = *user_list_ptr; if (cmd_argc <= 0) { return 0; } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) { return -1; } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) { switch (create_user(cmd_argv[1], user_list_ptr)) { case 1: error("user by this name already exists"); break; case 2: error("username is too long"); break; } } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) { list_users(user_list); } else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) { User *user = find_user(cmd_argv[1], user_list); if (user == NULL) { error("user not found"); } else { switch (update_pic(user, cmd_argv[2])) { case 1: error("file not found"); break; case 2: error("filename too long"); break; } } } else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) { if (delete_user(cmd_argv[1], user_list_ptr) == 1) { error("user by this name does not exist"); } } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) { switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { case 1: error("users are already friends"); break; case 2: error("at least one user you entered has the max number of friends"); break; case 3: error("you must enter two different users"); break; case 4: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) { // first determine how long a string we need int space_needed = 0; for (int i = 3; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } // allocate the space char *contents = malloc(space_needed); // copy in the bits to make a single string strcpy(contents, cmd_argv[3]); for (int i = 4; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); } User *author = find_user(cmd_argv[1], user_list); User *target = find_user(cmd_argv[2], user_list); switch (make_post(author, target, contents)) { case 1: error("the users are not friends"); break; case 2: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) { User *user = find_user(cmd_argv[1], user_list); if (print_user(user) == 1) { error("user not found"); } } else { error("Incorrect syntax"); } return 0; } /* * Tokenize the string stored in cmd. * Return the number of tokens, and store the tokens in cmd_argv. */ int tokenize(char *cmd, char **cmd_argv) { int cmd_argc = 0; char *next_token = strtok(cmd, DELIM); while (next_token != NULL) { if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { error("too many arguments!"); cmd_argc = 0; break; } cmd_argv[cmd_argc] = next_token; cmd_argc++; next_token = strtok(NULL, DELIM); } return cmd_argc; } int main(int argc, char* argv[]) { int batch_mode = (argc == 2); char input[INPUT_BUFFER_SIZE]; FILE *input_stream; // Create the heads of the empty data structure User *user_list = NULL; if (batch_mode) { input_stream = fopen(argv[1], "r"); if (input_stream == NULL) { perror("Error opening file"); exit(1); } } else { // interactive mode input_stream = stdin; } printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> "); while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { // only echo the line in batch mode since in interactive mode the user // just typed the line if (batch_mode) { printf("%s", input); } char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { break; // can only reach if quit command was entered } printf("> "); } if (batch_mode) { fclose(input_stream); } // Delete all users. This should free all heap memory that was allocated // during the run of the program. Uncomment after you've implemented // deleting users. /*printf("Freeing all heap space.\n"); while (user_list != NULL) { delete_user(user_list->name, &user_list); }*/ return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-07T04:10:15.611Z
Question from Code
v1
what is causing the segmentation fault
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } User *prev = NULL; User *curr = *user_ptr_add; while (curr != NULL) { if (strcmp(curr->name, name)) { return 1; } prev = curr; curr = curr->next; } User *new_user_ptr = malloc(sizeof(User)); User new_user; strncpy(new_user.name, name, MAX_NAME); new_user.name[MAX_NAME-1] = '\0'; new_user.next = NULL; new_user.first_post = NULL; for (int i = 0 ; i < MAX_FRIENDS ; i++) { new_user.friends[i] = NULL; } *new_user_ptr = new_user; prev->next = new_user_ptr; return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr->name, name)) { return curr; } curr = curr->next; } return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { printf("User List"); while (curr != NULL) { printf("\t%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { char filename_str[MAX_NAME]; strncpy(filename_str, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; if (strcmp(filename, filename_str) != 1) { return 2; } FILE *profile_pic_file = fopen(filename, "r"); if (profile_pic_file == NULL) { return 1; } fclose(profile_pic_file); strncpy(user->profile_pic, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if (user1 == NULL || user2 == NULL) { return 4; } else if (user1 == user2) { return 3; } User *curr1 = user1->friends[0]; User *curr2 = user2->friends[0]; int user1_friends = 0; int user2_friends = 0; int i = 0; int alr_friends = 0; while (i < MAX_FRIENDS) { if (curr1 == user2 || curr2 == user1) { alr_friends = 1; } if (curr1 != NULL) { user1_friends++; curr1 = curr1->next; } if (curr2 != NULL) { user2_friends++; curr2 = curr2->next; } i++; } if ((user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) && alr_friends == 0) { return 2; } else if (alr_friends != 0) { return 1; } return 0; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if (user == NULL) { return 1; } FILE *profile_pic = fopen(user->profile_pic, "r"); char line[30]; if (profile_pic != NULL) { while (fgets(line, 30, profile_pic) != NULL) { line[29] = '\0'; printf("%s\n", line); } } fclose(profile_pic); printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *curr = user->first_post; while (curr != NULL) { printf("From: %s\n", curr->author); printf("Date: %s\n", ctime(curr->date)); printf("\n"); printf("%s\n", curr->contents); if (curr->next == NULL) { printf("------------------------------------------\n"); } else { printf("\n"); printf("===\n"); printf("\n"); } curr = curr->next; } return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (target == NULL || author == NULL) { return 2; } int friends = 0; for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (author->friends[i] == NULL) { break; } else if (author->friends[i] == target) { friends = 1; } } if (!friends) { return 1; } Post *new_post_ptr = malloc(sizeof(Post)); Post new_post; strncpy(new_post.author, author->name, MAX_NAME); new_post.author[MAX_NAME-1] = '\0'; new_post.contents = contents; new_post.date = NULL; time(new_post.date); new_post.next = target->first_post; *new_post_ptr = new_post; target->first_post = new_post_ptr; return 0; } /* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { // User *curr = *user_ptr_del; // while (curr != NULL) { // for (int i = 0 ; i < MAX_FRIENDS ; i++) { // if (curr->friends[i] == NULL) { // break; // } else if (curr->friends[i]->name == name) { // for (int j = i ; j < MAX_FRIENDS ; j++) { // if (j == MAX_FRIENDS - 1) { // curr->friends[j] = NULL; // } else { // curr->friends[j] = curr->friends[j+1]; // } // } // } // } // curr = curr->next; // } User *prev = NULL; User *curr = *user_ptr_del; int found_user = 0; User *del_user; while (curr != NULL) { if (strcmp(curr->name, name)) { del_user = curr; found_user = 1; } if (found_user) { prev->next = curr->next; } prev = curr; curr = curr->next; } for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (del_user->friends[i] == NULL) { break; } else { User *friend = del_user->friends[i]; int shift = 0; for (int j = 0 ; j < MAX_FRIENDS ; j++) { if (friend->friends[j] == del_user) { shift = 1; } if (shift) { if (j == MAX_FRIENDS - 1) { friend->friends[j] = NULL; } else { friend->friends[j] = friend->friends[j+1]; } } } } } free(del_user); return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> #include "friends.h" #define INPUT_BUFFER_SIZE 256 #define INPUT_ARG_MAX_NUM 12 #define DELIM " \n" /* * Print a formatted error message to stderr. */ void error(char *msg) { fprintf(stderr, "Error: %s\n", msg); } /* * Read and process commands * Return: -1 for quit command * 0 otherwise */ int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { User *user_list = *user_list_ptr; if (cmd_argc <= 0) { return 0; } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) { return -1; } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) { switch (create_user(cmd_argv[1], user_list_ptr)) { case 1: error("user by this name already exists"); break; case 2: error("username is too long"); break; } } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) { list_users(user_list); } else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) { User *user = find_user(cmd_argv[1], user_list); if (user == NULL) { error("user not found"); } else { switch (update_pic(user, cmd_argv[2])) { case 1: error("file not found"); break; case 2: error("filename too long"); break; } } } else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) { if (delete_user(cmd_argv[1], user_list_ptr) == 1) { error("user by this name does not exist"); } } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) { switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { case 1: error("users are already friends"); break; case 2: error("at least one user you entered has the max number of friends"); break; case 3: error("you must enter two different users"); break; case 4: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) { // first determine how long a string we need int space_needed = 0; for (int i = 3; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } // allocate the space char *contents = malloc(space_needed); // copy in the bits to make a single string strcpy(contents, cmd_argv[3]); for (int i = 4; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); } User *author = find_user(cmd_argv[1], user_list); User *target = find_user(cmd_argv[2], user_list); switch (make_post(author, target, contents)) { case 1: error("the users are not friends"); break; case 2: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) { User *user = find_user(cmd_argv[1], user_list); if (print_user(user) == 1) { error("user not found"); } } else { error("Incorrect syntax"); } return 0; } /* * Tokenize the string stored in cmd. * Return the number of tokens, and store the tokens in cmd_argv. */ int tokenize(char *cmd, char **cmd_argv) { int cmd_argc = 0; char *next_token = strtok(cmd, DELIM); while (next_token != NULL) { if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { error("too many arguments!"); cmd_argc = 0; break; } cmd_argv[cmd_argc] = next_token; cmd_argc++; next_token = strtok(NULL, DELIM); } return cmd_argc; } int main(int argc, char* argv[]) { int batch_mode = (argc == 2); char input[INPUT_BUFFER_SIZE]; FILE *input_stream; // Create the heads of the empty data structure User *user_list = NULL; if (batch_mode) { input_stream = fopen(argv[1], "r"); if (input_stream == NULL) { perror("Error opening file"); exit(1); } } else { // interactive mode input_stream = stdin; } printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> "); while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { // only echo the line in batch mode since in interactive mode the user // just typed the line if (batch_mode) { printf("%s", input); } char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { break; // can only reach if quit command was entered } printf("> "); } if (batch_mode) { fclose(input_stream); } // Delete all users. This should free all heap memory that was allocated // during the run of the program. Uncomment after you've implemented // deleting users. /*printf("Freeing all heap space.\n"); while (user_list != NULL) { delete_user(user_list->name, &user_list); }*/ return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-07T04:11:16.194Z
Question from Code
v1
how to fix the code
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } User *prev = NULL; User *curr = *user_ptr_add; while (curr != NULL) { if (strcmp(curr->name, name)) { return 1; } prev = curr; curr = curr->next; } User *new_user_ptr = malloc(sizeof(User)); User new_user; strncpy(new_user.name, name, MAX_NAME); new_user.name[MAX_NAME-1] = '\0'; new_user.next = NULL; new_user.first_post = NULL; for (int i = 0 ; i < MAX_FRIENDS ; i++) { new_user.friends[i] = NULL; } *new_user_ptr = new_user; prev->next = new_user_ptr; return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-08T15:07:56.766Z
Question from Code
v1
why is there a seg fault
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } User *prev = NULL; User *curr = *user_ptr_add; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return 1; } prev = curr; curr = curr->next; } User *new_user_ptr = malloc(sizeof(User)); strncpy(new_user_ptr->name, name, MAX_NAME); new_user_ptr->name[MAX_NAME-1] = '\0'; new_user_ptr->next = NULL; new_user_ptr->first_post = NULL; for (int i = 0 ; i < MAX_FRIENDS ; i++) { new_user_ptr->friends[i] = NULL; } if (*user_ptr_add == NULL) { // no user in list user_ptr_add[0] = new_user_ptr; } else { prev->next = new_user_ptr; } return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { User *curr = (User *) head; while (curr != NULL) { // printf("Iterating"); if (strcmp(curr->name, name) == 0) { // TODO: adding == 0 causes seg fault return curr; } curr = curr->next; } return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { printf("User List\n"); while (curr != NULL) { printf("\t%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { char filename_str[MAX_NAME]; strncpy(filename_str, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; if (strcmp(filename, filename_str) != 1) { return 2; } FILE *profile_pic_file = fopen(filename, "r"); if (profile_pic_file == NULL) { return 1; } fclose(profile_pic_file); strncpy(user->profile_pic, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if (user1 == NULL || user2 == NULL) { return 4; } else if (user1 == user2) { return 3; } User *curr1 = user1->friends[0]; User *curr2 = user2->friends[0]; int user1_friends = 0; int user2_friends = 0; int i = 0; int alr_friends = 0; while (i < MAX_FRIENDS) { if (curr1 == user2 || curr2 == user1) { alr_friends = 1; } if (curr1 != NULL) { user1_friends++; curr1 = curr1->next; } if (curr2 != NULL) { user2_friends++; curr2 = curr2->next; } i++; } if ((user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) && alr_friends == 0) { return 2; } else if (alr_friends != 0) { return 1; } return 0; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if (user == NULL) { return 1; } FILE *profile_pic = fopen(user->profile_pic, "r"); char line[30]; if (profile_pic != NULL) { while (fgets(line, 30, profile_pic) != NULL) { line[29] = '\0'; printf("%s\n", line); } } fclose(profile_pic); printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *curr = user->first_post; while (curr != NULL) { printf("From: %s\n", curr->author); printf("Date: %s\n", ctime(curr->date)); printf("\n"); printf("%s\n", curr->contents); if (curr->next == NULL) { printf("------------------------------------------\n"); } else { printf("\n"); printf("===\n"); printf("\n"); } curr = curr->next; } return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (target == NULL || author == NULL) { return 2; } int friends = 0; for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (author->friends[i] == NULL) { break; } else if (author->friends[i] == target) { friends = 1; } } if (!friends) { return 1; } Post *new_post_ptr = malloc(sizeof(Post)); Post new_post; strncpy(new_post.author, author->name, MAX_NAME); new_post.author[MAX_NAME-1] = '\0'; new_post.contents = contents; new_post.date = NULL; time(new_post.date); new_post.next = target->first_post; *new_post_ptr = new_post; target->first_post = new_post_ptr; return 0; } /* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { // User *curr = *user_ptr_del; // while (curr != NULL) { // for (int i = 0 ; i < MAX_FRIENDS ; i++) { // if (curr->friends[i] == NULL) { // break; // } else if (curr->friends[i]->name == name) { // for (int j = i ; j < MAX_FRIENDS ; j++) { // if (j == MAX_FRIENDS - 1) { // curr->friends[j] = NULL; // } else { // curr->friends[j] = curr->friends[j+1]; // } // } // } // } // curr = curr->next; // } User *prev = NULL; User *curr = *user_ptr_del; int found_user = 0; User *del_user; while (curr != NULL) { if (strcmp(curr->name, name)) { del_user = curr; found_user = 1; } if (found_user) { prev->next = curr->next; } prev = curr; curr = curr->next; } for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (del_user->friends[i] == NULL) { break; } else { User *friend = del_user->friends[i]; int shift = 0; for (int j = 0 ; j < MAX_FRIENDS ; j++) { if (friend->friends[j] == del_user) { shift = 1; } if (shift) { if (j == MAX_FRIENDS - 1) { friend->friends[j] = NULL; } else { friend->friends[j] = friend->friends[j+1]; } } } } } free(del_user); return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> #include "friends.h" #define INPUT_BUFFER_SIZE 256 #define INPUT_ARG_MAX_NUM 12 #define DELIM " \n" /* * Print a formatted error message to stderr. */ void error(char *msg) { fprintf(stderr, "Error: %s\n", msg); } /* * Read and process commands * Return: -1 for quit command * 0 otherwise */ int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { User *user_list = *user_list_ptr; if (cmd_argc <= 0) { return 0; } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) { return -1; } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) { switch (create_user(cmd_argv[1], user_list_ptr)) { case 1: error("user by this name already exists"); break; case 2: error("username is too long"); break; } } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) { list_users(user_list); } else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) { User *user = find_user(cmd_argv[1], user_list); if (user == NULL) { error("user not found"); } else { switch (update_pic(user, cmd_argv[2])) { case 1: error("file not found"); break; case 2: error("filename too long"); break; } } } else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) { if (delete_user(cmd_argv[1], user_list_ptr) == 1) { error("user by this name does not exist"); } } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) { switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { case 1: error("users are already friends"); break; case 2: error("at least one user you entered has the max number of friends"); break; case 3: error("you must enter two different users"); break; case 4: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) { // first determine how long a string we need int space_needed = 0; for (int i = 3; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } // allocate the space char *contents = malloc(space_needed); // copy in the bits to make a single string strcpy(contents, cmd_argv[3]); for (int i = 4; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); } User *author = find_user(cmd_argv[1], user_list); User *target = find_user(cmd_argv[2], user_list); switch (make_post(author, target, contents)) { case 1: error("the users are not friends"); break; case 2: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) { User *user = find_user(cmd_argv[1], user_list); if (print_user(user) == 1) { error("user not found"); } } else { error("Incorrect syntax"); } return 0; } /* * Tokenize the string stored in cmd. * Return the number of tokens, and store the tokens in cmd_argv. */ int tokenize(char *cmd, char **cmd_argv) { int cmd_argc = 0; char *next_token = strtok(cmd, DELIM); while (next_token != NULL) { if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { error("too many arguments!"); cmd_argc = 0; break; } cmd_argv[cmd_argc] = next_token; cmd_argc++; next_token = strtok(NULL, DELIM); } return cmd_argc; } int main(int argc, char* argv[]) { int batch_mode = (argc == 2); char input[INPUT_BUFFER_SIZE]; FILE *input_stream; // Create the heads of the empty data structure User *user_list = NULL; if (batch_mode) { input_stream = fopen(argv[1], "r"); if (input_stream == NULL) { perror("Error opening file"); exit(1); } } else { // interactive mode input_stream = stdin; } printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> "); while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { // only echo the line in batch mode since in interactive mode the user // just typed the line if (batch_mode) { printf("%s", input); } char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { break; // can only reach if quit command was entered } printf("> "); } if (batch_mode) { fclose(input_stream); } // Delete all users. This should free all heap memory that was allocated // during the run of the program. Uncomment after you've implemented // deleting users. /*printf("Freeing all heap space.\n"); while (user_list != NULL) { delete_user(user_list->name, &user_list); }*/ return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-08T15:08:20.406Z
Question from Code
v1
what is causing the seg fault?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } User *prev = NULL; User *curr = *user_ptr_add; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return 1; } prev = curr; curr = curr->next; } User *new_user_ptr = malloc(sizeof(User)); strncpy(new_user_ptr->name, name, MAX_NAME); new_user_ptr->name[MAX_NAME-1] = '\0'; new_user_ptr->next = NULL; new_user_ptr->first_post = NULL; for (int i = 0 ; i < MAX_FRIENDS ; i++) { new_user_ptr->friends[i] = NULL; } if (*user_ptr_add == NULL) { // no user in list user_ptr_add[0] = new_user_ptr; } else { prev->next = new_user_ptr; } return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { User *curr = (User *) head; while (curr != NULL) { // printf("Iterating"); if (strcmp(curr->name, name) == 0) { // TODO: adding == 0 causes seg fault return curr; } curr = curr->next; } return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { printf("User List\n"); while (curr != NULL) { printf("\t%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { char filename_str[MAX_NAME]; strncpy(filename_str, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; if (strcmp(filename, filename_str) != 1) { return 2; } FILE *profile_pic_file = fopen(filename, "r"); if (profile_pic_file == NULL) { return 1; } fclose(profile_pic_file); strncpy(user->profile_pic, filename, MAX_NAME); filename_str[MAX_NAME-1] = '\0'; return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if (user1 == NULL || user2 == NULL) { return 4; } else if (user1 == user2) { return 3; } User *curr1 = user1->friends[0]; User *curr2 = user2->friends[0]; int user1_friends = 0; int user2_friends = 0; int i = 0; int alr_friends = 0; while (i < MAX_FRIENDS) { if (curr1 == user2 || curr2 == user1) { alr_friends = 1; } if (curr1 != NULL) { user1_friends++; curr1 = curr1->next; } if (curr2 != NULL) { user2_friends++; curr2 = curr2->next; } i++; } if ((user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) && alr_friends == 0) { return 2; } else if (alr_friends != 0) { return 1; } return 0; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if (user == NULL) { return 1; } FILE *profile_pic = fopen(user->profile_pic, "r"); char line[30]; if (profile_pic != NULL) { while (fgets(line, 30, profile_pic) != NULL) { line[29] = '\0'; printf("%s\n", line); } } fclose(profile_pic); printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *curr = user->first_post; while (curr != NULL) { printf("From: %s\n", curr->author); printf("Date: %s\n", ctime(curr->date)); printf("\n"); printf("%s\n", curr->contents); if (curr->next == NULL) { printf("------------------------------------------\n"); } else { printf("\n"); printf("===\n"); printf("\n"); } curr = curr->next; } return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (target == NULL || author == NULL) { return 2; } int friends = 0; for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (author->friends[i] == NULL) { break; } else if (author->friends[i] == target) { friends = 1; } } if (!friends) { return 1; } Post *new_post_ptr = malloc(sizeof(Post)); Post new_post; strncpy(new_post.author, author->name, MAX_NAME); new_post.author[MAX_NAME-1] = '\0'; new_post.contents = contents; new_post.date = NULL; time(new_post.date); new_post.next = target->first_post; *new_post_ptr = new_post; target->first_post = new_post_ptr; return 0; } /* * From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. */ int delete_user(const char *name, User **user_ptr_del) { // User *curr = *user_ptr_del; // while (curr != NULL) { // for (int i = 0 ; i < MAX_FRIENDS ; i++) { // if (curr->friends[i] == NULL) { // break; // } else if (curr->friends[i]->name == name) { // for (int j = i ; j < MAX_FRIENDS ; j++) { // if (j == MAX_FRIENDS - 1) { // curr->friends[j] = NULL; // } else { // curr->friends[j] = curr->friends[j+1]; // } // } // } // } // curr = curr->next; // } User *prev = NULL; User *curr = *user_ptr_del; int found_user = 0; User *del_user; while (curr != NULL) { if (strcmp(curr->name, name)) { del_user = curr; found_user = 1; } if (found_user) { prev->next = curr->next; } prev = curr; curr = curr->next; } for (int i = 0 ; i < MAX_FRIENDS ; i++) { if (del_user->friends[i] == NULL) { break; } else { User *friend = del_user->friends[i]; int shift = 0; for (int j = 0 ; j < MAX_FRIENDS ; j++) { if (friend->friends[j] == del_user) { shift = 1; } if (shift) { if (j == MAX_FRIENDS - 1) { friend->friends[j] = NULL; } else { friend->friends[j] = friend->friends[j+1]; } } } } } free(del_user); return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> #include "friends.h" #define INPUT_BUFFER_SIZE 256 #define INPUT_ARG_MAX_NUM 12 #define DELIM " \n" /* * Print a formatted error message to stderr. */ void error(char *msg) { fprintf(stderr, "Error: %s\n", msg); } /* * Read and process commands * Return: -1 for quit command * 0 otherwise */ int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { User *user_list = *user_list_ptr; if (cmd_argc <= 0) { return 0; } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) { return -1; } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) { switch (create_user(cmd_argv[1], user_list_ptr)) { case 1: error("user by this name already exists"); break; case 2: error("username is too long"); break; } } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) { list_users(user_list); } else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) { User *user = find_user(cmd_argv[1], user_list); if (user == NULL) { error("user not found"); } else { switch (update_pic(user, cmd_argv[2])) { case 1: error("file not found"); break; case 2: error("filename too long"); break; } } } else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) { if (delete_user(cmd_argv[1], user_list_ptr) == 1) { error("user by this name does not exist"); } } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) { switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { case 1: error("users are already friends"); break; case 2: error("at least one user you entered has the max number of friends"); break; case 3: error("you must enter two different users"); break; case 4: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) { // first determine how long a string we need int space_needed = 0; for (int i = 3; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } // allocate the space char *contents = malloc(space_needed); // copy in the bits to make a single string strcpy(contents, cmd_argv[3]); for (int i = 4; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); } User *author = find_user(cmd_argv[1], user_list); User *target = find_user(cmd_argv[2], user_list); switch (make_post(author, target, contents)) { case 1: error("the users are not friends"); break; case 2: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) { User *user = find_user(cmd_argv[1], user_list); if (print_user(user) == 1) { error("user not found"); } } else { error("Incorrect syntax"); } return 0; } /* * Tokenize the string stored in cmd. * Return the number of tokens, and store the tokens in cmd_argv. */ int tokenize(char *cmd, char **cmd_argv) { int cmd_argc = 0; char *next_token = strtok(cmd, DELIM); while (next_token != NULL) { if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { error("too many arguments!"); cmd_argc = 0; break; } cmd_argv[cmd_argc] = next_token; cmd_argc++; next_token = strtok(NULL, DELIM); } return cmd_argc; } int main(int argc, char* argv[]) { int batch_mode = (argc == 2); char input[INPUT_BUFFER_SIZE]; FILE *input_stream; // Create the heads of the empty data structure User *user_list = NULL; if (batch_mode) { input_stream = fopen(argv[1], "r"); if (input_stream == NULL) { perror("Error opening file"); exit(1); } } else { // interactive mode input_stream = stdin; } printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> "); while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { // only echo the line in batch mode since in interactive mode the user // just typed the line if (batch_mode) { printf("%s", input); } char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { break; // can only reach if quit command was entered } printf("> "); } if (batch_mode) { fclose(input_stream); } // Delete all users. This should free all heap memory that was allocated // during the run of the program. Uncomment after you've implemented // deleting users. /*printf("Freeing all heap space.\n"); while (user_list != NULL) { delete_user(user_list->name, &user_list); }*/ return 0; }
null
null
63c648705f004bf4898cfc7f
2023-02-08T15:09:33.199Z
Help Fix Code
v1
null
/* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { User *curr = (User *) head; while (curr != NULL) { // printf("Iterating"); if (strcmp(curr->name, name) == 0) { // TODO: adding == 0 causes seg fault return curr; } curr = curr->next; } return NULL;
/* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */
null
63c648705f004bf4898cfc7f
2023-02-08T15:10:33.883Z
Help Fix Code
v1
null
/* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { User *curr = (User *) head; while (curr != NULL) { // printf("Iterating"); if (strcmp(curr->name, name) == 0) { // TODO: adding == 0 causes seg fault return curr; } curr = curr->next; } return NULL;
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists.
null
63c648705f004bf4898cfc7f
2023-02-20T18:54:45.700Z
General Question
v1
are makefiles binary files
null
null
null
63c648705f004bf4898cfc7f
2023-02-23T19:30:13.010Z
Question from Code
v1
How to fix the segmentation fault
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" /* Return a pointer to the rule with the given target. If the target doesn't exist, return null. */ Rule *find_rule(Rule **rule_head, char target[MAXLINE]) { Rule *curr_rule = *rule_head; while (curr_rule != NULL) { if (strcmp(curr_rule->target, target) == 0) { return curr_rule; } curr_rule = curr_rule->next_rule; } return NULL; } /* Create a new rule with the given target. Insert it at the tail of the list * of rules whose head is pointed to by rule_head. Return a pointer to the created rule. */ Rule *create_rule(Rule **rule_head, char target[MAXLINE]) { // Iterate to end of rules Rule *prev = NULL; Rule *curr = *rule_head; while (curr != NULL) { prev = curr; curr = curr->next_rule; } // Create the rule Rule *new_rule = malloc(sizeof(Rule)); new_rule->target = malloc((strlen(target) + 1) * sizeof(char)); strcpy(new_rule->target, target); new_rule->dependencies = NULL; new_rule->actions = NULL; new_rule->next_rule = NULL; // Link the rule if (rule_head == NULL) { rule_head[0] = new_rule; } else { prev->next_rule = new_rule; } return new_rule; } /* Search for the rule with the given target, where the list of rules * has their head pointed at rule_head and add the dependency with corr_rule. * Precondition: * - the rule with the given target exists */ void add_dependency(Rule **rule_head, char target[MAXLINE], Rule *corr_rule) { // Find rule Rule *curr_rule = *rule_head; while (strcmp(curr_rule->target, target) != 0) { curr_rule = curr_rule->next_rule; } // Create the dependency Dependency *new_dep = malloc(sizeof(Dependency)); new_dep->rule = corr_rule; new_dep->next_dep = NULL; // Link the dependency Dependency *prev_dep = NULL; Dependency *curr_dep = curr_rule->dependencies; while (curr_dep != NULL) { prev_dep = curr_dep; curr_dep = curr_dep->next_dep; } if (curr_rule->dependencies == NULL) { curr_rule->dependencies = new_dep; } else { prev_dep->next_dep = new_dep; } } /* Search for the rule with the given target, where the list of rules * has their head pointed at rule_head and link an action with args * to the rule. * Precondition: * - the rule with the given target exists */ void add_action(Rule **rule_head, char target[MAXLINE], char **args) { // Find Rule Rule *curr_rule = *rule_head; while (strcmp(curr_rule->target, target) != 0) { curr_rule = curr_rule->next_rule; } // Create the action Action *new_action = malloc(sizeof(Action)); new_action->args = args; new_action->next_act = NULL; // Link the action Action *prev_action = NULL; Action *curr_action = curr_rule->actions; while (curr_action != NULL) { prev_action = curr_action; curr_action = curr_action->next_act; } if (curr_rule->actions == NULL) { curr_rule->actions = new_action; } else { prev_action->next_act = new_action; } } /* Parse through the given (target) line and create and link the necessary dependencies and targets. * Return a pointer to the rule object with the given target in line */ Rule *parse_target_line(Rule **rule_head, char line[MAXLINE]) { int i = 0; int j; char target[MAXLINE]; char dep[MAXLINE]; while (line[i] != ' ') { target[i] = line[i]; // TODO: line seems to be characters generated from reading binary file i++; } target[i] = '\0'; Rule *rule; if (line[i+2] == '\n') { // no dependencies if (find_rule(rule_head, target) == NULL) { // target doesn't exist rule = create_rule(rule_head, target); } else { rule = find_rule(rule_head, target); } return rule; } // Move index to first dependency i += 3; while (line[i] != '\n') { // Iterate over line // Initialize index for dependency j = 0; // Iterate over dependency while (line[i] != ' ' && line[i] != '\n') { dep[j] = line[i]; j++; i++; } dep[j] = '\0'; if (find_rule(rule_head, target) == NULL) { // target doesn't exist create_rule(rule_head, target); } Rule *corr_rule = find_rule(rule_head, dep); if (corr_rule == NULL) { // corr_rule doesn't exist corr_rule = create_rule(rule_head, dep); } add_dependency(rule_head, target, corr_rule); // Move index to next dependency if (line[i] != '\n') { i++; } else { break; } } return find_rule(rule_head, target); } /* Parse through the given (action) line and create and link the necessary actions and rules. */ void parse_action_line(Rule *rule, char line[MAXLINE]) { char **args = malloc(MAXLINE * sizeof(char *)); char word[MAXLINE]; int num_words = 0; int i = 1; // first index is '\t' int k; while (i != '\n') { // iterate over line k = 0; // Iterate over words in action while (line[i] != ' ' && line[i] != '\n') { word[k] = line[i]; k++; i++; } word[k] = '\0'; args[num_words] = malloc(sizeof(char) * (strlen(word) + 1)); strcpy(args[num_words], word); num_words++; if (line[i] == ' ') { i++; // Move index to the next word } } args[num_words] = malloc(sizeof(NULL)); args[num_words] = NULL; // Create action Action *new_action = malloc(sizeof(Action)); new_action->args = args; new_action->next_act = NULL; // Link action to rule Action *prev_action = NULL; Action *curr_action = rule->actions; while (curr_action != NULL) { prev_action = curr_action; curr_action = curr_action->next_act; } if (rule->actions == NULL) { rule->actions = new_action; } else { prev_action->next_act = new_action; } } /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. Rule *rule_head = NULL; char line[MAXLINE]; Rule *rule; while (fgets(line, MAXLINE, fp) != NULL) { if (is_comment_or_empty(line)) { continue; } else if (line[0] != ' ' && line[0] != '\t') { // target line printf("%s\n", line); rule = parse_target_line(&rule_head, line); } else { // action line parse_action_line(rule, line); } } // char target[MAXLINE]; // // Rule *curr_rule = new_rule; // while (fgets(line, max_line, fp) != NULL) { // if (line[0] != ' ' && line[0] != '\t') { // int i = 0; // while (line[i] != ' ') { // target[i] = line[i]; // i++; // } // target[i] = '\0'; // // // initialize target attribute for curr_rule // curr_rule->target = malloc(strlen(target) + 1); // strcpy(curr_rule->target, target); // // // initialize actions attribute to NULL // curr_rule->actions = NULL; // // // initialize next_rule attribute to NULL // curr_rule->next_rule = NULL; // // // initialize dependencies attribute to NULL // curr_rule->dependencies = NULL; // // if (line[i] == '\n' || line[i+1] == '\n' || line[i+2] == '\n' || line[i+3] == '\n') { // // there are no dependencies so don't iterate through loop // } // // i += 3; // move index to first dependency // curr_rule->dependencies = malloc(sizeof(Dependency)); // // Dependency *curr_dependency = new_rule->dependencies; // // // iterate through dependencies // while (line[i] != '\n') { // // initialize new dependency // curr_dependency->rule = malloc(sizeof(Rule)); // curr_dependency->next_dep = NULL; // // // initialize new rule associated with new dependency // curr_rule->dependencies = curr_dependency->rule; // curr_rule->next_rule = curr_dependency->rule; // curr_rule = curr_rule->next_rule; // // int j = 0; // char dependency[MAXLINE]; // // // initialize the dependency's corresponding Rule // while (line[i] != '\n' && line[i] != ' ') { // dependency[j] = line[i]; // j++; // i++; // } // } // } // for (int i = 0 ; i < max_line ; i++) { // if (line[i] == ' ') { // line[i] = '\0'; // } else if (line[i] == '\n') { // line[i] = '\0'; // break; // } // } // } return rule_head; } /****************************************************************************** * These helper functions are provided for you. Do not modify them. *****************************************************************************/ /* Print the list of actions */ void print_actions(Action *act) { while(act != NULL) { if(act->args == NULL) { fprintf(stderr, "ERROR: action with NULL args\n"); act = act->next_act; continue; } printf("\t"); int i = 0; while(act->args[i] != NULL) { printf("%s ", act->args[i]) ; i++; } printf("\n"); act = act->next_act; } } /* Print the list of rules to stdout in makefile format. If the output of print_rules is saved to a file, it should be possible to use it to run make correctly. */ void print_rules(Rule *rules){ Rule *cur = rules; while (cur != NULL) { if (cur->dependencies || cur->actions) { // Print target printf("%s : ", cur->target); // Print dependencies Dependency *dep = cur->dependencies; while (dep != NULL){ if(dep->rule->target == NULL) { fprintf(stderr, "ERROR: dependency with NULL rule\n"); } printf("%s ", dep->rule->target); dep = dep->next_dep; } printf("\n"); // Print actions print_actions(cur->actions); } cur = cur->next_rule; } } /* Return 1 if the line is a comment line, as defined on the assignment handout. Return 0 otherwise. */ int is_comment_or_empty(const char *line) { for (int i = 0; i < strlen(line); i++){ if (line[i] == '#') { return 1; } if (line[i] != '\t' && line[i] != ' ') { return 0; } } return 1; } /* Convert an array of args to a single space-separated string in buffer. Returns buffer. Note that memory for args and buffer should be allocted by the caller. */ char *args_to_string(char **args, char *buffer, int size) { buffer[0] = '\0'; int i = 0; while (args[i] != NULL) { strncat(buffer, args[i], size - strlen(buffer)); strncat(buffer, " ", size - strlen(buffer)); i++; } return buffer; }
null
null
63c648705f004bf4898cfc7f
2023-02-23T19:34:36.333Z
Question from Code
v1
How to fix segmentation fault?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" /* Return a pointer to the rule with the given target. If the target doesn't exist, return null. */ Rule *find_rule(Rule **rule_head, char target[MAXLINE]) { Rule *curr_rule = *rule_head; while (curr_rule != NULL) { if (strcmp(curr_rule->target, target) == 0) { return curr_rule; } curr_rule = curr_rule->next_rule; } return NULL; } /* Create a new rule with the given target. Insert it at the tail of the list * of rules whose head is pointed to by rule_head. Return a pointer to the created rule. */ Rule *create_rule(Rule **rule_head, char target[MAXLINE]) { // Iterate to end of rules Rule *prev = NULL; Rule *curr = *rule_head; while (curr != NULL) { prev = curr; curr = curr->next_rule; } // Create the rule Rule *new_rule = malloc(sizeof(Rule)); new_rule->target = malloc((strlen(target) + 1) * sizeof(char)); strcpy(new_rule->target, target); new_rule->dependencies = NULL; new_rule->actions = NULL; new_rule->next_rule = NULL; // Link the rule if (rule_head == NULL) { rule_head[0] = new_rule; } else { prev->next_rule = new_rule; } return new_rule; } /* Search for the rule with the given target, where the list of rules * has their head pointed at rule_head and add the dependency with corr_rule. * Precondition: * - the rule with the given target exists */ void add_dependency(Rule **rule_head, char target[MAXLINE], Rule *corr_rule) { // Find rule Rule *curr_rule = *rule_head; while (strcmp(curr_rule->target, target) != 0) { curr_rule = curr_rule->next_rule; } // Create the dependency Dependency *new_dep = malloc(sizeof(Dependency)); new_dep->rule = corr_rule; new_dep->next_dep = NULL; // Link the dependency Dependency *prev_dep = NULL; Dependency *curr_dep = curr_rule->dependencies; while (curr_dep != NULL) { prev_dep = curr_dep; curr_dep = curr_dep->next_dep; } if (curr_rule->dependencies == NULL) { curr_rule->dependencies = new_dep; } else { prev_dep->next_dep = new_dep; } } /* Search for the rule with the given target, where the list of rules * has their head pointed at rule_head and link an action with args * to the rule. * Precondition: * - the rule with the given target exists */ void add_action(Rule **rule_head, char target[MAXLINE], char **args) { // Find Rule Rule *curr_rule = *rule_head; while (strcmp(curr_rule->target, target) != 0) { curr_rule = curr_rule->next_rule; } // Create the action Action *new_action = malloc(sizeof(Action)); new_action->args = args; new_action->next_act = NULL; // Link the action Action *prev_action = NULL; Action *curr_action = curr_rule->actions; while (curr_action != NULL) { prev_action = curr_action; curr_action = curr_action->next_act; } if (curr_rule->actions == NULL) { curr_rule->actions = new_action; } else { prev_action->next_act = new_action; } } /* Parse through the given (target) line and create and link the necessary dependencies and targets. * Return a pointer to the rule object with the given target in line */ Rule *parse_target_line(Rule **rule_head, char line[MAXLINE]) { int i = 0; int j; char target[MAXLINE]; char dep[MAXLINE]; while (line[i] != ' ') { target[i] = line[i]; // TODO: line seems to be characters generated from reading binary file i++; } target[i] = '\0'; Rule *rule; if (line[i+2] == '\n') { // no dependencies if (find_rule(rule_head, target) == NULL) { // target doesn't exist rule = create_rule(rule_head, target); } else { rule = find_rule(rule_head, target); } return rule; } // Move index to first dependency i += 3; while (line[i] != '\n') { // Iterate over line // Initialize index for dependency j = 0; // Iterate over dependency while (line[i] != ' ' && line[i] != '\n') { dep[j] = line[i]; j++; i++; } dep[j] = '\0'; if (find_rule(rule_head, target) == NULL) { // target doesn't exist create_rule(rule_head, target); } Rule *corr_rule = find_rule(rule_head, dep); if (corr_rule == NULL) { // corr_rule doesn't exist corr_rule = create_rule(rule_head, dep); } add_dependency(rule_head, target, corr_rule); // Move index to next dependency if (line[i] != '\n') { i++; } else { break; } } return find_rule(rule_head, target); } /* Parse through the given (action) line and create and link the necessary actions and rules. */ void parse_action_line(Rule *rule, char line[MAXLINE]) { char **args = malloc(MAXLINE * sizeof(char *)); char word[MAXLINE]; int num_words = 0; int i = 1; // first index is '\t' int k; while (i != '\n') { // iterate over line k = 0; // Iterate over words in action while (line[i] != ' ' && line[i] != '\n') { word[k] = line[i]; k++; i++; } word[k] = '\0'; args[num_words] = malloc(sizeof(char) * (strlen(word) + 1)); strcpy(args[num_words], word); num_words++; if (line[i] == ' ') { i++; // Move index to the next word } } args[num_words] = malloc(sizeof(NULL)); args[num_words] = NULL; // Create action Action *new_action = malloc(sizeof(Action)); new_action->args = args; new_action->next_act = NULL; // Link action to rule Action *prev_action = NULL; Action *curr_action = rule->actions; while (curr_action != NULL) { prev_action = curr_action; curr_action = curr_action->next_act; } if (rule->actions == NULL) { rule->actions = new_action; } else { prev_action->next_act = new_action; } } /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. Rule *rule_head = NULL; char line[MAXLINE]; line[0] = '\0'; Rule *rule; while (fgets(line, MAXLINE, fp) != NULL) { if (is_comment_or_empty(line)) { continue; } else if (line[0] != ' ' && line[0] != '\t') { // target line printf("%s\n", line); rule = parse_target_line(&rule_head, line); } else { // action line parse_action_line(rule, line); } } // char target[MAXLINE]; // // Rule *curr_rule = new_rule; // while (fgets(line, max_line, fp) != NULL) { // if (line[0] != ' ' && line[0] != '\t') { // int i = 0; // while (line[i] != ' ') { // target[i] = line[i]; // i++; // } // target[i] = '\0'; // // // initialize target attribute for curr_rule // curr_rule->target = malloc(strlen(target) + 1); // strcpy(curr_rule->target, target); // // // initialize actions attribute to NULL // curr_rule->actions = NULL; // // // initialize next_rule attribute to NULL // curr_rule->next_rule = NULL; // // // initialize dependencies attribute to NULL // curr_rule->dependencies = NULL; // // if (line[i] == '\n' || line[i+1] == '\n' || line[i+2] == '\n' || line[i+3] == '\n') { // // there are no dependencies so don't iterate through loop // } // // i += 3; // move index to first dependency // curr_rule->dependencies = malloc(sizeof(Dependency)); // // Dependency *curr_dependency = new_rule->dependencies; // // // iterate through dependencies // while (line[i] != '\n') { // // initialize new dependency // curr_dependency->rule = malloc(sizeof(Rule)); // curr_dependency->next_dep = NULL; // // // initialize new rule associated with new dependency // curr_rule->dependencies = curr_dependency->rule; // curr_rule->next_rule = curr_dependency->rule; // curr_rule = curr_rule->next_rule; // // int j = 0; // char dependency[MAXLINE]; // // // initialize the dependency's corresponding Rule // while (line[i] != '\n' && line[i] != ' ') { // dependency[j] = line[i]; // j++; // i++; // } // } // } // for (int i = 0 ; i < max_line ; i++) { // if (line[i] == ' ') { // line[i] = '\0'; // } else if (line[i] == '\n') { // line[i] = '\0'; // break; // } // } // } return rule_head; } /****************************************************************************** * These helper functions are provided for you. Do not modify them. *****************************************************************************/ /* Print the list of actions */ void print_actions(Action *act) { while(act != NULL) { if(act->args == NULL) { fprintf(stderr, "ERROR: action with NULL args\n"); act = act->next_act; continue; } printf("\t"); int i = 0; while(act->args[i] != NULL) { printf("%s ", act->args[i]) ; i++; } printf("\n"); act = act->next_act; } } /* Print the list of rules to stdout in makefile format. If the output of print_rules is saved to a file, it should be possible to use it to run make correctly. */ void print_rules(Rule *rules){ Rule *cur = rules; while (cur != NULL) { if (cur->dependencies || cur->actions) { // Print target printf("%s : ", cur->target); // Print dependencies Dependency *dep = cur->dependencies; while (dep != NULL){ if(dep->rule->target == NULL) { fprintf(stderr, "ERROR: dependency with NULL rule\n"); } printf("%s ", dep->rule->target); dep = dep->next_dep; } printf("\n"); // Print actions print_actions(cur->actions); } cur = cur->next_rule; } } /* Return 1 if the line is a comment line, as defined on the assignment handout. Return 0 otherwise. */ int is_comment_or_empty(const char *line) { for (int i = 0; i < strlen(line); i++){ if (line[i] == '#') { return 1; } if (line[i] != '\t' && line[i] != ' ') { return 0; } } return 1; } /* Convert an array of args to a single space-separated string in buffer. Returns buffer. Note that memory for args and buffer should be allocted by the caller. */ char *args_to_string(char **args, char *buffer, int size) { buffer[0] = '\0'; int i = 0; while (args[i] != NULL) { strncat(buffer, args[i], size - strlen(buffer)); strncat(buffer, " ", size - strlen(buffer)); i++; } return buffer; }
null
null
63c648705f004bf4898cfc7f
2023-02-23T19:35:06.267Z
Question from Code
v1
what is wrong with my code
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" /* Return a pointer to the rule with the given target. If the target doesn't exist, return null. */ Rule *find_rule(Rule **rule_head, char target[MAXLINE]) { Rule *curr_rule = *rule_head; while (curr_rule != NULL) { if (strcmp(curr_rule->target, target) == 0) { return curr_rule; } curr_rule = curr_rule->next_rule; } return NULL; } /* Create a new rule with the given target. Insert it at the tail of the list * of rules whose head is pointed to by rule_head. Return a pointer to the created rule. */ Rule *create_rule(Rule **rule_head, char target[MAXLINE]) { // Iterate to end of rules Rule *prev = NULL; Rule *curr = *rule_head; while (curr != NULL) { prev = curr; curr = curr->next_rule; } // Create the rule Rule *new_rule = malloc(sizeof(Rule)); new_rule->target = malloc((strlen(target) + 1) * sizeof(char)); strcpy(new_rule->target, target); new_rule->dependencies = NULL; new_rule->actions = NULL; new_rule->next_rule = NULL; // Link the rule if (rule_head == NULL) { rule_head[0] = new_rule; } else { prev->next_rule = new_rule; } return new_rule; } /* Search for the rule with the given target, where the list of rules * has their head pointed at rule_head and add the dependency with corr_rule. * Precondition: * - the rule with the given target exists */ void add_dependency(Rule **rule_head, char target[MAXLINE], Rule *corr_rule) { // Find rule Rule *curr_rule = *rule_head; while (strcmp(curr_rule->target, target) != 0) { curr_rule = curr_rule->next_rule; } // Create the dependency Dependency *new_dep = malloc(sizeof(Dependency)); new_dep->rule = corr_rule; new_dep->next_dep = NULL; // Link the dependency Dependency *prev_dep = NULL; Dependency *curr_dep = curr_rule->dependencies; while (curr_dep != NULL) { prev_dep = curr_dep; curr_dep = curr_dep->next_dep; } if (curr_rule->dependencies == NULL) { curr_rule->dependencies = new_dep; } else { prev_dep->next_dep = new_dep; } } /* Search for the rule with the given target, where the list of rules * has their head pointed at rule_head and link an action with args * to the rule. * Precondition: * - the rule with the given target exists */ void add_action(Rule **rule_head, char target[MAXLINE], char **args) { // Find Rule Rule *curr_rule = *rule_head; while (strcmp(curr_rule->target, target) != 0) { curr_rule = curr_rule->next_rule; } // Create the action Action *new_action = malloc(sizeof(Action)); new_action->args = args; new_action->next_act = NULL; // Link the action Action *prev_action = NULL; Action *curr_action = curr_rule->actions; while (curr_action != NULL) { prev_action = curr_action; curr_action = curr_action->next_act; } if (curr_rule->actions == NULL) { curr_rule->actions = new_action; } else { prev_action->next_act = new_action; } } /* Parse through the given (target) line and create and link the necessary dependencies and targets. * Return a pointer to the rule object with the given target in line */ Rule *parse_target_line(Rule **rule_head, char line[MAXLINE]) { int i = 0; int j; char target[MAXLINE]; char dep[MAXLINE]; while (line[i] != ' ') { target[i] = line[i]; // TODO: line seems to be characters generated from reading binary file i++; } target[i] = '\0'; Rule *rule; if (line[i+2] == '\n') { // no dependencies if (find_rule(rule_head, target) == NULL) { // target doesn't exist rule = create_rule(rule_head, target); } else { rule = find_rule(rule_head, target); } return rule; } // Move index to first dependency i += 3; while (line[i] != '\n') { // Iterate over line // Initialize index for dependency j = 0; // Iterate over dependency while (line[i] != ' ' && line[i] != '\n') { dep[j] = line[i]; j++; i++; } dep[j] = '\0'; if (find_rule(rule_head, target) == NULL) { // target doesn't exist create_rule(rule_head, target); } Rule *corr_rule = find_rule(rule_head, dep); if (corr_rule == NULL) { // corr_rule doesn't exist corr_rule = create_rule(rule_head, dep); } add_dependency(rule_head, target, corr_rule); // Move index to next dependency if (line[i] != '\n') { i++; } else { break; } } return find_rule(rule_head, target); } /* Parse through the given (action) line and create and link the necessary actions and rules. */ void parse_action_line(Rule *rule, char line[MAXLINE]) { char **args = malloc(MAXLINE * sizeof(char *)); char word[MAXLINE]; int num_words = 0; int i = 1; // first index is '\t' int k; while (i != '\n') { // iterate over line k = 0; // Iterate over words in action while (line[i] != ' ' && line[i] != '\n') { word[k] = line[i]; k++; i++; } word[k] = '\0'; args[num_words] = malloc(sizeof(char) * (strlen(word) + 1)); strcpy(args[num_words], word); num_words++; if (line[i] == ' ') { i++; // Move index to the next word } } args[num_words] = malloc(sizeof(NULL)); args[num_words] = NULL; // Create action Action *new_action = malloc(sizeof(Action)); new_action->args = args; new_action->next_act = NULL; // Link action to rule Action *prev_action = NULL; Action *curr_action = rule->actions; while (curr_action != NULL) { prev_action = curr_action; curr_action = curr_action->next_act; } if (rule->actions == NULL) { rule->actions = new_action; } else { prev_action->next_act = new_action; } } /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. Rule *rule_head = NULL; char line[MAXLINE]; line[0] = '\0'; Rule *rule; while (fgets(line, MAXLINE, fp) != NULL) { if (is_comment_or_empty(line)) { continue; } else if (line[0] != ' ' && line[0] != '\t') { // target line printf("%s\n", line); rule = parse_target_line(&rule_head, line); } else { // action line parse_action_line(rule, line); } } // char target[MAXLINE]; // // Rule *curr_rule = new_rule; // while (fgets(line, max_line, fp) != NULL) { // if (line[0] != ' ' && line[0] != '\t') { // int i = 0; // while (line[i] != ' ') { // target[i] = line[i]; // i++; // } // target[i] = '\0'; // // // initialize target attribute for curr_rule // curr_rule->target = malloc(strlen(target) + 1); // strcpy(curr_rule->target, target); // // // initialize actions attribute to NULL // curr_rule->actions = NULL; // // // initialize next_rule attribute to NULL // curr_rule->next_rule = NULL; // // // initialize dependencies attribute to NULL // curr_rule->dependencies = NULL; // // if (line[i] == '\n' || line[i+1] == '\n' || line[i+2] == '\n' || line[i+3] == '\n') { // // there are no dependencies so don't iterate through loop // } // // i += 3; // move index to first dependency // curr_rule->dependencies = malloc(sizeof(Dependency)); // // Dependency *curr_dependency = new_rule->dependencies; // // // iterate through dependencies // while (line[i] != '\n') { // // initialize new dependency // curr_dependency->rule = malloc(sizeof(Rule)); // curr_dependency->next_dep = NULL; // // // initialize new rule associated with new dependency // curr_rule->dependencies = curr_dependency->rule; // curr_rule->next_rule = curr_dependency->rule; // curr_rule = curr_rule->next_rule; // // int j = 0; // char dependency[MAXLINE]; // // // initialize the dependency's corresponding Rule // while (line[i] != '\n' && line[i] != ' ') { // dependency[j] = line[i]; // j++; // i++; // } // } // } // for (int i = 0 ; i < max_line ; i++) { // if (line[i] == ' ') { // line[i] = '\0'; // } else if (line[i] == '\n') { // line[i] = '\0'; // break; // } // } // } return rule_head; } /****************************************************************************** * These helper functions are provided for you. Do not modify them. *****************************************************************************/ /* Print the list of actions */ void print_actions(Action *act) { while(act != NULL) { if(act->args == NULL) { fprintf(stderr, "ERROR: action with NULL args\n"); act = act->next_act; continue; } printf("\t"); int i = 0; while(act->args[i] != NULL) { printf("%s ", act->args[i]) ; i++; } printf("\n"); act = act->next_act; } } /* Print the list of rules to stdout in makefile format. If the output of print_rules is saved to a file, it should be possible to use it to run make correctly. */ void print_rules(Rule *rules){ Rule *cur = rules; while (cur != NULL) { if (cur->dependencies || cur->actions) { // Print target printf("%s : ", cur->target); // Print dependencies Dependency *dep = cur->dependencies; while (dep != NULL){ if(dep->rule->target == NULL) { fprintf(stderr, "ERROR: dependency with NULL rule\n"); } printf("%s ", dep->rule->target); dep = dep->next_dep; } printf("\n"); // Print actions print_actions(cur->actions); } cur = cur->next_rule; } } /* Return 1 if the line is a comment line, as defined on the assignment handout. Return 0 otherwise. */ int is_comment_or_empty(const char *line) { for (int i = 0; i < strlen(line); i++){ if (line[i] == '#') { return 1; } if (line[i] != '\t' && line[i] != ' ') { return 0; } } return 1; } /* Convert an array of args to a single space-separated string in buffer. Returns buffer. Note that memory for args and buffer should be allocted by the caller. */ char *args_to_string(char **args, char *buffer, int size) { buffer[0] = '\0'; int i = 0; while (args[i] != NULL) { strncat(buffer, args[i], size - strlen(buffer)); strncat(buffer, " ", size - strlen(buffer)); i++; } return buffer; }
null
null
63c648705f004bf4898cfc7f
2023-02-23T19:56:01.968Z
Question from Code
v1
what is wrong
#define MAXLINE 256 typedef struct action_node { char **args; // An array of strings suitable to be passed to execvp struct action_node *next_act; } Action; typedef struct dep_node { struct rule_node *rule; struct dep_node *next_dep; } Dependency; typedef struct rule_node { char *target; Dependency *dependencies; Action *actions; struct rule_node *next_rule; } Rule; /* Return a pointer to the rule with the given target. If the target doesn't exist, return null. */ Rule *find_rule(Rule **rule_head, char target[MAXLINE]) { Rule *curr_rule = *rule_head; while (curr_rule != NULL) { if (strcmp(curr_rule->target, target) == 0) { return curr_rule; } curr_rule = curr_rule->next_rule; } return NULL; }
null
null
63c648705f004bf4898cfc7f
2023-02-23T19:56:52.518Z
Question from Code
v1
how to make find_rule thread safe
#define MAXLINE 256 typedef struct action_node { char **args; // An array of strings suitable to be passed to execvp struct action_node *next_act; } Action; typedef struct dep_node { struct rule_node *rule; struct dep_node *next_dep; } Dependency; typedef struct rule_node { char *target; Dependency *dependencies; Action *actions; struct rule_node *next_rule; } Rule; /* Return a pointer to the rule with the given target. If the target doesn't exist, return null. */ Rule *find_rule(Rule **rule_head, char target[MAXLINE]) { Rule *curr_rule = *rule_head; while (curr_rule != NULL) { if (strcmp(curr_rule->target, target) == 0) { return curr_rule; } curr_rule = curr_rule->next_rule; } return NULL; }
null
null
63c648715f004bf4898cfc84
2023-01-28T20:38:53.239Z
Question from Code
v1
what is wrong with my code
#include <stdio.h> #include <stdlib.h> int populate_array(int, int *); int check_sin(int *); int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc != 2) { exit(2); } // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. int sin_num = strtol(argv[1], NULL, 10); int *sin_array = malloc(sizeof(int) * 9); int array_success = populate_array(sin_num, sin_array); if (array_success == 1) { printf("Invalid SIN"); return 1; } else { int valid_sin = check_sin(sin_array); if (valid_sin == 1){ printf("Invalid SIN"); return 1; } else{ printf("Valid SIN"); } } return 0; }
null
null
63c648715f004bf4898cfc84
2023-01-28T20:41:36.150Z
Question from Code
v1
why does populate array generate use of undeclared identifier 'sin_helpers' error
#include <stdio.h> #include <stdlib.h> int populate_array(int, int *); int check_sin(int *); int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc != 2) { exit(2); } // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. int sin_num = strtol(argv[1], NULL, 10); int *sin_array = malloc(sizeof(int) * 9); int array_success = populate_array(sin_num, sin_array); if (array_success == 1) { printf("Invalid SIN"); return 1; } else { int valid_sin = check_sin(sin_array); if (valid_sin == 1){ printf("Invalid SIN"); return 1; } else{ printf("Valid SIN"); } } return 0; } #include <stdio.h> #include <stdlib.h> int populate_array(int, int *); int check_sin(int *); int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc != 2) { exit(2); } // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. int sin_num = strtol(argv[1], NULL, 10); int *sin_array = malloc(sizeof(int) * 9); int array_success = populate_array(sin_num, sin_array); if (array_success == 1) { printf("Invalid SIN"); return 1; } else { int valid_sin = check_sin(sin_array); if (valid_sin == 1){ printf("Invalid SIN"); return 1; } else{ printf("Valid SIN"); } } return 0; }
null
null
63c648715f004bf4898cfc84
2023-01-28T20:50:15.021Z
Question from Code
v1
why is sin_num an undeclared identifier
#include <stdio.h> #include <stdlib.h> int populate_array(int, int *); int check_sin(int *); int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc != 2) { exit(2); } // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. int sin_num = strtol(argv[1], NULL, 10); int *sin_array = malloc(sizeof(int) * 9); int array_success = populate_array(sin_num, sin_array); if (array_success == 1) { printf("Invalid SIN"); return 1; } else { int valid_sin = check_sin(sin_array); if (valid_sin == 1){ printf("Invalid SIN"); return 1; } else{ printf("Valid SIN"); } } return 0; } #include <stdio.h> #include <stdlib.h> int populate_array(int, int *); int check_sin(int *); int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc != 2) { exit(2); } // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. int sin_num = strtol(argv[1], NULL, 10); int *sin_array = malloc(sizeof(int) * 9); int array_success = populate_array(sin_num, sin_array); if (array_success == 1) { printf("Invalid SIN"); return 1; } else { int valid_sin = check_sin(sin_array); if (valid_sin == 1){ printf("Invalid SIN"); return 1; } else{ printf("Valid SIN"); } } return 0; }
null
null
63c648725f004bf4898cfc8e
2023-01-19T03:50:22.101Z
General Question
v1
How do I stop a program when end of standard of input is reached?
null
null
null
63c648725f004bf4898cfc8e
2023-02-14T02:23:43.795Z
General Question
v1
How should I free a linked list from the heap?
null
null
null
63c648725f004bf4898cfc8e
2023-03-10T01:52:22.065Z
General Question
v2
i want to run a program called "validate" which taken in input from stdin, how should i do this?
null
null
null
63c648725f004bf4898cfc8e
2023-03-10T04:11:01.947Z
General Question
v2
how can i read from a text file one line at a time
null
null
null
63c648725f004bf4898cfc8e
2023-03-11T17:23:45.125Z
General Question
v2
What does implicit declaration with type int() mean?
null
null
null
63c648725f004bf4898cfc8e
2023-03-11T17:49:41.036Z
Help Fix Code
v2
has implicit declarator for test for some reason
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. // char *line = malloc(sizeof(char) * MAXLINE); char hi; // Create first rule Rule *first_rule = malloc(sizeof(Rule)); hi = test(); return first_rule; } char test() { // char *hi = malloc(2); return 'h'; }
null
null
63c648725f004bf4898cfc8e
2023-03-13T20:54:25.561Z
General Question
v2
Is there an easy way to strip whitespace from a string
null
null
null
63c648725f004bf4898cfc8e
2023-03-14T01:01:18.933Z
General Question
v2
how can i find the number of words in a sentence
null
null
null
63c648725f004bf4898cfc8e
2023-03-14T02:01:26.264Z
General Question
v2
How can I use strtok to separate based on whitespaces in general
null
null
null
63c648725f004bf4898cfc8e
2023-03-14T02:23:06.834Z
General Question
v2
Why do i get different errors based on whether or not i print '\n'
null
null
null
63c648725f004bf4898cfc8e
2023-03-14T02:34:56.620Z
Help Fix Code
v2
I want args to be array of strings stored on the heap, however args[0] is being written over some reason in the while loop
void set_action(Rule *rule_block, char *action_line){ int num_tokens = find_num_tokens(action_line); char **args = malloc(sizeof(char) * (num_tokens + 1)); // an extra for NULL at the end args[num_tokens] = NULL; int i = 0; printf("action line is: '%s'\n", action_line); char *token = strtok(action_line, " "); while (token != NULL){ args[i] = malloc(sizeof(char) * (strlen(token) + 1)); char *heap_token = args[i]; strncpy(heap_token, token, strlen(token)); heap_token[strlen(token)] = '\0'; printf("Token is %s\n", heap_token); i++; token = strtok(NULL, " "); printf("%s\n", args[0]); } Action *new_action = malloc(sizeof(Action)); new_action->args = args; new_action->next_act = NULL; if (rule_block->actions == NULL){ rule_block->actions = new_action; return; } Action *curr_action = rule_block->actions; while (curr_action->next_act != NULL){ curr_action = curr_action->next_act; } curr_action->next_act = new_action; return; }
null
null
63c648725f004bf4898cfc8e
2023-03-14T02:38:09.690Z
Help Fix Code
v2
args[0] keeps being written over as the loop progresses
char **args = malloc(sizeof(char) * (num_tokens + 1)); int i = 0; while (token != NULL){ args[i] = malloc(sizeof(char) * (strlen(token) + 1)); char *heap_token = args[i]; strncpy(heap_token, token, strlen(token)); heap_token[strlen(token)] = '\0'; printf("Token is %s\n", heap_token); i++; token = strtok(NULL, " "); printf("%s\n", args[0]); }
null
null
63c648725f004bf4898cfc8e
2023-03-14T03:44:02.337Z
General Question
v2
how to tell if a string is made entirely of whitespaces
null
null
null
63c648725f004bf4898cfc8e
2023-03-14T14:16:07.196Z
General Question
v2
how can i use the stat function to find the last modified time of a file
null
null
null
63c648725f004bf4898cfc8e
2023-03-14T14:50:19.685Z
Help Fix Code
v2
I get a segmentation fault when running this bit of code.
int main(){ Rule *rule; rule->target = "test"; }
null
null
63c648725f004bf4898cfc8e
2023-03-14T15:12:19.326Z
General Question
v2
How can I check whether or not a file exists in a given directory
null
null
null
63c648725f004bf4898cfc8e
2023-03-17T19:36:34.522Z
General Question
v2
how to use the random function to return values in a specific range, say 0 to 99
null
null
null
63c648725f004bf4898cfc8e
2023-03-17T21:13:34.236Z
General Question
v2
Could you give an example of using setitimer
null
null
null
63c648725f004bf4898cfc8e
2023-03-26T16:03:54.835Z
General Question
v2
what does fflush do
null
null
null
63c648725f004bf4898cfc8e
2023-03-31T13:50:44.115Z
General Question
v2
how can i ensure that a new initialised character array is filled with null characters
null
null
null
63c648725f004bf4898cfc8e
2023-04-02T01:44:35.930Z
General Question
v2
could you explain what asprintf and snprintf do?
null
null
null
63c648725f004bf4898cfc93
2023-02-02T19:45:11.137Z
Question from Code
v1
are there any buffer overflows in the code
int main(int argc, char **argv) {       if (argc != 3) {           fprintf(stderr, "Usage: greeting message name\n");           return 1;       }       char greeting[20];       char *name = argv[2];          // Your code goes here       strncpy(greeting, argv[1], 20);       greeting[19] = '\0';       int len = strlen(greeting);          if (len < 19) {         greeting[len++] = ' ';         greeting[len]   = '\0';       }          if (len < 19) {         strncat(greeting, name, 19 - len);       }          printf("%s\n", greeting);       return 0;   }  
null
null
63c648725f004bf4898cfc93
2023-02-02T20:03:26.953Z
Question from Code
v1
can you rewrite this without using any string functions
int main(int argc, char **argv) {       if (argc != 3) {           fprintf(stderr, "Usage: greeting message name\n");           return 1;       }       char greeting[20];       char *name = argv[2];          // Your code goes here       strncpy(greeting, argv[1], 20);       greeting[19] = '\0';       int len = strlen(greeting);          if (len < 19) {         greeting[len++] = ' ';         greeting[len]   = '\0';       }          if (len < 19) {         strncat(greeting, name, 19 - len);       }          printf("%s\n", greeting);       return 0;   }  
null
null
63c648725f004bf4898cfc93
2023-02-09T16:55:26.439Z
General Question
v1
what is the smallest possible value of sizeof(int)
null
null
null
63c648735f004bf4898cfc98
2023-01-18T13:19:20.035Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> /* * This function interprets score_card as an array of pointers with size elements. * Return the sum of the values pointed to by the elements of score_card. */ int sum_card(int **score_card, int size) { // TODO: write the body of sum_card according to its description. int sum = 0; for (int i = 0; i < sizeof(score_card); i++) { sum += *(score_card[i]); } return sum; } /* * NOTE: don't change the main function! * The command line arguments are a sequence of integers that will be * used to initialize the array score_card. * * Sample usage: * $ gcc -Wall -std=gnu99 -g -o score_card score_card.c * $ ./score_card 10 -3 4 * Sum: 11 */ int main(int argc, char **argv) { int size = argc - 1; int *score_card[size]; for (int i = 0; i < size; i++) { // NOTE: We haven't covered malloc yet, so don't worry about this line. score_card[i] = malloc(sizeof(int)); *(score_card[i]) = strtol(argv[i + 1], NULL, 10); } printf("Sum: %d\n", sum_card(score_card, size)); return 0; }
Get the sum of the values pointed at in score_card.
null
63c648755f004bf4898cfcac
2023-03-14T20:44:37.713Z
Help Fix Code
v2
check if line only contains tab or spaces
int i = 0 while (i; i < strlen(line); i++) { if (line[i] == ' ' || line[i] == '\t') { break; } }
null
null
63c648755f004bf4898cfcac
2023-03-14T20:47:53.207Z
Help Fix Code
v2
if the line contains all spaces or tabs, then continue
int i = 0; while (i; i < strlen(line); i++) { if (line[i] != ' ' || line[i] != '\t') { break; } } if (i == strlen(line) - 2) { continue; }
null
null
63c648755f004bf4898cfcac
2023-03-14T20:53:29.035Z
Help Fix Code
v2
check if every character in the string is either space or tab.
int i = 0; while (i < strlen(line)) { if (line[i] != ' ' || line[i] != '\t') { break; } i++; } if (i == strlen(line) - 2) { continue; }
null
null
63c648755f004bf4898cfcac
2023-03-14T20:55:26.690Z
Help Fix Code
v2
check the line is not blank or comment line
int i = 0; while (i < strlen(line)) { if (line[i] != ' ' && line[i] != '\t') { break; } i++; } if (i == strlen(line) - 2) { continue; } if (line[0] == '\n' || line[0] == '#') { continue; }
null
null
63c648765f004bf4898cfcb1
2023-01-18T00:32:44.542Z
Question from Code
v1
what does this code do?
int **i;
null
null
63c648765f004bf4898cfcb1
2023-01-18T00:33:56.350Z
Question from Code
v1
how to overflow test so i is changed to 10
int test[4]; int i = 0;
null
null
63c648775f004bf4898cfcbb
2023-01-18T21:26:26.862Z
Explain Code
v1
null
#include <stdio.h> /* set *largest_pt to the largest element pointed to by the array */ void find_largest(int **A, int A_size, int *largest_pt) { *largest_pt = **A; for (int i = 1; i < A_size; i++) { if (*A[i] > *largest_pt) { *largest_pt = *A[i]; } } } int main() { int i = 81; int j = -4; // now A holds 2 pointers int *A[2] = {&j, &i}; int largest; find_largest(A, 2, &largest); printf("largest is %d\n", largest); return 0; }
null
null
63c648775f004bf4898cfcbb
2023-04-20T19:02:11.174Z
General Question
v2
what is the difference between double quote and single quote in shell programming
null
null
null
63c648775f004bf4898cfcbb
2023-04-21T06:23:23.591Z
Explain Code
v2
null
#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <unistd.h> int main() { int result; int i, j; printf("[%d] Original process (my parent is %d)\n", getpid(), getppid()); for (i = 0; i < 5; i++) { result = fork(); if (result == -1) { perror("fork:"); exit(1); } else if (result == 0) { for (j = 0; j < 5; j++) { printf("[%d] Child %d %d\n", getpid(), i, j); sleep(100); } if (i == 2) { abort(); } exit(i); } } sleep(10); for (i = 0; i < 5; i++) { pid_t pid; int status; if ((pid = wait(&status)) == -1) { perror("wait"); } else { if (WIFEXITED(status)) { printf("Child %d terminated with %d\n", pid, WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("Child %d terminated with signal %d\n", pid, WTERMSIG(status)); } else { printf("Shouldn't get here\n"); } } } printf("[%d] Parent about to terminate\n", getpid()); return 0; }
null
null
63c648775f004bf4898cfcc0
2023-01-18T02:43:59.611Z
Question from Code
v1
I want this code to repeatedly get input from the user and iterate until they close the standard input, but it only works for one iteration. How can I make it iterate as described above?
int main(){ char phone[11]; printf("Enter in a 10 character string: "); scanf("%s", phone); int input; printf(""); int value = scanf("%d", &input); while (-1 != value) { if (input == -1) { printf("%s \n", phone); return 0; } else if ((0 <= input) && (9 >= input)) { printf("%c \n", phone[input]); return 0; } else { printf("ERROR \n"); return 1; } printf(""); value = scanf("%d", &input); } }
null
null
63c648775f004bf4898cfcc0
2023-01-25T01:59:25.752Z
Question from Code
v1
What type of object is created here?
char *perm_list[100];
null
null
63c648775f004bf4898cfcc0
2023-01-25T02:00:41.193Z
General Question
v1
Can I edit a string after it has been created?
null
null
null
63c648775f004bf4898cfcc0
2023-01-25T02:01:17.903Z
General Question
v1
How can I create an array of 100 empty strings?
null
null
null
63c648775f004bf4898cfcc0
2023-01-25T03:26:28.450Z
Question from Code
v1
What is the type of perm_list ?
char **perm_list[100];
null
null
63c648775f004bf4898cfcc0
2023-01-25T03:26:56.179Z
General Question
v1
Is a pointer to a character the same as a string?
null
null
null