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
63c647695f004bf4898cf38d
2023-02-13T01:25:44.446Z
Help Fix Code
v1
null
/* * 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) { return 2; } if (!are_friends(author, target)) { return 1; } Post *new_post = (Post *)malloc(sizeof(Post)); new_post->contents = contents; new_post->next = target->first_post; Post *p = author->first_post; while (p->next != NULL){ p = p->next; } p->next = new_post; 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 */
null
63c647695f004bf4898cf38d
2023-02-13T01:59:51.672Z
Explain Code
v1
null
/* * 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) { return 2; } if (!are_friends(author, target)) { return 1; } Post *new_post = (Post *)malloc(sizeof(Post)); new_post->contents = contents; new_post->next = target->first_post; target->first_post = new_post; Post *p = author->first_post; while (p->next != NULL){ p = p->next; } p->next = new_post; return 0; }
null
null
63c647695f004bf4898cf38d
2023-02-13T02:07:26.663Z
Help Fix Code
v1
null
* 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) { return 2; } if (!are_friends(author, target)) { return 1; } Post *new_post = (Post *)malloc(sizeof(Post)); new_post->author = author->name; new_post->contents = contents; new_post->next = target->first_post; target->first_post = new_post; 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 */
null
63c647695f004bf4898cf38d
2023-02-13T20:26:37.587Z
Help Fix Code
v1
null
FILE *file; char line[4096]; file = fopen(user->profile_pic, "r"); while (fgets(line, 4096, file) != NULL) { printf("%s", line); } fclose(file);
Open a file and print each line in the file to standard output
null
63c647695f004bf4898cf38d
2023-02-13T20:54:04.454Z
Help Fix Code
v1
null
FILE *file; char line[4096]; char string[32]; strncpy(string, user->profile_pic, 32); file = fopen(string, "r"); while (fgets(line, 4096, file) != NULL) { printf("%s", line); } fclose(file);
STORE the name of the file. Then you read the file and immediately print it back out. You can do this in pieces and never store the entire thing. If you think about it for a while, you will realize that you don't need to assume a maximum line length for using fgets.
null
63c647695f004bf4898cf38d
2023-02-15T05:44:24.782Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } if (!are_friends(author, target)) { return 1; } Post *new_post = (Post *)malloc(sizeof(Post)); strcpy(new_post->author, author->name); time_t *ltime; ltime =(time_t*)malloc(sizeof(*ltime)); time(ltime); new_post->date = ltime; new_post->contents = contents; new_post->next = target->first_post; target->first_post = new_post; free(new_post->contents); free(new_post->date); free(new_post); 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 */ Free memory after use
null
63c647695f004bf4898cf38d
2023-03-14T01:46:47.061Z
Help Fix Code
v2
You will need to use fork and execvp to the run the command specified by an action. Fortunately in Part 1, you already prepared the array of arguments to pass into execvp. The parent process should wait for each child to complete. If the child terminates with a non-zero exit status, the parent should also terminate with a non-zero exit status. This means that pmake will stop when it encounters an error.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include "pmake.h" void execute_action(Action *action) { printf("%s\n", *action->args); pid_t pid = fork(); if (pid == 0) { execvp(action->args[0], action->args); fprintf(stderr, "Error: execvp failed\n"); exit(1); } else if (pid < 0) { fprintf(stderr, "Error: Fork failed\n"); exit(1); } else { int status; waitpid(pid, &status, 0); if (WIFSIGNALED(status) || WEXITSTATUS(status) != 0) { fprintf(stderr, "Error: Child process exited with non-zero status\n"); exit(1); } } }
null
null
63c6476a5f004bf4898cf397
2023-02-15T01:57:37.027Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { if (strlen(name)>= MAX_NAME){ return 2; } //Initializing a new user with name "name" and .next set to NULL to be added User *new_user; new_user = malloc(sizeof(struct user)); strncpy(new_user->name, name, MAX_NAME); new_user->first_post = NULL; // new_user->profile_pic = NULL; new_user->friends = (User*)calloc(MAX_FRIENDS, sizeof(User)); // User *friends[MAX_FRIENDS]; // new_user->friends = friends; // if (friends == NULL) { // return 2; // } // = friends; new_user->next = NULL; if (user_ptr_add != NULL){ User *curr_user = *user_ptr_add; while (curr_user->next!= NULL){ if (strcmp(curr_user->name, name)==0){ return 1; } curr_user = curr_user->next; } if (strcmp(curr_user->name, name)==0){ return 1;} curr_user->next = new_user; return 0; }else{ // struct user *new_user; // printf("New user initialized\n"); // new_user = malloc(sizeof(struct user)); // printf("malloc\n"); // strncpy(new_user->name, name, MAX_NAME); // printf("name set\n"); // new_user->first_post = NULL; // printf("1st post done\n"); // // new_user->profile_pic = NULL; // // new_user->friends = NULL; // new_user->next = NULL; // printf("set next to nill\n"); *user_ptr_add = new_user; return 0; } }
* 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)
null
63c6476b5f004bf4898cf39c
2023-01-24T01:28:52.498Z
General Question
v1
syntax for strtol
null
null
null
63c6476b5f004bf4898cf39c
2023-01-24T01:29:28.471Z
General Question
v1
how does scanf work?
null
null
null
63c6476b5f004bf4898cf39c
2023-02-01T05:51:57.934Z
General Question
v1
how does malloc work?
null
null
null
63c6476b5f004bf4898cf39c
2023-02-14T08:59:26.715Z
General Question
v1
what does strncpy do?
null
null
null
63c6476b5f004bf4898cf39c
2023-02-14T09:00:30.196Z
Help Write Code
v1
null
null
null
How does strncpy work?
63c6476b5f004bf4898cf39c
2023-03-17T21:49:23.736Z
General Question
v2
How does wait work?
null
null
null
63c6476b5f004bf4898cf39c
2023-03-17T21:50:12.266Z
General Question
v2
what are the attributes of the struct time_t
null
null
null
63c6476b5f004bf4898cf39c
2023-03-23T05:00:40.129Z
General Question
v2
how does bind work
null
null
null
63c6476b5f004bf4898cf39c
2023-03-23T05:01:25.269Z
Help Write Code
v2
null
null
null
how to find a character in a character array without a null terminator
63c6476e5f004bf4898cf3b5
2023-03-23T17:49:49.910Z
Help Fix Code
v2
print hello world
print("Helloworld");
null
null
63c6476e5f004bf4898cf3b5
2023-03-23T17:50:48.733Z
Help Fix Code
v2
print Hello world
printf("Hello world");
null
null
63c6476e5f004bf4898cf3b5
2023-03-23T17:51:13.198Z
Help Fix Code
v2
print Hello world
printf("Hello world\n");
null
null
63c647705f004bf4898cf3c4
2023-02-14T16:40:01.041Z
General Question
v1
what is valgrind?
null
null
null
63c647705f004bf4898cf3c4
2023-02-14T16:45:08.369Z
General Question
v1
what is the best way to debug c code?
null
null
null
63c647705f004bf4898cf3c4
2023-02-14T17:10:00.285Z
General Question
v1
is a null pointer different from null?
null
null
null
63c647705f004bf4898cf3c4
2023-03-08T05:04:51.815Z
General Question
v1
how to count number of spaces in a string
null
null
null
63c647705f004bf4898cf3c4
2023-03-09T17:59:17.293Z
General Question
v2
what does getopt do?
null
null
null
63c647705f004bf4898cf3c4
2023-03-10T04:39:55.998Z
General Question
v2
what does the exec function do
null
null
null
63c647705f004bf4898cf3c4
2023-03-10T18:23:27.712Z
General Question
v2
how to use dup2
null
null
null
63c647705f004bf4898cf3c4
2023-03-14T21:29:46.419Z
General Question
v2
how to use stat()
null
null
null
63c647705f004bf4898cf3c4
2023-04-02T22:23:25.093Z
General Question
v2
what is asprintf and how do i use it
null
null
null
63c647705f004bf4898cf3c4
2023-04-23T19:10:20.144Z
Question from Code
v2
what is line 3 doing
FLAGS = -Wall - g - std = gnu99 all : classifier test_load_data classifier : dectree.o classifier.o gcc ${FLAGS} - o classifier dectree.o classifier.o - lm test_load_data : dectree.o test_load_data.o gcc ${FLAGS} - o test_load_data dectree.o test_load_data.o - lm classifier.o : classifier.c dectree.h gcc ${FLAGS} - c classifier.c test_load_data.o : test_load_data.c dectree.h gcc ${FLAGS} - c test_load_data.c dectree.o : dectree.c dectree.h gcc ${FLAGS} - c dectree.c datasets : datasets.tgz tar xzf datasets.tgz.PHONY : clean all clean : rm classifier test_load_data *.o
null
null
63c647705f004bf4898cf3c4
2023-04-24T02:47:39.158Z
General Question
v2
in a makefile, are actions called if a target has no dependencies?
null
null
null
63c647705f004bf4898cf3c4
2023-04-24T16:16:33.334Z
General Question
v2
what does wait() do?
null
null
null
63c647705f004bf4898cf3c4
2023-04-24T16:33:06.066Z
General Question
v2
what does dup2() do?
null
null
null
63c647705f004bf4898cf3c4
2023-04-24T16:45:57.001Z
General Question
v2
how to use pipe()
null
null
null
63c647735f004bf4898cf3e2
2023-01-18T21:53:27.099Z
General Question
v1
What value is returned by scanf when the input stream is closed?
null
null
null
63c647735f004bf4898cf3e2
2023-01-18T21:53:58.520Z
General Question
v1
What is the EOF symbol in C?
null
null
null
63c647745f004bf4898cf3e7
2023-01-27T20:03:54.977Z
Question from Code
v1
What is wrong with this code?
#include <stdlib.h> #include <stdio.h> void fib(int **arr, int n) { *arr = malloc(sizeof(int) * n); *arr[0] = 0; int index = 1; if (n>=2) { *arr[1] = 1; index = 2; } while (index < n) { *arr[index] = *arr[index - 1] + *arr[index -2]; index ++; } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c647745f004bf4898cf3e7
2023-01-27T20:05:51.724Z
Help Fix Code
v1
null
#include <stdlib.h> #include <stdio.h> void fib(int **arr, int n) { *arr = malloc(sizeof(int) * n); *arr[0] = 0; int index = 1; if (n>=2) { *arr[1] = 1; index = 2; } while (index < n) { *arr[index] = *arr[index - 1] + *arr[index -2]; index ++; } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
build a dynamic array containing the first n fibonacci numbers, assuming that n is greater than or equal to 1.
null
63c647745f004bf4898cf3e7
2023-01-29T02:51:54.328Z
General Question
v1
how do i get the output of a shell command in my c program?
null
null
null
63c647745f004bf4898cf3e7
2023-01-29T04:29:29.621Z
General Question
v1
in the c programming language, how do i skip the rest of a line using scanf once I've gotten the data I wanted to use?
null
null
null
63c647745f004bf4898cf3e7
2023-01-29T04:31:44.137Z
General Question
v1
how do i skip the rest of the line using scanf if there are spaces remaining?
null
null
null
63c647745f004bf4898cf3e7
2023-01-29T04:33:18.003Z
General Question
v1
If I pipe the output of "ls -l" into a c program, how can I use scanf to get the size of each file?
null
null
null
63c647745f004bf4898cf3e7
2023-01-29T04:35:59.302Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } int size = strtol(argv[1], NULL, 10); int total = 0; int cur; char permissions[11]; scanf("%*s %*d"); while (scanf("%s %*d %*s %*s %d %*s", permissions, &cur) != EOF) { printf("%s %d\n", permissions, cur); } return 0; }
I am piping the following output into this code: -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz it should output: -rwx------1734 -rw------- 21510 -rwxr-xr-x 8968
null
63c647745f004bf4898cf3e7
2023-01-29T04:37:28.277Z
Question from Code
v1
I am piping the following output into this code: -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz it should output: -rwx------1734 -rw------- 21510 -rwxr-xr-x 8968
#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } int size = strtol(argv[1], NULL, 10); //int total = 0; int cur; char permissions[11]; while (scanf("%s %*d %*s %*s %d %*s", permissions, &cur) != EOF) { printf("%s %d\n", permissions, cur); } return 0; }
null
null
63c647745f004bf4898cf3e7
2023-01-29T04:39:46.758Z
Help Write Code
v1
null
null
null
I am piping the following output into this code: -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz it should use scanf to output: -rwx------1734 -rw------- 21510 -rwxr-xr-x 8968
63c647745f004bf4898cf3e7
2023-01-29T12:24:26.420Z
Question from Code
v1
after passing sin_array into the populate_array function, how do i access the modified array?
#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) { return 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_int = strtol(argv[1], NULL, 10); int *sin_array = malloc(sizeof(int) * 9); if (populate_array(sin_int, sin_array) == 1) { for (int i = 0; i < 9; i ++) { printf("%d\n", sin_array[i]); } return 1; } else if (check_sin(sin_array)) { return 1; } return 0; }
null
null
63c647745f004bf4898cf3e7
2023-02-15T09:45:42.310Z
Question from Code
v1
Why do I get a segmentation fault from this code?
int create_user(const char *name, User **user_ptr_add) { // Check length if (strlen(name) > MAX_NAME) { return 2; } // Search for given User if (find_user(name, *user_ptr_add) != NULL) { return 1; } // Create User User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, MAX_NAME); User *curr = *user_ptr_add; while (curr->next != NULL) { curr = curr->next; } curr->next = new_user; return 0; }
null
null
63c647745f004bf4898cf3e7
2023-02-15T10:24:30.234Z
General Question
v1
If a struct contains an attribute which is an array of pointers to another struct, do I need to malloc any new elements to the array when adding them in a helper function?
null
null
null
63c647745f004bf4898cf3e7
2023-02-15T12:08:49.120Z
General Question
v1
How can I use time_t and ctime to get the current date/time as a string in the following format: "Friday Jan 27 23:01:00 2023"
null
null
null
63c647745f004bf4898cf3e7
2023-02-15T14:33:07.070Z
Question from Code
v1
Why does this code not correctly set the friend_of_deleted->friends[j] element to NULL? I want to delete the element user_to_be_deleted from the array called friend_of_deleted->friends
int delete_user(const char *name, User **user_ptr_del) { User *user_to_be_deleted = find_user(name, *user_ptr_del); // Check existence if (user_to_be_deleted == NULL) { return 1; } // Delete from main user list if first if (*user_ptr_del == user_to_be_deleted) { *user_ptr_del = (*user_ptr_del)->next; return 0; } // Delete from main user list if not first User *curr = *user_ptr_del; while (curr->next != user_to_be_deleted) { curr = curr->next; } curr->next = curr->next->next; return 0; // Delete friends User *friend_of_deleted; for (int i = 0; i < MAX_FRIENDS; i++) { friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); for (int j = 0; j < MAX_FRIENDS - 1; j++) { if (friend_of_deleted->friends[j] == user_to_be_deleted) { friend_of_deleted->friends[j] = NULL; } } } }
null
null
63c647745f004bf4898cf3e7
2023-02-15T14:36:10.614Z
Question from Code
v1
Why does this code not correctly set the friend_of_deleted->friends[j] element to NULL? I want to delete the element user_to_be_deleted from the array called friend_of_deleted->friends
int delete_user(const char *name, User **user_ptr_del) { User *user_to_be_deleted = find_user(name, *user_ptr_del); // Check existence if (user_to_be_deleted == NULL) { return 1; } // Delete from main user list if first if (*user_ptr_del == user_to_be_deleted) { *user_ptr_del = (*user_ptr_del)->next; return 0; } // Delete from main user list if not first User *curr = *user_ptr_del; while (curr->next != user_to_be_deleted) { curr = curr->next; } curr->next = curr->next->next; return 0; // Delete friends User *friend_of_deleted; for (int i = 0; i < MAX_FRIENDS; i++) { friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); for (int j = 0; j < MAX_FRIENDS - 1; j++) { if (strcmp(friend_of_deleted->friends[j]->name, user_to_be_deleted->name) == 0) { friend_of_deleted->friends[j] = NULL; } } } }
null
null
63c647745f004bf4898cf3e7
2023-02-15T14:43:57.679Z
Help Fix Code
v1
null
int delete_user(const char *name, User **user_ptr_del) { User *user_to_be_deleted = find_user(name, *user_ptr_del); // Check existence if (user_to_be_deleted == NULL) { return 1; } // Delete from main user list if first if (*user_ptr_del == user_to_be_deleted) { *user_ptr_del = (*user_ptr_del)->next; return 0; } // Delete from main user list if not first User *curr = *user_ptr_del; while (curr->next != user_to_be_deleted) { curr = curr->next; } curr->next = curr->next->next; return 0; // Delete friends User *friend_of_deleted; for (int i = 0; i < MAX_FRIENDS; i++) { friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); int j = 0; while (strcmp(friend_of_deleted->friends[j]->name, user_to_be_deleted->name) != 0) { j++; } while (j < MAX_FRIENDS - 1) { friend_of_deleted->friends[j] = friend_of_deleted->friends[j + 1]; j++; } friend_of_deleted->friends[j] = NULL; } return 0; }
friend_of_deleted->friends contains the element user_to_be_deleted. This element should be deleted.
null
63c647745f004bf4898cf3e7
2023-02-15T15:56:40.862Z
Question from Code
v1
Are there any memory leaks in this code?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> int check_friends_length(User *checked_user); void add_friends(User *user1, User *user2); int check_friendship(User *user1, User *user2); int remove_from_friends(User *unfriendee, User *unfriender); /* * 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 length if (strlen(name) > MAX_NAME) { return 2; } // Search for given User User *user_to_check = find_user(name, *user_ptr_add); if (user_to_check != NULL) { return 1; } // Create User User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, MAX_NAME); // If list is empty, initialize it if (*user_ptr_add == NULL) { *user_ptr_add = new_user; return 0; } // If list is not empty, add a User to the end of it User *curr = *user_ptr_add; while (curr->next != NULL) { curr = 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) { User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return curr; } curr = curr->next; } return curr; } /* * 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) { while (curr != NULL) { printf("%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) { // Check length if (strlen(filename) > MAX_NAME) { return 2; } FILE *given_file = fopen(filename, "r"); if (given_file == NULL) { return 1; } fclose(given_file); strncpy(user->profile_pic, filename, MAX_NAME); 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); // Check existence if (user1 == NULL || user2 == NULL) { return 4; } // Check equivalence if (strcmp(name1, name2) == 0) { return 3; } // Check friend list lengths if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { return 2; } // Check existing friendship if (check_friendship(user1, user2) == 0) { return 1; } // Add friends add_friends(user1, user2); return 0; } int check_friends_length(User *checked_user) { //HELPER // return number of friends int counter = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (checked_user->friends[i] != NULL) { counter++; } } return counter; } void add_friends(User *user1, User *user2) { // HELPER // PRECONDITION: Users have room in their friend's list int i = 0; while (user1->friends[i] != NULL) { i++; } user1->friends[i] = user2; i = 0; while (user2->friends[i] != NULL) { i++; } user2->friends[i] = user1; } int check_friendship(User *user1, User *user2) { // HELPER // Return 0 if friends, return 1 if not friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == user2) { return 0; } } return 1; } /* * 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) { // Check if User exists if (user == NULL) { return 1; } // Print photo if photo exists FILE *profile_photo = fopen(user->profile_pic, "r"); char c; if (profile_photo) { while ((c = getc(profile_photo)) != EOF) printf("%c", c); printf("\n"); } fclose(profile_photo); // Print details printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; 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("%s\n", curr->contents); curr = curr->next; if (curr != NULL) { printf("\n===\n\n"); } } 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) { // Check if Users exist if (author == NULL || target == NULL) { return 2; } // Check if Users are friends if (check_friendship((User *) author, target) == 1) { return 1; } // Create Post Post *new_post = malloc(sizeof(Post)); strncpy(new_post->author, author->name, MAX_NAME); new_post->contents = contents; // Initialize time created time_t *time_ptr = malloc(sizeof(time_t)); *time_ptr = time(NULL); new_post->date = time_ptr; // // Check if a first post exists if (target->first_post == NULL) { target->first_post = new_post; return 0; } // // Add post to the front if others already exist Post *old_first = target->first_post; new_post->next = old_first; 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 *user_to_be_deleted = find_user(name, *user_ptr_del); // Check existence if (user_to_be_deleted == NULL) { return 1; } // Delete friends User *friend_of_deleted; for (int i = 0; i < MAX_FRIENDS; i++) { if (user_to_be_deleted->friends[i] != NULL) { friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); remove_from_friends(user_to_be_deleted, friend_of_deleted); } } // Delete from main user list if first if (*user_ptr_del == user_to_be_deleted) { *user_ptr_del = (*user_ptr_del)->next; return 0; } // Delete from main user list if not first User *curr = *user_ptr_del; while (curr->next != user_to_be_deleted) { curr = curr->next; } curr->next = curr->next->next; // Free memory from posts sent to this user if (user_to_be_deleted->first_post != NULL) { Post *curr_post = user_to_be_deleted->first_post; Post *next_post = curr_post->next; while (next_post != NULL){ free(curr_post->date); free(curr_post); curr_post = next_post; next_post = next_post->next; } free(curr_post->date); free(curr_post); } free(user_to_be_deleted); return 0; } int remove_from_friends(User *unfriendee, User *unfriender) { // HELPER // Returns 0 when successfully unfriended and returns 1 when not found in friends list for (int i = 0; i < MAX_FRIENDS; i++) { if (unfriender->friends[i] == unfriendee) { unfriender->friends[i] = NULL; return 0; } } return 1; }
null
null
63c647745f004bf4898cf3e7
2023-02-15T16:05:10.781Z
Question from Code
v1
Are there any memory leaks or segmentation faults in this code?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> int check_friends_length(User *checked_user); void add_friends(User *user1, User *user2); int check_friendship(User *user1, User *user2); int remove_from_friends(User *unfriendee, User *unfriender); /* * 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 length if (strlen(name) > MAX_NAME) { return 2; } // Search for given User User *user_to_check = find_user(name, *user_ptr_add); if (user_to_check != NULL) { return 1; } // Create User User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, MAX_NAME); // If list is empty, initialize it if (*user_ptr_add == NULL) { *user_ptr_add = new_user; return 0; } // If list is not empty, add a User to the end of it User *curr = *user_ptr_add; while (curr->next != NULL) { curr = 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) { User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return curr; } curr = curr->next; } return curr; } /* * 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) { while (curr != NULL) { printf("%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) { // Check length if (strlen(filename) > MAX_NAME) { return 2; } FILE *given_file = fopen(filename, "r"); if (given_file == NULL) { return 1; } fclose(given_file); strncpy(user->profile_pic, filename, MAX_NAME); 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); // Check existence if (user1 == NULL || user2 == NULL) { return 4; } // Check equivalence if (strcmp(name1, name2) == 0) { return 3; } // Check friend list lengths if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { return 2; } // Check existing friendship if (check_friendship(user1, user2) == 0) { return 1; } // Add friends add_friends(user1, user2); return 0; } int check_friends_length(User *checked_user) { //HELPER // return number of friends int counter = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (checked_user->friends[i] != NULL) { counter++; } } return counter; } void add_friends(User *user1, User *user2) { // HELPER // PRECONDITION: Users have room in their friend's list int i = 0; while (user1->friends[i] != NULL) { i++; } user1->friends[i] = user2; i = 0; while (user2->friends[i] != NULL) { i++; } user2->friends[i] = user1; } int check_friendship(User *user1, User *user2) { // HELPER // Return 0 if friends, return 1 if not friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == user2) { return 0; } } return 1; } /* * 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) { // Check if User exists if (user == NULL) { return 1; } // Print photo if photo exists FILE *profile_photo = fopen(user->profile_pic, "r"); char c; if (profile_photo) { while ((c = getc(profile_photo)) != EOF) printf("%c", c); printf("\n"); } fclose(profile_photo); // Print details printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; 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("%s\n", curr->contents); curr = curr->next; if (curr != NULL) { printf("\n===\n\n"); } } 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) { // Check if Users exist if (author == NULL || target == NULL) { return 2; } // Check if Users are friends if (check_friendship((User *) author, target) == 1) { return 1; } // Create Post Post *new_post = malloc(sizeof(Post)); strncpy(new_post->author, author->name, MAX_NAME); new_post->contents = contents; // Initialize time created time_t *time_ptr = malloc(sizeof(time_t)); *time_ptr = time(NULL); new_post->date = time_ptr; // // Check if a first post exists if (target->first_post == NULL) { target->first_post = new_post; return 0; } // // Add post to the front if others already exist Post *old_first = target->first_post; new_post->next = old_first; 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 *user_to_be_deleted = find_user(name, *user_ptr_del); // Check existence if (user_to_be_deleted == NULL) { return 1; } // Delete friends User *friend_of_deleted; for (int i = 0; i < MAX_FRIENDS; i++) { if (user_to_be_deleted->friends[i] != NULL) { friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); remove_from_friends(user_to_be_deleted, friend_of_deleted); } } // Delete from main user list if first if (*user_ptr_del == user_to_be_deleted) { *user_ptr_del = (*user_ptr_del)->next; } else { // Delete from main user list if not first User *curr = *user_ptr_del; while (curr->next != user_to_be_deleted) { curr = curr->next; } curr->next = curr->next->next; } // Free memory from posts sent to this user if (user_to_be_deleted->first_post != NULL) { Post *curr_post = user_to_be_deleted->first_post; Post *next_post = curr_post->next; while (next_post != NULL){ free(curr_post->date); free(curr_post->contents); free(curr_post); curr_post = next_post; next_post = next_post->next; } free(curr_post->date); free(curr_post->contents); free(curr_post); } free(user_to_be_deleted); return 0; } int remove_from_friends(User *unfriendee, User *unfriender) { // HELPER // Returns 0 when successfully unfriended and returns 1 when not found in friends list for (int i = 0; i < MAX_FRIENDS; i++) { if (unfriender->friends[i] == unfriendee) { unfriender->friends[i] = NULL; return 0; } } return 1; }
null
null
63c647745f004bf4898cf3e7
2023-02-15T16:10:48.577Z
Question from Code
v1
Are there any segmentation faults? WHich line is it on?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> int check_friends_length(User *checked_user); void add_friends(User *user1, User *user2); int check_friendship(User *user1, User *user2); int remove_from_friends(User *unfriendee, User *unfriender); /* * 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 length if (strlen(name) > MAX_NAME) { return 2; } // Search for given User User *user_to_check = find_user(name, *user_ptr_add); if (user_to_check != NULL) { return 1; } // Create User User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, MAX_NAME); // If list is empty, initialize it if (*user_ptr_add == NULL) { *user_ptr_add = new_user; return 0; } // If list is not empty, add a User to the end of it User *curr = *user_ptr_add; while (curr->next != NULL) { curr = 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) { User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return curr; } curr = curr->next; } return curr; } /* * 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) { while (curr != NULL) { printf("%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) { // Check length if (strlen(filename) > MAX_NAME) { return 2; } FILE *given_file = fopen(filename, "r"); if (given_file == NULL) { return 1; } fclose(given_file); strncpy(user->profile_pic, filename, MAX_NAME); 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); // Check existence if (user1 == NULL || user2 == NULL) { return 4; } // Check equivalence if (strcmp(name1, name2) == 0) { return 3; } // Check friend list lengths if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { return 2; } // Check existing friendship if (check_friendship(user1, user2) == 0) { return 1; } // Add friends add_friends(user1, user2); return 0; } int check_friends_length(User *checked_user) { //HELPER // return number of friends int counter = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (checked_user->friends[i] != NULL) { counter++; } } return counter; } void add_friends(User *user1, User *user2) { // HELPER // PRECONDITION: Users have room in their friend's list int i = 0; while (user1->friends[i] != NULL) { i++; } user1->friends[i] = user2; i = 0; while (user2->friends[i] != NULL) { i++; } user2->friends[i] = user1; } int check_friendship(User *user1, User *user2) { // HELPER // Return 0 if friends, return 1 if not friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == user2) { return 0; } } return 1; } /* * 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) { // Check if User exists if (user == NULL) { return 1; } // Print photo if photo exists FILE *profile_photo = fopen(user->profile_pic, "r"); char c; if (profile_photo) { while ((c = getc(profile_photo)) != EOF) printf("%c", c); printf("\n"); } fclose(profile_photo); // Print details printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; 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("%s\n", curr->contents); curr = curr->next; if (curr != NULL) { printf("\n===\n\n"); } } 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) { // Check if Users exist if (author == NULL || target == NULL) { return 2; } // Check if Users are friends if (check_friendship((User *) author, target) == 1) { return 1; } // Create Post Post *new_post = malloc(sizeof(Post)); strncpy(new_post->author, author->name, MAX_NAME); new_post->contents = contents; // Initialize time created time_t *time_ptr = malloc(sizeof(time_t)); *time_ptr = time(NULL); new_post->date = time_ptr; // // Check if a first post exists if (target->first_post == NULL) { target->first_post = new_post; return 0; } // // Add post to the front if others already exist Post *old_first = target->first_post; new_post->next = old_first; 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 *user_to_be_deleted = find_user(name, *user_ptr_del); // Check existence if (user_to_be_deleted == NULL) { return 1; } // Delete friends User *friend_of_deleted; for (int i = 0; i < MAX_FRIENDS; i++) { if (user_to_be_deleted->friends[i] != NULL) { friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); remove_from_friends(user_to_be_deleted, friend_of_deleted); } } // Delete from main user list if first if (*user_ptr_del == user_to_be_deleted) { *user_ptr_del = (*user_ptr_del)->next; } else { // Delete from main user list if not first User *curr = *user_ptr_del; while (curr->next != user_to_be_deleted) { curr = curr->next; } curr->next = curr->next->next; } // Free memory from posts sent to this user if (user_to_be_deleted->first_post != NULL) { Post *curr_post = user_to_be_deleted->first_post; Post *next_post = curr_post->next; while (next_post != NULL){ free(curr_post->date); free(curr_post->contents); free(curr_post); curr_post = next_post; next_post = next_post->next; } free(curr_post->date); free(curr_post->contents); free(curr_post); } free(user_to_be_deleted); return 0; } void remove_from_friends(User *unfriendee, User *unfriender) { // HELPER // Returns 0 when successfully unfriended and returns 1 when not found in friends list for (int i = 0; i < MAX_FRIENDS; i++) { if (unfriender->friends[i] == unfriendee) { unfriender->friends[i] = NULL; } } }
null
null
63c647745f004bf4898cf3e7
2023-02-15T16:12:53.120Z
Help Fix Code
v1
null
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> int check_friends_length(User *checked_user); void add_friends(User *user1, User *user2); int check_friendship(User *user1, User *user2); int remove_from_friends(User *unfriendee, User *unfriender); /* * 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 length if (strlen(name) > MAX_NAME) { return 2; } // Search for given User User *user_to_check = find_user(name, *user_ptr_add); if (user_to_check != NULL) { return 1; } // Create User User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, MAX_NAME); // If list is empty, initialize it if (*user_ptr_add == NULL) { *user_ptr_add = new_user; return 0; } // If list is not empty, add a User to the end of it User *curr = *user_ptr_add; while (curr->next != NULL) { curr = 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) { User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return curr; } curr = curr->next; } return curr; } /* * 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) { while (curr != NULL) { printf("%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) { // Check length if (strlen(filename) > MAX_NAME) { return 2; } FILE *given_file = fopen(filename, "r"); if (given_file == NULL) { return 1; } fclose(given_file); strncpy(user->profile_pic, filename, MAX_NAME); 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); // Check existence if (user1 == NULL || user2 == NULL) { return 4; } // Check equivalence if (strcmp(name1, name2) == 0) { return 3; } // Check friend list lengths if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { return 2; } // Check existing friendship if (check_friendship(user1, user2) == 0) { return 1; } // Add friends add_friends(user1, user2); return 0; } int check_friends_length(User *checked_user) { //HELPER // return number of friends int counter = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (checked_user->friends[i] != NULL) { counter++; } } return counter; } void add_friends(User *user1, User *user2) { // HELPER // PRECONDITION: Users have room in their friend's list int i = 0; while (user1->friends[i] != NULL) { i++; } user1->friends[i] = user2; i = 0; while (user2->friends[i] != NULL) { i++; } user2->friends[i] = user1; } int check_friendship(User *user1, User *user2) { // HELPER // Return 0 if friends, return 1 if not friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == user2) { return 0; } } return 1; } /* * 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) { // Check if User exists if (user == NULL) { return 1; } // Print photo if photo exists FILE *profile_photo = fopen(user->profile_pic, "r"); char c; if (profile_photo) { while ((c = getc(profile_photo)) != EOF) printf("%c", c); printf("\n"); } fclose(profile_photo); // Print details printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; 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("%s\n", curr->contents); curr = curr->next; if (curr != NULL) { printf("\n===\n\n"); } } 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) { // Check if Users exist if (author == NULL || target == NULL) { return 2; } // Check if Users are friends if (check_friendship((User *) author, target) == 1) { return 1; } // Create Post Post *new_post = malloc(sizeof(Post)); strncpy(new_post->author, author->name, MAX_NAME); new_post->contents = contents; // Initialize time created time_t *time_ptr = malloc(sizeof(time_t)); *time_ptr = time(NULL); new_post->date = time_ptr; // // Check if a first post exists if (target->first_post == NULL) { target->first_post = new_post; return 0; } // // Add post to the front if others already exist Post *old_first = target->first_post; new_post->next = old_first; 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 *user_to_be_deleted = find_user(name, *user_ptr_del); // Check existence if (user_to_be_deleted == NULL) { return 1; } // Delete friends User *friend_of_deleted; for (int i = 0; i < MAX_FRIENDS; i++) { if (user_to_be_deleted->friends[i] != NULL) { friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); remove_from_friends(user_to_be_deleted, friend_of_deleted); } } // Delete from main user list if first if (*user_ptr_del == user_to_be_deleted) { *user_ptr_del = (*user_ptr_del)->next; } else { // Delete from main user list if not first User *curr = *user_ptr_del; while (curr->next != user_to_be_deleted) { curr = curr->next; } curr->next = curr->next->next; } // Free memory from posts sent to this user if (user_to_be_deleted->first_post != NULL) { Post *curr_post = user_to_be_deleted->first_post; Post *next_post = curr_post->next; while (next_post != NULL){ free(curr_post->date); free(curr_post->contents); free(curr_post); curr_post = next_post; next_post = next_post->next; } free(curr_post->date); free(curr_post->contents); free(curr_post); } free(user_to_be_deleted); return 0; } void remove_from_friends(User *unfriendee, User *unfriender) { // HELPER // Returns 0 when successfully unfriended and returns 1 when not found in friends list for (int i = 0; i < MAX_FRIENDS; i++) { if (unfriender->friends[i] == unfriendee) { unfriender->friends[i] = NULL; } } }
Can you fix the segmentation fault?
null
63c647745f004bf4898cf3e7
2023-02-15T16:15:52.321Z
Question from Code
v1
Where is the segmentation fault exactly? I cannot find it nor how to fix it.
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> int check_friends_length(User *checked_user); void add_friends(User *user1, User *user2); int check_friendship(User *user1, User *user2); int remove_from_friends(User *unfriendee, User *unfriender); /* * 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 length if (strlen(name) > MAX_NAME) { return 2; } // Search for given User User *user_to_check = find_user(name, *user_ptr_add); if (user_to_check != NULL) { return 1; } // Create User User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, MAX_NAME); // If list is empty, initialize it if (*user_ptr_add == NULL) { *user_ptr_add = new_user; return 0; } // If list is not empty, add a User to the end of it User *curr = *user_ptr_add; while (curr->next != NULL) { curr = 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) { User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return curr; } curr = curr->next; } return curr; } /* * 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) { while (curr != NULL) { printf("%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) { // Check length if (strlen(filename) > MAX_NAME) { return 2; } FILE *given_file = fopen(filename, "r"); if (given_file == NULL) { return 1; } fclose(given_file); strncpy(user->profile_pic, filename, MAX_NAME); 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); // Check existence if (user1 == NULL || user2 == NULL) { return 4; } // Check equivalence if (strcmp(name1, name2) == 0) { return 3; } // Check friend list lengths if (check_friends_length(user1) == MAX_FRIENDS || check_friends_length(user2) == MAX_FRIENDS) { return 2; } // Check existing friendship if (check_friendship(user1, user2) == 0) { return 1; } // Add friends add_friends(user1, user2); return 0; } int check_friends_length(User *checked_user) { //HELPER // return number of friends int counter = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (checked_user->friends[i] != NULL) { counter++; } } return counter; } void add_friends(User *user1, User *user2) { // HELPER // PRECONDITION: Users have room in their friend's list int i = 0; while (user1->friends[i] != NULL) { i++; } user1->friends[i] = user2; i = 0; while (user2->friends[i] != NULL) { i++; } user2->friends[i] = user1; } int check_friendship(User *user1, User *user2) { // HELPER // Return 0 if friends, return 1 if not friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == user2) { return 0; } } return 1; } /* * 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) { // Check if User exists if (user == NULL) { return 1; } // Print photo if photo exists FILE *profile_photo = fopen(user->profile_pic, "r"); char c; if (profile_photo) { while ((c = getc(profile_photo)) != EOF) printf("%c", c); printf("\n"); } fclose(profile_photo); // Print details printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; 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("%s\n", curr->contents); curr = curr->next; if (curr != NULL) { printf("\n===\n\n"); } } 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) { // Check if Users exist if (author == NULL || target == NULL) { return 2; } // Check if Users are friends if (check_friendship((User *) author, target) == 1) { return 1; } // Create Post Post *new_post = malloc(sizeof(Post)); strncpy(new_post->author, author->name, MAX_NAME); new_post->contents = contents; // Initialize time created time_t *time_ptr = malloc(sizeof(time_t)); *time_ptr = time(NULL); new_post->date = time_ptr; // // Check if a first post exists if (target->first_post == NULL) { target->first_post = new_post; return 0; } // // Add post to the front if others already exist Post *old_first = target->first_post; new_post->next = old_first; 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 *user_to_be_deleted = find_user(name, *user_ptr_del); // Check existence if (user_to_be_deleted == NULL) { return 1; } // Delete friends User *friend_of_deleted; for (int i = 0; i < MAX_FRIENDS; i++) { if (user_to_be_deleted->friends[i] != NULL) { friend_of_deleted = find_user(user_to_be_deleted->friends[i]->name, *user_ptr_del); remove_from_friends(user_to_be_deleted, friend_of_deleted); } } // Delete from main user list if first if (*user_ptr_del == user_to_be_deleted) { *user_ptr_del = (*user_ptr_del)->next; } else { // Delete from main user list if not first User *curr = *user_ptr_del; while (curr->next != user_to_be_deleted) { curr = curr->next; } curr->next = curr->next->next; } // Free memory from posts sent to this user if (user_to_be_deleted->first_post != NULL) { Post *curr_post = user_to_be_deleted->first_post; Post *next_post = curr_post->next; while (next_post != NULL){ free(curr_post->date); free(curr_post->contents); free(curr_post); curr_post = next_post; next_post = next_post->next; } free(curr_post->date); free(curr_post->contents); free(curr_post); } free(user_to_be_deleted); return 0; } void remove_from_friends(User *unfriendee, User *unfriender) { // HELPER // Returns 0 when successfully unfriended and returns 1 when not found in friends list for (int i = 0; i < MAX_FRIENDS; i++) { if (unfriender->friends[i] == unfriendee) { unfriender->friends[i] = NULL; } } }
null
null
63c647745f004bf4898cf3e7
2023-03-15T18:55:00.721Z
Help Write Code
v2
null
null
null
allocate memory to a node and then set it's value to "hello"
63c647745f004bf4898cf3e7
2023-03-15T19:24:26.940Z
Help Fix Code
v2
parse_file should create rule_node and/or action_node from the given file fp. fp will have the following format: main : linked_list.o main.o gcc -Wall -g -std=gnu99 -o main linked_list.o main.o main.o : main.c linked_list.h gcc -Wall -g -std=gnu99 -c main.c linked_list.o : linked_list.c linked_list.h gcc -Wall -g -std=gnu99 -c linked_list.c
#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; 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; } /* 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. // Create the rules linked_list Rule rules = malloc(sizeof(Rule)); Rule *cur_rule = &rules; // Read each line in the file char line[MAXLINE + 1]; while (fgets(line, MAXLINE + 1, fp) != NULL) { if (is_comment_or_empty(line) == 1) { continue; } if( myString[0] == ' ' || myString[0] == '\t' ){ // Create action_node // Insert node to the end of rules } else { // create rule_node // Insert node to the end of rules } } }
null
null
63c647745f004bf4898cf3e7
2023-03-15T19:59:40.354Z
Help Fix Code
v2
count the amount of words in string
#include <stdio.h> #include <string.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> int count_words(char *sentence) { int count = 0; char* token = strtok(sentence, " "); while (token) { token++; token = strtok(NULL, " "); } return count; } int main() { int n; n = count_words("gcc -Wall -g -std=gnu99 -o main linked_list.o main.o"); printf("%d\n", n); return 0; }
null
null
63c647745f004bf4898cf3e7
2023-03-15T20:00:42.237Z
Help Fix Code
v2
count the amount of words in string
#include <stdio.h> #include <string.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> int count_words(char *sentence) { int count = 0; char* token = strtok(sentence, " "); while (token) { count++; token = strtok(NULL, " "); } return count; } int main() { int n; n = count_words("gcc -Wall -g -std=gnu99 -o main linked_list.o main.o"); printf("%d\n", n); return 0; }
null
null
63c647745f004bf4898cf3e7
2023-03-15T20:03:33.345Z
Help Write Code
v2
null
null
null
function to count the number of words in a string
63c647745f004bf4898cf3e7
2023-03-15T20:18:56.578Z
Question from Code
v2
why does this return a bus error
#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int count_words(char *sentence) { int count = 0; char *token = strtok(sentence, " "); while (token) { count++; printf("token: %s\n", token); token = strtok(NULL, " "); } return count; } int main() { char *sentence = "main.o : main.c linked_list.h "; int n = count_words(sentence); printf("%d\n", n); return 0; }
null
null
63c647785f004bf4898cf400
2023-02-10T22:53:28.203Z
Help Fix Code
v1
null
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel** pixel_array = malloc(height * sizeof(struct pixel*)); struct pixel* first_pixel_row = malloc(width * sizeof(struct pixel)); for (int k = 0; k < width; k+= 3) { struct pixel* pixie = malloc(3); fseek(image, pixel_array_offset + k, SEEK_SET); fread(&pixie->blue, 1, 1, image); fseek(image, pixel_array_offset + k + 1, SEEK_SET); fread(&pixie->green, 1, 1, image); fseek(image, pixel_array_offset + k + 2, SEEK_SET); fread(&pixie->red, 1, 1, image); first_pixel_row[k] = *pixie; } pixel_array[0] = first_pixel_row; for (int i = 1; i < height; i++) { struct pixel* pixel_row = malloc(width * sizeof(struct pixel)); for (int j = 0; j < width; i+= 3) { struct pixel* pixie = malloc(3); fseek(image, i * pixel_array_offset + j, SEEK_SET); fread(&pixie->blue, 1, 1, image); fseek(image, i * pixel_array_offset + j + 1, SEEK_SET); fread(&pixie->green, 1, 1, image); fseek(image, i * pixel_array_offset + j + 2, SEEK_SET); fread(&pixie->red, 1, 1, image); pixel_row[j] = *pixie; } pixel_array[i] = pixel_row; } return pixel_array; }
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. * 4. Return the address of the first struct pixel * you initialized.
null
63c647785f004bf4898cf400
2023-02-10T23:18:17.472Z
Help Fix Code
v1
null
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel** pixel_array = malloc(height * sizeof(struct pixel*)); for (int i = 0; i < height; i++) { struct pixel* pixie_row = malloc(width * sizeof(struct pixel)); for (int j = 0; j + 3 < width; j+= 3) { struct pixel* pixie = malloc(3); fseek(image, pixel_array_offset + j + (width * i), SEEK_SET); fread(&pixie->blue, 1, 1, image); fseek(image, pixel_array_offset + j + (width * i) + 1, SEEK_SET); fread(&pixie->green, 1, 1, image); fseek(image, pixel_array_offset + j + (width * i) + 2, SEEK_SET); fread(&pixie->red, 1, 1, image); pixie_row[j] = *pixie; // printf("row %d, column %d\n", i, j); } pixel_array[i] = pixie_row; } return pixel_array; }
Each group of 3 bytes represents a single pixel, where the bytes store the blue, green, and red colour values of the pixel, in that order. Pixels are grouped by row. 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. 4. Return the address of the first struct pixel * you initialized.
null
63c647785f004bf4898cf400
2023-02-10T23:20:18.682Z
Help Fix Code
v1
null
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel** pixel_array = malloc(height * sizeof(struct pixel*)); for (int i = 0; i < height; i++) { struct pixel* pixie_row = malloc(width * sizeof(struct pixel)); for (int j = 0; j + 4 < width; j+= 4) { struct pixel* pixie = malloc(3); fseek(image, pixel_array_offset + j + (width * i), SEEK_SET); fread(&pixie->blue, 1, 1, image); fseek(image, pixel_array_offset + j + (width * i) + 1, SEEK_SET); fread(&pixie->green, 1, 1, image); fseek(image, pixel_array_offset + j + (width * i) + 2, SEEK_SET); fread(&pixie->red, 1, 1, image); pixie_row[j] = *pixie; // printf("row %d, column %d\n", i, j); } pixel_array[i] = pixie_row; } return &pixel_array[0]; }
Each group of 3 bytes represents a single pixel, where the bytes store the blue, green, and red colour values of the pixel, in that order. Pixels are grouped by row. 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. 4. Return the address of the first struct pixel * you initialized.
null
63c647785f004bf4898cf400
2023-03-10T23:22:37.737Z
Explain Code
v2
null
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.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]; if (fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if (fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if (strlen(password) > 9) { perror("password too long"); exit(1); } if (strlen(user_id) > 9) { perror("user id too long"); exit(1); } int fd[2]; if (pipe(fd) == -1) { perror("pipe"); exit(1); } int r = fork(); if (r == 0) { close(fd[1]); if (dup2(fd[0], fileno(stdin)) == -1) { perror("dup2"); exit(1); } execl("./validate", "validate", NULL); } else if (r > 0) { close(fd[0]); write(fd[1], user_id, strlen(user_id)); write(fd[1], password, strlen(password)); int status; while (wait(&status) > 0) ; if (WIFEXITED(status)) { if (WEXITSTATUS(status) == 1) { perror("validate"); exit(1); } else if (WEXITSTATUS(status) == 0) { printf("%s\n", SUCCESS); } else if (WEXITSTATUS(status) == 2) { printf("%s\n", INVALID); } else if (WEXITSTATUS(status) == 3) { printf("%s\n", NO_USER); } } else { perror("validate"); exit(1); } } else { perror("fork"); exit(1); } return 0; }
null
null
63c647785f004bf4898cf400
2023-03-10T23:23:50.108Z
Help Fix Code
v2
return exit status of child
#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); } if (strlen(password) > 9) { perror("password too long"); exit(1); } if (strlen(user_id) > 9) { perror("user id too long"); exit(1); } int fd[2]; if (pipe(fd) == -1) { perror("pipe"); exit(1); } int r = fork(); if (r == 0) { close(fd[1]); if (dup2(fd[0], fileno(stdin)) == -1) { perror("dup2"); exit(1); } execl("./validate", "validate", NULL); } else if (r > 0) { close(fd[0]); write(fd[1], user_id, strlen(user_id)); write(fd[1], password, strlen(password)); int status; while (wait(&status) > 0); if (WIFEXITED(status)) { if (WEXITSTATUS(status) == 1) { perror("validate"); exit(1); } else if (WEXITSTATUS(status) == 0) { printf("%s\n", SUCCESS); } else if (WEXITSTATUS(status) == 2) { printf("%s\n", INVALID); } else if (WEXITSTATUS(status) == 3) { printf("%s\n", NO_USER); } } else { perror("validate"); exit(1); } } else { perror("fork"); exit(1); } return 0; }
null
null
63c647795f004bf4898cf40a
2023-02-07T19:35:03.403Z
General Question
v1
how are pointers used with structs?
null
null
null
63c647795f004bf4898cf40a
2023-02-07T19:35:52.449Z
General Question
v1
could you explain what printf does?
null
null
null
63c647795f004bf4898cf40a
2023-02-07T19:36:45.488Z
Explain Code
v1
null
for (int i = 0; i < 5; i++){ printf(i); }
null
null
63c6477a5f004bf4898cf414
2023-01-30T15:59:08.737Z
General Question
v1
can you declare two variables of the same type on one line?
null
null
null
63c6477a5f004bf4898cf414
2023-02-06T16:04:19.712Z
General Question
v1
how do you find current time using time and ctime functions?
null
null
null
63c6477a5f004bf4898cf414
2023-02-07T14:19:25.003Z
General Question
v1
what does fseek return?
null
null
null
63c6477a5f004bf4898cf414
2023-02-12T14:25:22.391Z
General Question
v1
how would you free nodes in a linked list?
null
null
null
63c6477a5f004bf4898cf414
2023-02-20T15:47:53.422Z
General Question
v1
How do you split a string on spaces?
null
null
null
63c6477a5f004bf4898cf414
2023-02-20T19:42:42.309Z
General Question
v1
Can you write me a function that checks whether a string has any non-space characters?
null
null
null
63c6477a5f004bf4898cf414
2023-03-06T15:50:30.862Z
General Question
v1
how do I calculate how many space separated words make up a line of a file?
null
null
null
63c6477a5f004bf4898cf414
2023-03-10T01:33:37.751Z
General Question
v2
what is the return type of stat? and how do i call it to determine the last time a file was modified?
null
null
null
63c6477a5f004bf4898cf414
2023-03-14T13:28:50.295Z
General Question
v2
how to generate a random number from 0-99 inclusive using random function?
null
null
null
63c6477a5f004bf4898cf414
2023-03-14T13:32:41.204Z
Explain Code
v2
null
for (;;) { ; }
null
null
63c6477a5f004bf4898cf414
2023-03-14T14:09:12.613Z
General Question
v2
how to set an alarm using setitimer and ITIMER_PROF?
null
null
null
63c6477a5f004bf4898cf414
2023-03-14T14:20:56.315Z
Explain Code
v2
null
struct itimerval new_time, old_time; new_time.it_interval.tv_usec = 0; new_time.it_interval.tv_sec = 5; new_time.it_value.tv_usec = 0; new_time.it_value.tv_sec = seconds; setitimer(ITIMER_PROF, &new_time, &old_time);
null
null
63c6477a5f004bf4898cf414
2023-03-14T14:22:55.749Z
Explain Code
v2
null
struct itimerval new_time, old_time; new_time.it_interval.tv_usec = 0; new_time.it_interval.tv_sec = 0; new_time.it_value.tv_usec = 0; new_time.it_value.tv_sec = seconds; setitimer(ITIMER_PROF, &new_time, &old_time);
null
null
63c6477a5f004bf4898cf414
2023-03-14T14:25:49.634Z
General Question
v2
how do you make an alarm that will send the SIGPROF signal to a program while it's executing
null
null
null
63c6477a5f004bf4898cf414
2023-03-14T14:28:09.580Z
Help Fix Code
v2
I need this code to exit(0) when the timer sends a SIGPROF to the process
/* The purpose of this program is to practice writing signal handling * functions and observing the behaviour of signals. */ #include <stdio.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> /* Message to print in the signal handling function. */ #define MESSAGE "%ld reads were done in %ld seconds.\n" /* Global variables to store number of read operations and seconds elapsed. */ long num_reads, seconds; // Signal handler function. void handler(int code) { fprintf(stdout, MESSAGE, num_reads, seconds); exit(0); } /* The first command-line argument is the number of seconds to set a timer to run. * The second argument is the name of a binary file containing 100 ints. * Assume both of these arguments are correct. */ int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: time_reads s filename\n"); exit(1); } seconds = strtol(argv[1], NULL, 10); FILE *fp; if ((fp = fopen(argv[2], "r")) == NULL) { perror("fopen"); exit(1); } struct sigaction newact; newact.sa_handler = handler; newact.sa_flags = 0; sigemptyset(&newact.sa_mask); sigaction(SIGPROF, &newact, NULL); struct itimerval new_time, old_time; new_time.it_interval.tv_usec = 0; new_time.it_interval.tv_sec = 0; new_time.it_value.tv_usec = 0; new_time.it_value.tv_sec = seconds; setitimer(ITIMER_PROF, &new_time, &old_time); /* In an infinite loop, read an int from a random location in the file, * and print it to stderr. */ int num; for (;;) { fseek(fp, random() % 100, SEEK_SET); fread(&num, sizeof(int), 1, fp); num_reads++; //Increment num_reads fprintf(stderr, "%d", num); } return 1; // something is wrong if we ever get here! }
null
null
63c6477c5f004bf4898cf428
2023-01-24T20:41:32.819Z
General Question
v1
what is char**
null
null
null
63c6477c5f004bf4898cf428
2023-01-24T20:42:08.099Z
General Question
v1
how to dereference char**?
null
null
null
63c6477e5f004bf4898cf432
2023-01-17T18:48:35.924Z
Explain Code
v1
null
print("Hello World")
null
null
63c6477e5f004bf4898cf432
2023-01-17T20:34:36.144Z
Explain Code
v1
null
int main() { int i = 2; int j = 30; int a[4]; int *p; int *q; p = &i; j = *p; *p = 1; a[0] = 10; a[3] = 12; a[i] = 11; return 0; }
null
null
63c6477f5f004bf4898cf441
2023-01-17T20:14:43.308Z
Question from Code
v1
what will be the value of pA after execution?
int A[4] = {5, 10, 15, 20}; int *pA = A[0]; pA += 1;
null
null
63c6477f5f004bf4898cf441
2023-01-17T20:16:01.166Z
Question from Code
v1
what will be the value of pA after execution? why?
int A[4] = {5, 10, 15, 20}; int *pA = A[0]; pA += 1;
null
null
63c6477f5f004bf4898cf441
2023-01-17T20:19:18.954Z
General Question
v1
given an array of four random integers called arr, and a pointer to an integer called point, assign point to be the address of the last element in arr
null
null
null
63c6477f5f004bf4898cf441
2023-01-17T20:21:20.406Z
General Question
v1
what are strings in C?
null
null
null
63c6477f5f004bf4898cf441
2023-01-17T20:22:23.255Z
General Question
v1
give an example of a linked list in C using structs
null
null
null
63c6477f5f004bf4898cf441
2023-01-17T20:25:41.876Z
Help Write Code
v1
null
null
null
make a linked list using structs
63c6477f5f004bf4898cf441
2023-01-21T05:23:59.489Z
General Question
v1
in C, does scanf read until the newline?
null
null
null
63c6477f5f004bf4898cf441
2023-01-21T06:13:37.805Z
General Question
v1
read past the first newline of scanf
null
null
null
63c6477f5f004bf4898cf441
2023-01-21T06:14:29.068Z
General Question
v1
generate a detailed documentation of `scanf("%*c%d", &number)` with usage examples and explanations
null
null
null
63c6477f5f004bf4898cf441
2023-01-21T06:42:07.726Z
General Question
v1
skip the first line of scanf
null
null
null