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
⌀ |
---|---|---|---|---|---|---|---|
63c648b75f004bf4898cff04 | 2023-02-25T01:12:08.433Z | General Question | v1 | What does each term mean in this input:
gcc -Wall -g -std=gnu99 –o hello hello.c | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-25T01:15:17.727Z | General Question | v1 | Assume that the executable is in your parent directory; give the command to run this executable without any
command-line arguments. | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-25T01:15:41.956Z | General Question | v1 | Assume you have changed back into the same directory as the executable. Give the command to run the
executable where the resulting output is redirected to a file named test1.out | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-25T01:16:15.929Z | General Question | v1 | When you run the program, it interacts with the user expecting the user to type input. Imagine that up until
now you’ve been providing input from the keyboard. Give the command to run the program and redirect the
input so that the executable reads from the file somefile.txt. | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-25T01:18:02.462Z | General Question | v1 | Show the command to run the executable prog with the command-line arguments -k
3 myfile, reading input from standard input redirected from somefile.txt and redirecting the output to
test1.out. | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-25T01:18:51.239Z | General Question | v1 | Write a shell command to remove all the files in the current working directory that end in .o | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-25T01:19:12.635Z | General Question | v1 | One of the most powerful features of the shell is chaining multiple commands together using pipes. Use the
commands cut, grep, sort and uniq to list the unique paths to the bash shell in the file /etc/passwd.
•/etc/passwd is delimited by : characters and the 7th field contains the path to the shell
•If you haven’t used cut before, you will want to look at the man page. Run man cut to read how this
command works.
•Build up each component of the pipeline one command at a time and see if the output is what you would
expect. | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-25T23:22:24.658Z | General Question | v1 | 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 | null | null |
63c648b75f004bf4898cff04 | 2023-02-25T23:25:07.193Z | General Question | v1 | what is the second parameter for strtol and what does it do, give an example | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-26T00:55:51.918Z | General Question | v1 | make a list that contains three mutable strings | null | null | null |
63c648b75f004bf4898cff04 | 2023-02-26T05:59:39.634Z | General Question | v1 | is strtol a system call? | null | null | null |
63c648b75f004bf4898cff04 | 2023-03-02T23:28:12.969Z | Question from Code | v1 | Wanted to write code such that one parent has iterations number of child processes, but not working as expected | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n == 0) {
exit(0);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| null | null |
63c648b75f004bf4898cff04 | 2023-03-02T23:28:54.479Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n == 0) {
exit(0);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| Wanted to write code such that one parent has iterations number of child processes, but not working as expected | null |
63c648b75f004bf4898cff04 | 2023-03-02T23:36:27.982Z | General Question | v1 | what argument to put for wait() | null | null | null |
63c648b75f004bf4898cff04 | 2023-03-10T21:28:27.260Z | General Question | v2 | strtok example | null | null | null |
63c648b95f004bf4898cff1d | 2023-03-10T21:43:03.071Z | Question from Code | v2 | What is wrong with my insert linked list function
| void insert_rule(Rule* head, Rule* rule) {
if (head == NULL) {
head = rule;
return;
}
while (head->next_rule != NULL) {
head = head->next_rule;
}
head->next_rule = rule;
return;
}
| null | null |
63c648b95f004bf4898cff1d | 2023-03-10T21:57:07.449Z | Question from Code | v2 | What is wrong with my find rule function
| Rule* find_rule(Rule** head, char* target) {
// printf("last rule %s \n", target);
Rule* curr = *head;
while (curr != NULL) {
if (strcmp(curr->target, target) == 0) {
return curr;
}
curr = curr->next_rule;
}
return NULL;
} | null | null |
63c648b95f004bf4898cff1d | 2023-03-10T21:58:16.517Z | Question from Code | v2 | what is wrong with my insert function | void insert_rule(Rule** head, Rule* rule) {
Rule* curr = *head;
if (curr == NULL) {
curr = rule;
return;
}
while (curr->next_rule != NULL) {
curr = curr->next_rule;
}
curr->next_rule = rule;
return;
}
| null | null |
63c648ba5f004bf4898cff22 | 2023-02-03T18:29:10.989Z | Explain Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *next;
} Node;
Node *create_node(int num, Node *next) {
Node *new_node = malloc(sizeof(Node));
new_node->value = num;
new_node->next = next;
return new_node;
}
int main() {
Node *front = NULL;
front = create_node(3, front);
front = create_node(2, front);
front = create_node(1, front);
}
| null | null |
63c648ba5f004bf4898cff22 | 2023-02-03T18:31:35.047Z | Question from Code | v1 | Why does the code in the main function properly create a linked list | #include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *next;
} Node;
Node *create_node(int num, Node *next) {
Node *new_node = malloc(sizeof(Node));
new_node->value = num;
new_node->next = next;
return new_node;
}
int main() {
Node *front = NULL;
front = create_node(3, front);
front = create_node(2, front);
front = create_node(1, front);
}
| null | null |
63c648ba5f004bf4898cff22 | 2023-02-15T20:32:16.836Z | Question from Code | v1 | where is there a memory leak
| #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) {
//check if name is too long
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
//Seperate case if linked list is empty
if (*user_ptr_add == NULL){
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
for (int i = 0; i < MAX_FRIENDS; i ++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
//make user head of the list
*user_ptr_add = new_user;
new_user->next = NULL;
strcpy(new_user->profile_pic, "empty_pic");
return 0;
}
// Traverse to end of linked_list; Compare usernames
User *curr = *user_ptr_add;
while (curr->next != NULL) {
if (strcmp(curr->name, name) == 0) {
return 1;
}
curr = curr->next;
}
//special case where user already exists at end of list
if (strcmp(curr->name, name) == 0) {
return 1;
}
// Declare new User object
User *new_user = malloc(sizeof(User));
//Initialize members of new_user
strcpy(new_user->name, name);
for (int i = 0; i < MAX_FRIENDS; i ++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
strcpy(new_user->profile_pic, "empty_pic");
//Now curr points to the tail
new_user->next = curr->next;
curr->next = 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) {
if (head == NULL) {
return NULL;
}
User *curr = (User *) head;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
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) {
User *new_curr = (User *) curr;
while (new_curr != NULL) {
printf("%s\n", new_curr->name);
new_curr = new_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) {
FILE *test_exists;
test_exists = fopen(filename, "r");
if (strlen(filename) > MAX_NAME){
return 2;
} else if (test_exists == NULL){
return 1;
} else {
strcpy(user->profile_pic, filename);
if (fclose(test_exists) != 0){
return 1;
}
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) {
//First, check if users dont exist
User *curr = head;
User *user1;
User *user2;
int found1 = 0;
int found2 = 0;
while (curr != NULL) {
if (strcmp(name1, curr->name) == 0){
found1 = 1;
user1 = curr;
} else if (strcmp(name2, curr->name) == 0){
found2 = 1;
user2 = curr;
}
curr = curr->next;
}
if (found1 == 0 || found2 == 0) {
return 4;
}
//Second, check if same user is passed in twice
if (strcmp(name1, name2) == 0){
return 3;
}
//Third, check if either of them has MAX_FRIENDS
int not_full1 = 0;
int not_full2 = 0;
for (int i = 0; i < MAX_FRIENDS; i ++) {
if (user1->friends[i] == NULL) {
not_full1 = 1;
}
if (user2->friends[i] == NULL) {
not_full2 = 1;
}
}
if (not_full1 == 0 || not_full2 == 0) {
return 2;
}
//Fourth, check if already friends
for (int i = 0; i < MAX_FRIENDS; i ++) {
if (user1->friends[i] != NULL) {
if (strcmp((user1->friends[i])->name, user2->name) == 0) {
return 1;
}
}
}
//Make friends
int index1 = 0;
int index2 = 0;
while (index1 < MAX_FRIENDS) {
if (user1->friends[index1] == NULL) {
user1->friends[index1] = user2;
break;
} else {
index1 ++;
}
}
while (index2 < MAX_FRIENDS) {
if (user2->friends[index2] == NULL) {
user2->friends[index2] = user1;
break;
} else {
index2 ++;
}
}
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;
}
//print profile pic
FILE *pfp = fopen(user->profile_pic, "r");
if (pfp != NULL) {
char line[61];
while (fgets(line, 61, pfp) != NULL) {
printf("%s", line);
}
printf("\n");
}
//print name
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
//print friends
printf("Friends: \n");
int i = 0;
while (user->friends[i] != NULL && i < MAX_FRIENDS) {
printf("%s\n", (user->friends[i])->name);
i ++;
}
printf("------------------------------------------\n");
printf("Posts: \n");
//print posts
if (user->first_post != NULL) {
printf("From: %s\n", (user->first_post)->author);
printf("Date: %s\n", ctime((user->first_post)->date));
printf("%s\n", (user->first_post)->contents);
if ((user->first_post)->next != NULL){
Post *curr = (user->first_post)->next;
while (curr != NULL) {
printf("\n===\n\n");
printf("From: %s\n", curr->author);
printf("Date: %s\n", ctime(curr->date));
printf("%s\n", curr->contents);
curr = curr->next;
}
}
}
printf("------------------------------------------\n");
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 (author == NULL || target == NULL) {
free(contents);
return 2;
}
//check if users are friends
int i = 0;
int is_friend = 0;
while (author->friends[i] != NULL) {
if (strcmp((author->friends[i])->name, target->name) == 0) {
is_friend = 1;
break;
}
i ++;
}
if (is_friend == 0) {
free(contents);
return 1;
} else {
//create and insert post at beginning of post linked list
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
time_t *ct = malloc(sizeof(time_t));
time(ct);
new_post->date = ct;
if (target->first_post == NULL) {
new_post->next = NULL;
target->first_post = new_post;
} else {
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) {
//first, find user and free memory from all their posts
User *del_user = find_user(name, *user_ptr_del);
if (del_user == NULL) {
return 1;
}
Post *curr_post = del_user->first_post;
while (curr_post != NULL) {
Post *prev = curr_post;
curr_post = curr_post->next;
free(prev->date);
free(prev);
}
//second, find user before the user to delete, and remove user to delete from linked_list
User *curr_user = *user_ptr_del;
//check if the user to delete is the head of the linked list
if (strcmp(curr_user->name, name) == 0) {
*user_ptr_del = curr_user->next;
del_user->next = NULL;
} else {
while (strcmp((curr_user->next)->name, name) != 0) {
curr_user = curr_user->next;
}
curr_user->next = del_user->next;
del_user->next = NULL;
}
//third, delete user from friendslist of their friends
int i = 0;
while (del_user->friends[i] != NULL) {
User *curr_friend = find_user((del_user->friends[i])->name, *user_ptr_del);
int j = 0;
while (strcmp((curr_friend->friends[j])->name, name) != 0) {
j ++;
}
curr_friend->friends[j] = NULL;
i ++;
}
//fourth, free memory allocated for user
free(del_user);
return 0;
} | null | null |
63c648ba5f004bf4898cff22 | 2023-02-15T20:36:01.405Z | Question from Code | v1 | where is there a memory leak? by the way, the time_t pointer is freed in delete_post | #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) {
//check if name is too long
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
//Seperate case if linked list is empty
if (*user_ptr_add == NULL){
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
for (int i = 0; i < MAX_FRIENDS; i ++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
//make user head of the list
*user_ptr_add = new_user;
new_user->next = NULL;
strcpy(new_user->profile_pic, "empty_pic");
return 0;
}
// Traverse to end of linked_list; Compare usernames
User *curr = *user_ptr_add;
while (curr->next != NULL) {
if (strcmp(curr->name, name) == 0) {
return 1;
}
curr = curr->next;
}
//special case where user already exists at end of list
if (strcmp(curr->name, name) == 0) {
return 1;
}
// Declare new User object
User *new_user = malloc(sizeof(User));
//Initialize members of new_user
strcpy(new_user->name, name);
for (int i = 0; i < MAX_FRIENDS; i ++){
new_user->friends[i] = NULL;
}
new_user->first_post = NULL;
strcpy(new_user->profile_pic, "empty_pic");
//Now curr points to the tail
new_user->next = curr->next;
curr->next = 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) {
if (head == NULL) {
return NULL;
}
User *curr = (User *) head;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
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) {
User *new_curr = (User *) curr;
while (new_curr != NULL) {
printf("%s\n", new_curr->name);
new_curr = new_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) {
FILE *test_exists;
test_exists = fopen(filename, "r");
if (strlen(filename) > MAX_NAME){
return 2;
} else if (test_exists == NULL){
return 1;
} else {
strcpy(user->profile_pic, filename);
if (fclose(test_exists) != 0){
return 1;
}
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) {
//First, check if users dont exist
User *curr = head;
User *user1;
User *user2;
int found1 = 0;
int found2 = 0;
while (curr != NULL) {
if (strcmp(name1, curr->name) == 0){
found1 = 1;
user1 = curr;
} else if (strcmp(name2, curr->name) == 0){
found2 = 1;
user2 = curr;
}
curr = curr->next;
}
if (found1 == 0 || found2 == 0) {
return 4;
}
//Second, check if same user is passed in twice
if (strcmp(name1, name2) == 0){
return 3;
}
//Third, check if either of them has MAX_FRIENDS
int not_full1 = 0;
int not_full2 = 0;
for (int i = 0; i < MAX_FRIENDS; i ++) {
if (user1->friends[i] == NULL) {
not_full1 = 1;
}
if (user2->friends[i] == NULL) {
not_full2 = 1;
}
}
if (not_full1 == 0 || not_full2 == 0) {
return 2;
}
//Fourth, check if already friends
for (int i = 0; i < MAX_FRIENDS; i ++) {
if (user1->friends[i] != NULL) {
if (strcmp((user1->friends[i])->name, user2->name) == 0) {
return 1;
}
}
}
//Make friends
int index1 = 0;
int index2 = 0;
while (index1 < MAX_FRIENDS) {
if (user1->friends[index1] == NULL) {
user1->friends[index1] = user2;
break;
} else {
index1 ++;
}
}
while (index2 < MAX_FRIENDS) {
if (user2->friends[index2] == NULL) {
user2->friends[index2] = user1;
break;
} else {
index2 ++;
}
}
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;
}
//print profile pic
FILE *pfp = fopen(user->profile_pic, "r");
if (pfp != NULL) {
char line[61];
while (fgets(line, 61, pfp) != NULL) {
printf("%s", line);
}
printf("\n");
}
//print name
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
//print friends
printf("Friends: \n");
int i = 0;
while (user->friends[i] != NULL && i < MAX_FRIENDS) {
printf("%s\n", (user->friends[i])->name);
i ++;
}
printf("------------------------------------------\n");
printf("Posts: \n");
//print posts
if (user->first_post != NULL) {
printf("From: %s\n", (user->first_post)->author);
printf("Date: %s\n", ctime((user->first_post)->date));
printf("%s\n", (user->first_post)->contents);
if ((user->first_post)->next != NULL){
Post *curr = (user->first_post)->next;
while (curr != NULL) {
printf("\n===\n\n");
printf("From: %s\n", curr->author);
printf("Date: %s\n", ctime(curr->date));
printf("%s\n", curr->contents);
curr = curr->next;
}
}
}
printf("------------------------------------------\n");
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 (author == NULL || target == NULL) {
free(contents);
return 2;
}
//check if users are friends
int i = 0;
int is_friend = 0;
while (author->friends[i] != NULL) {
if (strcmp((author->friends[i])->name, target->name) == 0) {
is_friend = 1;
break;
}
i ++;
}
if (is_friend == 0) {
free(contents);
return 1;
} else {
//create and insert post at beginning of post linked list
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
time_t *ct = malloc(sizeof(time_t));
time(ct);
new_post->date = ct;
if (target->first_post == NULL) {
new_post->next = NULL;
target->first_post = new_post;
} else {
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) {
//first, find user and free memory from all their posts
User *del_user = find_user(name, *user_ptr_del);
if (del_user == NULL) {
return 1;
}
Post *curr_post = del_user->first_post;
while (curr_post != NULL) {
Post *prev = curr_post;
curr_post = curr_post->next;
free(prev->date);
free(prev);
}
//second, find user before the user to delete, and remove user to delete from linked_list
User *curr_user = *user_ptr_del;
//check if the user to delete is the head of the linked list
if (strcmp(curr_user->name, name) == 0) {
*user_ptr_del = curr_user->next;
del_user->next = NULL;
} else {
while (strcmp((curr_user->next)->name, name) != 0) {
curr_user = curr_user->next;
}
curr_user->next = del_user->next;
del_user->next = NULL;
}
//third, delete user from friendslist of their friends
int i = 0;
while (del_user->friends[i] != NULL) {
User *curr_friend = find_user((del_user->friends[i])->name, *user_ptr_del);
int j = 0;
while (strcmp((curr_friend->friends[j])->name, name) != 0) {
j ++;
}
curr_friend->friends[j] = NULL;
i ++;
}
//fourth, free memory allocated for user
free(del_user);
return 0;
} | null | null |
63c648ba5f004bf4898cff22 | 2023-03-15T09:08:20.158Z | Question from Code | v2 | why does this code not fill the tokens array properly | // tokenize line
char *tokens[MAXLINE];
int i = 0;
char *token = strtok(line, " \t\r\n");
tokens[i] = token;
i++;
int num_tokens = 1;
while (token != NULL) {
token = strtok(NULL, " \t\r\n");
tokens[i] = token;
i++;
num_tokens++;
}
// line is tokenized | null | null |
63c648ba5f004bf4898cff27 | 2023-01-17T20:56:55.329Z | Help Fix Code | v1 | null | 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;
}
void invest(double* principal, double rate){
principal = *principal * rate;
} | Invest should return principal multiplied by rate. | null |
63c648ba5f004bf4898cff27 | 2023-01-19T00:44:15.716Z | Help Fix Code | v1 | null | int main(){
char phone[11];
int num;
scanf("%10s %d", phone, num);
if (num == -1){
printf(phone);
return 0;
} else if (0 <= num <= 9) {
printf(phone[num]);
return 0;
} else {
printf("ERROR");
return 1;
}
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T02:25:55.627Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
printf("hi");
if (strlen(name) >= MAX_NAME){
return 2;
}
User *next_node = *user_ptr_add;
printf("%s\n", next_node->name);
while (next_node->next != NULL && next_node->name != name) {
next_node = next_node->next;
printf("%s\n", next_node->name);
}
if (next_node->name == name){
return 1;
}
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->next = NULL;
next_node->next = new_user;
return 0;
}
| iterate over linked list whose head is pointed to by **user_ptr_add and add a new node to the linked list with name *name | null |
63c648ba5f004bf4898cff27 | 2023-02-07T02:28:34.034Z | Question from Code | v1 | why is this giving a segmentation fault? | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME){
return 2;
}
User *next_node = *user_ptr_add;
while (next_node->next != NULL && strcmp(next_node->name, name)) {
next_node = next_node->next;
}
if (strcmp(next_node->name, name)){
return 1;
}
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->next = NULL;
next_node->next = new_user;
return 0;
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T02:32:49.605Z | Question from Code | v1 | why is this giving a segmentation error? | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME){
return 2;
}
User *next_node = *user_ptr_add;
while (next_node != NULL && strcmp(next_node->name, name)) {
if (next_node->next != NULL){
next_node = next_node->next;
} else {
break;
}
}
if (next_node != NULL && next_node->name != NULL && strcmp(next_node->name, name)){
return 1;
}
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->next = NULL;
next_node->next = new_user;
return 0;
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T02:34:48.215Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME){
return 2;
}
User *next_node = *user_ptr_add;
while (next_node != NULL && strcmp(next_node->name, name)) {
if (next_node->next != NULL){
next_node = next_node->next;
} else {
break;
}
}
if (next_node != NULL && next_node->name != NULL && strcmp(next_node->name, name)){
return 1;
}
User *new_user = malloc(sizeof(User));
new_user.name = malloc(sizeof(char) * MAX_NAME);
strcpy(new_user->name, name);
new_user->next = NULL;
next_node->next = new_user;
return 0;
} | traverse the linked list pointed to by **user_ptr_add and add a new node at the end with name value *name | null |
63c648ba5f004bf4898cff27 | 2023-02-07T02:35:53.929Z | Question from Code | v1 | why does this not compile and how do i fix it? | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME){
return 2;
}
User *next_node = *user_ptr_add;
while (next_node != NULL && strcmp(next_node->name, name)) {
if (next_node->next != NULL){
next_node = next_node->next;
} else {
break;
}
}
if (next_node != NULL && next_node->name != NULL && strcmp(next_node->name, name)){
return 1;
}
User *new_user = malloc(sizeof(User));
new_user.name = malloc(sizeof(char) * MAX_NAME);
strcpy(new_user->name, name);
new_user->next = NULL;
next_node->next = new_user;
return 0;
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T02:38:55.332Z | Question from Code | v1 | why does this give a segmentation error? | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME){
return 2;
}
User *next_node = *user_ptr_add;
while (next_node != NULL && strcmp(next_node->name, name)) {
if (next_node->next != NULL){
next_node = next_node->next;
} else {
break;
}
}
if (next_node != NULL && next_node->name != NULL && strcmp(next_node->name, name)){
return 1;
}
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->next = NULL;
next_node->next = new_user;
return 0;
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T02:42:57.479Z | General Question | v1 | if you have a pointer to a pointer to a linked list, how do you determine whether the head of the linked list is null? | null | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T02:48:41.314Z | Question from Code | v1 | why is this giving a segmentation fault | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME){
return 2;
}
User *next_node = *user_ptr_add;
while (next_node != NULL && strcmp(next_node->name, name)) {
if (next_node->next != NULL){
next_node = next_node->next;
} else {
break;
}
}
if (next_node == NULL){
strcpy(next_node->name, name);
} else if (next_node->next == NULL){
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->next = NULL;
next_node->next = new_user;
} else if (strcmp(next_node->name, name)) {
return 1;
}
return 0;
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T05:20:47.986Z | Question from Code | v1 | Given this structure, how do you determine whether the profile_pic attribute is empty | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T05:25:07.094Z | Question from Code | v1 | Given a user with the given structure, how do I check if the first character of the profile_pic attribute is the string terminating character | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T15:32:27.960Z | Question from Code | v1 | why does this give a segmentation fault | */
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
} else if (already_friends((User *) author, (User *) target) != 0) {
return 1;
} else {
time_t t;
time(&t);
if (target->first_post == NULL) {
target->first_post = malloc(sizeof(Post));
strcpy((target->first_post)->author, author->name);
strcpy((target->first_post)->contents, contents);
(target->first_post)->date = &t;
(target->first_post)->next = NULL;
} else {
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
new_post->date = &t;
new_post->next = target->first_post;
target->first_post = new_post;
}
return 0;
}
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T15:34:27.478Z | Question from Code | v1 | How do I save t to the heap so that I won't get a segmentation fault | */
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
} else if (already_friends((User *) author, (User *) target) != 0) {
return 1;
} else {
time_t t;
time(&t);
if (target->first_post == NULL) {
target->first_post = malloc(sizeof(Post));
strcpy((target->first_post)->author, author->name);
strcpy((target->first_post)->contents, contents);
(target->first_post)->date = &t;
(target->first_post)->next = NULL;
} else {
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
new_post->date = &t;
new_post->next = target->first_post;
target->first_post = new_post;
}
return 0;
}
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T15:35:14.010Z | Question from Code | v1 | How can I save the value of t
to the heap by allocating space for it and copying the value | */
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
} else if (already_friends((User *) author, (User *) target) != 0) {
return 1;
} else {
time_t t;
time(&t);
if (target->first_post == NULL) {
target->first_post = malloc(sizeof(Post));
strcpy((target->first_post)->author, author->name);
strcpy((target->first_post)->contents, contents);
(target->first_post)->date = &t;
(target->first_post)->next = NULL;
} else {
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
new_post->date = &t;
new_post->next = target->first_post;
target->first_post = new_post;
}
return 0;
}
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T15:40:06.116Z | Help Fix Code | v1 | null | */
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
} else if (already_friends((User *) author, (User *) target) != 0) {
return 1;
} else {
time_t t;
time(&t);
if (target->first_post == NULL) {
target->first_post = malloc(sizeof(Post));
strcpy((target->first_post)->author, author->name);
strcpy((target->first_post)->contents, contents);
(target->first_post)->date = &t;
(target->first_post)->next = NULL;
} else {
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
new_post->date = &t;
new_post->next = target->first_post;
target->first_post = new_post;
}
return 0;
}
}
| Add a new post to the start of the linked list target->first_post with contents contents and current time. | null |
63c648ba5f004bf4898cff27 | 2023-02-07T15:41:15.952Z | Help Fix Code | v1 | null | */
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
} else if (already_friends((User *) author, (User *) target) != 0) {
return 1;
} else {
time_t t;
time(&t);
if (target->first_post == NULL) {
target->first_post = malloc(sizeof(Post));
strcpy((target->first_post)->author, author->name);
strcpy((target->first_post)->contents, contents);
(target->first_post)->date = &t;
(target->first_post)->next = NULL;
} else {
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
new_post->date = &t;
new_post->next = target->first_post;
target->first_post = new_post;
}
return 0;
}
}
| Add a new post to the start of the linked list target->first_post with contents contents and a pointer to current time. | null |
63c648ba5f004bf4898cff27 | 2023-02-07T15:42:45.836Z | General Question | v1 | In a struct, how do I save a pointer to the current time as an attribute? | null | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T15:45:19.837Z | Help Fix Code | v1 | null | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
} else if (already_friends((User *) author, (User *) target) != 0) {
return 1;
} else {
time_t *t;
time(t);
if (target->first_post == NULL) {
target->first_post = malloc(sizeof(Post));
strcpy((target->first_post)->author, author->name);
strcpy((target->first_post)->contents, contents);
(target->first_post)->date = t;
(target->first_post)->next = NULL;
} else {
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
new_post->date = t;
new_post->next = target->first_post;
target->first_post = new_post;
}
return 0;
}
}
| Add a new post to the start of the linked list target->first_post with contents contents and a pointer to current time. | null |
63c648ba5f004bf4898cff27 | 2023-02-07T15:46:45.387Z | Question from Code | v1 | why does this give a segmentation fault | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
} else if (already_friends((User *) author, (User *) target) != 0) {
return 1;
} else {
time_t *t = malloc(sizeof(time_t));
time(t);
if (target->first_post == NULL) {
target->first_post = malloc(sizeof(Post));
strcpy((target->first_post)->author, author->name);
strcpy((target->first_post)->contents, contents);
(target->first_post)->date = t;
(target->first_post)->next = NULL;
} else {
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
new_post->date = t;
new_post->next = target->first_post;
target->first_post = new_post;
}
return 0;
}
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T18:21:54.963Z | Question from Code | v1 | Why does this give a segmentation fault? | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(curr_user->next);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (curr_user->name == name){
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *user->friends;
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends.
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *user->friends;
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T18:23:35.779Z | Question from Code | v1 | Why does this give a segmentation fault? | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post;
int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(curr_user->next);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (curr_user->name == name){
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *user->friends;
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends.
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *user->friends;
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T18:28:47.451Z | Question from Code | v1 | if User has attribute struct user *friends[size], why does this return a segmentation fault? | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(curr_user->next);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (curr_user->name == name){
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *user->friends;
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends.
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *user->friends;
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T18:36:49.721Z | Question from Code | v1 | Why does this give a segmentation error? | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(curr_user->next);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *user->friends;
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends.
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *user->friends;
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T18:44:26.243Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *(user->friends);
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends.
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *(user->friends);
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
} |
/*
* 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.
*/ | null |
63c648ba5f004bf4898cff27 | 2023-02-07T18:46:41.288Z | Question from Code | v1 | Why does this give a segmentation fault and how do I fix it | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *(user->friends);
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends.
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *(user->friends);
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T19:04:17.095Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
// User *first_friend = *temp->friends;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *user->friends;
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends. User
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *user->friends;
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
} | delete_user is supposed to delete the user with the name *name from the linked list headed by *user_ptr_del. rm_from_friends() is supposed to iterate over the friends of the user-to-be-deleted and remove itself from its friends' friend lists. | null |
63c648ba5f004bf4898cff27 | 2023-02-07T19:07:13.047Z | Question from Code | v1 | Why does this give a segmentation fault? | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
// User *first_friend = *temp->friends;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
rm_from_friends(curr_user);
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *user->friends;
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends. User
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *user->friends;
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T19:09:07.701Z | Question from Code | v1 | Why does this give a segmentation fault and how can I fix it? | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
// User *first_friend = *temp->friends;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
rm_from_friends(curr_user);
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
/*
* Remove user from all of its friends.
*/
void rm_from_friends(User *user){
User *friend = *user->friends;
while (friend != NULL){
rm_friend(friend, user);
friend = friend->next;
}
}
/*
* Helper for rm_from_friends. User
*/
void rm_friend(User *user, User *user_to_rm){
User *friend = *user->friends;
if (friend == NULL){
return;
}
while (friend->next != NULL){
if (strcmp((friend->next)->name, user_to_rm->name) == 0){
User *temp = friend->next;
friend->next = temp->next;
free(temp);
return;
}
friend = friend->next;
}
if (strcmp(friend->name, user_to_rm->name) == 0){
user->friends[0] = NULL;
free(friend);
}
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T21:05:58.155Z | Question from Code | v1 | Why does this code segmentation fault? | 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 (strcmp(name1, name2) == 0) {
return 3;
} else if (num_friends(user1) == MAX_FRIENDS || num_friends(user2) == MAX_FRIENDS){
return 2;
} else if (already_friends(user1, user2) == 0) {
return 1;
}
add_friend(user1, user2);
add_friend(user2, user1);
// create_user(name2, user1->friends);
// create_user(name1, user2->friends);
return 0;
}
/*
* Add user2 to user1's friend list.
*/
void add_friend(User *user1, User *user2){
for (int i=0; i<MAX_FRIENDS; i++){
if (user1->friends[i] == NULL){
user1->friends[i] = user2;
return;
}
}
}
/*
* Return the number of friends user has.
*/
int num_friends(User *user){
int friends = 0;
for (int i=0; i<MAX_FRIENDS; i++){
if (user->friends[i] != NULL){
friends++;
}
}
return friends;
/* User *friend = *user->friends;
while (friend != NULL){
friends++;
friend = friend->next;
}
return friends; */
}
int already_friends(User *user1, User *user2){
for (int i=0; i<MAX_FRIENDS; i++){
User *user2_friend = user2->friends[i];
if (strcmp(user2_friend->name, user1->name) == 0){
return 0;
}
}
return 1;
/* User *user2_friend = *user2->friends;
while (user2_friend != NULL){
if (strcmp(user2_friend->name, user1->name) == 0){
return 0;
}
user2_friend = user2_friend->next;
}
return 1; */
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T21:06:50.087Z | Question from Code | v1 | Why does this return a segmentation fault? | 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 (strcmp(name1, name2) == 0) {
return 3;
} else if (num_friends(user1) == MAX_FRIENDS || num_friends(user2) == MAX_FRIENDS){
return 2;
} else if (already_friends(user1, user2) == 0) {
return 1;
}
add_friend(user1, user2);
add_friend(user2, user1);
return 0;
}
/*
* Add user2 to user1's friend list.
*/
void add_friend(User *user1, User *user2){
for (int i=0; i<MAX_FRIENDS; i++){
if (user1->friends[i] == NULL){
user1->friends[i] = user2;
return;
}
}
}
/*
* Return the number of friends user has.
*/
int num_friends(User *user){
int friends = 0;
for (int i=0; i<MAX_FRIENDS; i++){
if (user->friends[i] != NULL){
friends++;
}
}
return friends;
}
int already_friends(User *user1, User *user2){
for (int i=0; i<MAX_FRIENDS; i++){
User *user2_friend = user2->friends[i];
if (strcmp(user2_friend->name, user1->name) == 0){
return 0;
}
}
return 1;
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T21:29:42.167Z | Question from Code | v1 | Does this code properly remove a user if it is the only one in the list? | */
int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
rm_from_friends(curr_user);
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T21:31:10.552Z | Question from Code | v1 | Suppose there is only user in the list of users. Why does the if statement after the while loop not remove the one user and set the list of users to be NULL? | */
int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
rm_from_friends(curr_user);
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T21:36:24.337Z | Question from Code | v1 | How do I update curr_user and the user list so that it is NULL if the function removes the last user in the list? | */
int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
rm_from_friends(curr_user);
free(curr_user);
*user_ptr_del = NULL;
return 0;
}
return 1;
}
| null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T21:40:53.824Z | Explain Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
rm_from_friends(curr_user);
// free(curr_user);
*user_ptr_del = NULL;
curr_user = NULL;
// free(curr_user);
return 0;
}
return 1;
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T21:45:19.349Z | Question from Code | v1 | What happens when name corresponds to the first user in the list of users? | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
rm_from_friends(curr_user);
// free(curr_user);
*user_ptr_del = NULL;
curr_user = NULL;
// free(curr_user);
return 0;
}
return 1;
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T21:46:09.840Z | Question from Code | v1 | Suppose name corresponds to the first user in the list of users. Why is it not deleted, and how do I fix it? | int delete_user(const char *name, User **user_ptr_del) {
User *curr_user = *user_ptr_del;
if (curr_user == NULL){
return 1;
}
while (curr_user->next != NULL) {
if (strcmp((curr_user->next)->name, name) == 0){
User *temp = curr_user->next;
rm_from_friends(temp);
curr_user->next = temp->next;
free(temp);
return 0;
}
curr_user = curr_user->next;
}
if (strcmp(curr_user->name, name) == 0){
rm_from_friends(curr_user);
// free(curr_user);
*user_ptr_del = NULL;
curr_user = NULL;
// free(curr_user);
return 0;
}
return 1;
} | null | null |
63c648ba5f004bf4898cff27 | 2023-02-07T22:18:04.113Z | Question from Code | v1 | How do I initialize new_user->friends so that I don't get a valgrind error | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME){
return 2;
}
User *next_node = *user_ptr_add;
if (next_node == NULL) {
*user_ptr_add = malloc(sizeof(User));
strcpy((*user_ptr_add)->name, name);
(*user_ptr_add)->profile_pic[0] = '\0';
(*user_ptr_add)->next = NULL;
return 0;
} else {
while (next_node->next != NULL) {
if (strcmp(next_node->name, name) == 0){
return 1;
}
next_node = next_node->next;
}
User *new_user = malloc(sizeof(User));
strcpy(new_user->name, name);
new_user->profile_pic[0] = '\0';
new_user->next = NULL;
next_node->next = new_user;
return 0;
}
} | null | null |
63c648bb5f004bf4898cff2c | 2023-03-13T16:11:59.861Z | Question from Code | v2 | is there a segmentation fault | Rule *find_rule(char *rule, Rule *head) {
Rule *curr = head;
while (curr != NULL) {
if (strcmp(rule, curr->target) == 0) {
return curr;
}
curr = curr->next_rule;
}
return NULL;
} | null | null |
63c648bb5f004bf4898cff2c | 2023-03-13T16:13:13.565Z | Question from Code | v2 | is there a segmentation fault | void add_rule(Rule *head, Rule *rule) {
Rule *curr = head;
while (curr->next_rule != NULL) {
curr = curr->next_rule;
}
curr->next_rule = rule;
} | null | null |
63c648bb5f004bf4898cff2c | 2023-03-13T16:13:44.901Z | Question from Code | v2 | is there a segmentation fault | Rule *add_target_line(char line[MAXLINE], Rule *head) {
int first_dependent = 1;
Rule *new_rule;
// Get target
char *token = strtok(line, " ");
Rule *old_rule = find_rule(token, head);
if (old_rule == NULL) {
new_rule = malloc(sizeof(Rule));
strcpy(new_rule->target, token);
new_rule->next_rule = NULL;
} else {
new_rule = old_rule;
}
// Remove :
token = strtok(NULL, " ");
while ((token = strtok(NULL, " ")) != NULL) {
Dependency *new_dep = make_dependency(token, head);
if (first_dependent == 1) {
new_rule->dependencies = new_dep;
first_dependent = 0;
} else {
add_dependency(new_rule->dependencies, new_dep);
}
}
return new_rule;
} | null | null |
63c648bb5f004bf4898cff2c | 2023-03-13T16:14:41.711Z | Question from Code | v2 | is there a segmentation fault | Action *add_action_line(char line[MAXLINE]) {
// Find number of arguments
int arguments = 1;
for (int i = 0; i < strlen(line); i++) {
if (line[i] == ' ') {
arguments++;
}
}
char **arr = malloc(sizeof(char *) * arguments);
int j = 0;
// Get first argument
char *token = strtok(line, "\t ");
arr[j] = token;
j++;
while ((token = strtok(NULL, " ")) != NULL) {
arr[j] = token;
j++;
}
Action *new_action = malloc(sizeof(Action));
new_action->args = arr;
new_action->next_act = NULL;
return new_action;
} | null | null |
63c648bb5f004bf4898cff2c | 2023-03-13T16:15:17.539Z | Question from Code | v2 | is there a segmentation fault | Dependency *make_dependency(char *target, Rule *head) {
Rule *rule = find_rule(target, head);
if (rule == NULL) {
Rule *new_rule = malloc(sizeof(Rule));
strcpy(new_rule->target, target);
new_rule->next_rule = NULL;
add_rule(head, new_rule);
rule = new_rule;
}
Dependency *new_dep = malloc(sizeof(Dependency));
new_dep->rule = rule;
new_dep->next_dep = NULL;
return new_dep;
} | null | null |
63c648bb5f004bf4898cff2c | 2023-03-13T16:15:52.792Z | Question from Code | v2 | is there a segmentation fault | Rule *parse_file(FILE *fp) {
// Implement this function and remove the stubbed return statement below.
char *line;
Rule *head = NULL;
int first_rule = 1;
int first_action = 1;
Rule *new_rule;
// For reading file lines
while ((line = fgets(line, MAXLINE, fp)) != NULL) {
if (is_comment_or_empty(line) == 0) {
if (line[0] == '\t') {
Action *new_action = add_action_line(line);
if (first_action == 1) {
new_rule->actions = new_action;
first_action = 0;
} else {
add_action(new_rule->actions, new_action);
}
} else {
first_action = 1;
new_rule = add_target_line(line, head);
if (first_rule == 1) {
head = new_rule;
first_rule = 0;
} else {
add_rule(head, new_rule);
}
}
}
}
return head;
} | null | null |
63c648bb5f004bf4898cff2c | 2023-04-25T00:17:56.471Z | Question from Code | v2 | If the file is run from command line with 2 0 9 what is the output | int main(int argc, int **argv) {
int sum;
sum = argv[1] + argv[2] + argv[3];
printf("%d", sum);
return 0;
} | null | null |
63c648bb5f004bf4898cff2c | 2023-04-25T01:12:13.841Z | Question from Code | v2 | what is wrong with this code | char *return_s() {
char *s = "hello world";
return s;
}
int main() {
char *r = return_s();
free(r);
} | null | null |
63c648bb5f004bf4898cff31 | 2023-01-27T16:33:27.754Z | Explain Code | v1 | null | void fib(int **name, int n){
*name = malloc(sizeof(int) * n);
*name[0] = 0;
*name[1] = 1;
for (int i = 2; i < n; i++){
*name[i] = *name[i - 2] + *name[i - 1];
}
} | null | null |
63c648bb5f004bf4898cff31 | 2023-01-27T16:35:43.262Z | Help Fix Code | v1 | null | void fib(int **name, int n){
*name = malloc(sizeof(int) * n);
*name[0] = 0;
*name[1] = 1;
for (int i = 2; i < n; i++){
*name[i] = *name[i - 2] + *name[i - 1];
}
} | give first n term of Fibonacci sequence
| null |
63c648bb5f004bf4898cff31 | 2023-03-30T14:19:12.585Z | Explain Code | v2 | null | if (FD_ISSET(sock_fd, &listen_fds)) {
int client_fd = accept_connection(sock_fd, users);
if (client_fd > max_fd) {
max_fd = client_fd;
}
FD_SET(client_fd, &all_fds);
printf("Accepted connection\n");
}
| null | null |
63c648bc5f004bf4898cff36 | 2023-01-18T23:41:10.496Z | General Question | v1 | how to scanf a string | null | null | null |
63c648bc5f004bf4898cff36 | 2023-02-12T00:55:14.152Z | General Question | v1 | how to know how elements are present in an array, not the size of the array? | null | null | null |
63c648bc5f004bf4898cff36 | 2023-02-12T00:55:40.064Z | General Question | v1 | how to know how many elements are in an array? | null | null | null |
63c648bd5f004bf4898cff3b | 2023-01-26T03:14:16.727Z | General Question | v1 | how to use strtol() ?
| null | null | null |
63c648bd5f004bf4898cff3b | 2023-01-26T03:25:59.813Z | Explain Code | v1 | null | int **split_array(const int *s, int length) {
int **result = malloc(sizeof(int*) * 2);
if (length % 2 == 0) {
result[0] = malloc(sizeof(int) * (length / 2));
}
else {
result[0] = malloc(sizeof(int) * ((length / 2) + 1));
}
result[1] = malloc(sizeof(int) * (length / 2));
int e = 0;
for (int i = 0; i < length; i += 2) {
result[0][e] = s[i];
e++;
}
int o = 0;
for (int i = 1; i < length; i += 2) {
result[1][o] = s[i];
o++;
}
return result;
}
/* 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 *ints;
ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = strtol(strs[i + 1], NULL, 10);
}
return ints;
}
| null | null |
63c648bd5f004bf4898cff3b | 2023-01-26T03:30:49.803Z | Question from Code | v1 | how can i improve this? | int **split_array(const int *s, int length) {
int **result = malloc(sizeof(int*) * 2);
if (length % 2 == 0) {
result[0] = malloc(sizeof(int) * (length / 2));
}
else {
result[0] = malloc(sizeof(int) * ((length / 2) + 1));
}
result[1] = malloc(sizeof(int) * (length / 2));
int e = 0;
for (int i = 0; i < length; i += 2) {
result[0][e] = s[i];
e++;
}
int o = 0;
for (int i = 1; i < length; i += 2) {
result[1][o] = s[i];
o++;
}
return result;
}
/* 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 *ints;
ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = strtol(strs[i + 1], NULL, 10);
}
return ints;
}
| null | null |
63c648bd5f004bf4898cff3b | 2023-01-29T07:38:40.933Z | Explain Code | v1 | null | int num = 0;
int num2 = 1;
while (num == 0) {
if (num2 != 1) {
continue;
}
} | null | null |
63c648bd5f004bf4898cff3b | 2023-01-29T07:43:26.397Z | General Question | v1 | how to initialize a char array | null | null | null |
63c648bd5f004bf4898cff3b | 2023-01-29T08:01:12.327Z | Help Fix Code | v1 | null | char arr[3] = {'-', 'b', 'c'};
if (arr[0] == '-') {
printf("match");
} | "match" | null |
63c648bd5f004bf4898cff3b | 2023-01-29T08:02:17.436Z | Help Fix Code | v1 | null | char arr[3] = {'-', 'b', 'c'};
if (arr[0] == '-') {
printf("match");
} | See if the first array element matches '-' | null |
63c648bd5f004bf4898cff3b | 2023-01-29T08:33:31.160Z | Explain Code | v1 | null | int i = 8;
while (sin > 0) {
sin_array[i] = sin % 10;
sin /= 10;
i--;
}
// Return 1 if the int does not have 9 digits.
if (i != 0) {
return 1;
}
return 0; | null | null |
63c648bd5f004bf4898cff3b | 2023-01-29T08:34:27.837Z | Help Fix Code | v1 | null | int i = 8;
while (sin > 0) {
sin_array[i] = sin % 10;
sin /= 10;
i--;
}
// Return 1 if the int does not have 9 digits.
if (i != 0) {
return 1;
}
return 0; | takes an integer and stores the digits of the integer in an array. The code returns 1 if the integer is not 9 digits long. | null |
63c648bd5f004bf4898cff3b | 2023-02-02T07:45:12.642Z | General Question | v1 | can i cast strlen() to int? | null | null | null |
63c648bd5f004bf4898cff3b | 2023-02-12T08:39:47.629Z | General Question | v1 | how do i initialize current time and allocate heap memory for it? | null | null | null |
63c648bd5f004bf4898cff3b | 2023-02-12T08:55:25.359Z | General Question | v1 | how do i find the current time and convert the time value to a string | null | null | null |
63c648bd5f004bf4898cff3b | 2023-02-12T08:59:30.151Z | General Question | v1 | generate a detailed documentation of `time` with usage examples and explanations | null | null | null |
63c648bd5f004bf4898cff3b | 2023-02-13T07:11:58.351Z | General Question | v1 | how do i use gdb | null | null | null |
63c648bd5f004bf4898cff3b | 2023-02-13T08:52:21.596Z | Question from Code | v1 | what will the output be? | #define SUPERVISOR(regular) regular + 5
int main() {
int regular_pay = 20;
int hours_worked = 10;
printf("pay is %d\n", (hours_worked * SUPERVISOR(regular_pay)));
// rest omitted
} | null | null |
63c648bd5f004bf4898cff3b | 2023-02-13T08:53:01.818Z | Question from Code | v1 | what will be printed from this code? | #define SUPERVISOR(regular) regular + 5
int main() {
int regular_pay = 20;
int hours_worked = 10;
printf("pay is %d\n", (hours_worked * SUPERVISOR(regular_pay)));
// rest omitted
} | null | null |
63c648bd5f004bf4898cff3b | 2023-03-08T06:42:14.486Z | General Question | v1 | does strcpy() include null terminator? | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-08T07:18:05.667Z | General Question | v1 | is "does_rule_exist" a good function name if i want to check if a rule already exists in a linked list of rules? | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-09T03:50:31.489Z | General Question | v1 | why is my code getting stuck at a call to strcspn | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-09T03:55:56.891Z | Help Fix Code | v1 | null | Rule *parse_file(FILE *fp) {
Rule *rules_head = malloc(sizeof(Rule));
Rule *curr = rules_head;
Dependency *prev_dep = NULL;
Rule *p = NULL;
Action *prev_action = NULL;
char line[MAXLINE + 1];
char *token;
printf("start1");
while (fgets(line, MAXLINE, fp) != NULL) {
printf("start");
line[strcspn(line, "\n")] = '\0';
line[strcspn(line, "\r")] = '\0';
if (is_comment_or_empty(line) == 1) {
continue;
}
if (line[0] == '\t') { // line is a target line
if (curr != rules_head) {
Rule *new_rule = malloc(sizeof(Rule));
curr->next_rule = new_rule;
curr = new_rule;
prev_action = NULL;
}
token = strtok(line, " "); // first token is the target word
curr->target = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(curr->target, token);
token = strtok(line, " "); // second token is the colon
// Loop through dependencies
while ((token = strtok(line, " ")) != NULL) {
Dependency *dep = malloc(sizeof(Dependency));
if ((p = rule_in_list(rules_head, token)) != NULL) {
dep->rule = p;
}
else {
Rule *new_rule = malloc(sizeof(Rule));
new_rule->target = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(new_rule->target, token);
new_rule->next_rule = NULL;
curr->next_rule = new_rule;
dep->rule = new_rule;
}
dep->next_dep = NULL;
if (prev_dep != NULL) {
prev_dep->next_dep = dep;
}
prev_dep = dep;
}
printf("target done");
prev_dep = NULL;
}
else { // line is an action line
Action *action = malloc(sizeof(Action));
char l[strlen(line + 1)];
strcpy(l, line);
int size = 0;
while ((token = strtok(l, " ")) != NULL) {
size++;
}
action->args = malloc(sizeof(char *) * (size + 1));
action->args[size] = NULL;
int i = 0;
while ((token = strtok(line, " ")) != NULL) {
action->args[i] = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(action->args[i], token);
i++;
}
if (prev_action != NULL) {
prev_action->next_act = action;
}
else {
curr->actions = action;
}
action->next_act = NULL;
prev_action = action;
printf("action done");
}
}
printf("parse done!\n");
return rules_head;
} | parse the make file to create a linked list of rules. | null |
63c648bd5f004bf4898cff3b | 2023-03-09T03:58:09.980Z | Explain Code | v1 | null | Rule *parse_file(FILE *fp) {
Rule *rules_head = malloc(sizeof(Rule));
Rule *curr = rules_head;
Dependency *prev_dep = NULL;
Rule *p = NULL;
Action *prev_action = NULL;
char line[MAXLINE + 1];
char *token;
printf("start1");
while (fgets(line, MAXLINE, fp) != NULL) {
printf("start");
line[strcspn(line, "\n")] = '\0';
line[strcspn(line, "\r")] = '\0';
if (is_comment_or_empty(line) == 1) {
continue;
}
if (line[0] == '\t') { // line is a target line
if (curr != rules_head) {
Rule *new_rule = malloc(sizeof(Rule));
curr->next_rule = new_rule;
curr = new_rule;
prev_action = NULL;
}
token = strtok(line, " "); // first token is the target word
curr->target = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(curr->target, token);
token = strtok(line, " "); // second token is the colon
// Loop through dependencies
while ((token = strtok(line, " ")) != NULL) {
Dependency *dep = malloc(sizeof(Dependency));
if ((p = rule_in_list(rules_head, token)) != NULL) {
dep->rule = p;
}
else {
Rule *new_rule = malloc(sizeof(Rule));
new_rule->target = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(new_rule->target, token);
new_rule->next_rule = NULL;
curr->next_rule = new_rule;
dep->rule = new_rule;
}
dep->next_dep = NULL;
if (prev_dep != NULL) {
prev_dep->next_dep = dep;
}
prev_dep = dep;
}
printf("target done");
prev_dep = NULL;
}
else { // line is an action line
Action *action = malloc(sizeof(Action));
char l[strlen(line + 1)];
strcpy(l, line);
int size = 0;
while ((token = strtok(l, " ")) != NULL) {
size++;
}
action->args = malloc(sizeof(char *) * (size + 1));
action->args[size] = NULL;
int i = 0;
while ((token = strtok(line, " ")) != NULL) {
action->args[i] = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(action->args[i], token);
i++;
}
if (prev_action != NULL) {
prev_action->next_act = action;
}
else {
curr->actions = action;
}
action->next_act = NULL;
prev_action = action;
printf("action done");
}
}
printf("parse done!\n");
return rules_head;
} | null | null |
63c648bd5f004bf4898cff3b | 2023-03-09T03:59:20.774Z | Question from Code | v1 | why is my code getting infinitely stuck at the call to strcspn? | Rule *parse_file(FILE *fp) {
Rule *rules_head = malloc(sizeof(Rule));
Rule *curr = rules_head;
Dependency *prev_dep = NULL;
Rule *p = NULL;
Action *prev_action = NULL;
char line[MAXLINE + 1];
char *token;
printf("start1");
while (fgets(line, MAXLINE, fp) != NULL) {
printf("start");
line[strcspn(line, "\n")] = '\0';
line[strcspn(line, "\r")] = '\0';
if (is_comment_or_empty(line) == 1) {
continue;
}
if (line[0] == '\t') { // line is a target line
if (curr != rules_head) {
Rule *new_rule = malloc(sizeof(Rule));
curr->next_rule = new_rule;
curr = new_rule;
prev_action = NULL;
}
token = strtok(line, " "); // first token is the target word
curr->target = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(curr->target, token);
token = strtok(line, " "); // second token is the colon
// Loop through dependencies
while ((token = strtok(line, " ")) != NULL) {
Dependency *dep = malloc(sizeof(Dependency));
if ((p = rule_in_list(rules_head, token)) != NULL) {
dep->rule = p;
}
else {
Rule *new_rule = malloc(sizeof(Rule));
new_rule->target = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(new_rule->target, token);
new_rule->next_rule = NULL;
curr->next_rule = new_rule;
dep->rule = new_rule;
}
dep->next_dep = NULL;
if (prev_dep != NULL) {
prev_dep->next_dep = dep;
}
prev_dep = dep;
}
printf("target done");
prev_dep = NULL;
}
else { // line is an action line
Action *action = malloc(sizeof(Action));
char l[strlen(line + 1)];
strcpy(l, line);
int size = 0;
while ((token = strtok(l, " ")) != NULL) {
size++;
}
action->args = malloc(sizeof(char *) * (size + 1));
action->args[size] = NULL;
int i = 0;
while ((token = strtok(line, " ")) != NULL) {
action->args[i] = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(action->args[i], token);
i++;
}
if (prev_action != NULL) {
prev_action->next_act = action;
}
else {
curr->actions = action;
}
action->next_act = NULL;
prev_action = action;
printf("action done");
}
}
printf("parse done!\n");
return rules_head;
} | null | null |
63c648bd5f004bf4898cff3b | 2023-03-10T05:52:22.060Z | Help Write Code | v2 | null | null | null | Using fork, exec, pipe, dup2, write, wait (along with WIFEXITED, WEXITSTATUS), create a child process that runs an executable using arguments it reads from the parent process. |
63c648bd5f004bf4898cff3b | 2023-03-17T02:19:06.982Z | Help Fix Code | v2 | write every integer from 0 to 99 to a binary file fp | int n = 0;
for (int i = 0; i < 100; i++) {
n = fwrite(&i, sizeof(int), 1, fp);
if (n != 1) {
perror("fwrite");
}
}
| null | null |
63c648bd5f004bf4898cff3b | 2023-03-17T02:21:18.447Z | Help Fix Code | v2 | write every integer from 0 to 99 to a binary file fp |
FILE *fp;
if ((fp = fopen(argv[1], "w")) == NULL) {
perror("fopen");
exit(1);
}
int n = 0;
for (int i = 0; i < 100; i++) {
n = fwrite(&i, sizeof(int), 1, fp);
if (n != 1) {
perror("fwrite");
}
}
fclose(fp);
return 0; | null | null |