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
63c649005f004bf4898d01b6
2023-03-29T05:54:51.475Z
General Question
v2
explain the main idea of lab 11
null
null
null
63c649005f004bf4898d01b6
2023-03-29T05:55:16.989Z
General Question
v2
what does the read_from function do in lab 11
null
null
null
63c649005f004bf4898d01b6
2023-03-29T05:55:58.479Z
General Question
v2
what are sockets
null
null
null
63c649005f004bf4898d01b6
2023-03-29T05:56:44.559Z
General Question
v2
what are file descriptors
null
null
null
63c649005f004bf4898d01b6
2023-04-05T02:31:15.590Z
Help Fix Code
v2
Loop through user linked list twice. Put all names of users in a buffer string called buf.
char *list_users(const User *head) { // First loop: add up all string lengths int str_len_sum = 0; int name_count = 0; const User *curr = head; while (curr != NULL) { str_len_sum += strlen(curr->name); name_count++; curr = curr->next; } // malloc buf: name_count * 2 means network newline after each name, 1 at end for \0 char *buf = (char *)malloc(sizeof(char) * (str_len_sum + name_count * 2 + 1)); if (buf == NULL) { perror("Error: failed to malloc buf"); exit(1); } // Second loop: copy strings into buffer of sufficient size curr = head; // reset curr to head while (curr != NULL) { // append name of user to buf int snprintf_result = snprintf(buf, INPUT_BUFFER_SIZE, "%s\r\n", curr->name); if (snprintf_result < 0) { perror("Error: snprintf failed"); } curr = curr->next; } //snprintf(buf, 1, "\0"); // add null terminator to the last index of the buffer return buf; }
null
null
63c649005f004bf4898d01b6
2023-04-06T04:22:32.473Z
Help Fix Code
v2
This is a server that asks for the client's username.
struct sockaddr_in *self = init_server_addr(PORT); int listenfd = set_up_server_socket(self, 5); while (1) { int fd = accept_connection(listenfd); if (fd < 0) { continue; } // Receive messages char buf[INPUT_BUFFER_SIZE] = {'\0'}; int inbuf = 0; // How many bytes currently in buffer? int room = sizeof(buf); // How many bytes remaining in buffer? char *after = buf; // Pointer to position after the data in buf User *user_list = NULL; // Create the heads of the empty data structure char input[INPUT_BUFFER_SIZE]; int received_username_flag = 0; printf("What is your user name?\n"); // get username int nbytes; while ((nbytes = read(fd, after, room)) > 0) { // Step 1: update inbuf (how many bytes were just added?) inbuf += nbytes; int where; // Step 2 determine if a full line has been read from the client while ((where = find_network_newline(buf, inbuf)) > 0) { // Step 3 get full input if (where >= 2 && inbuf > 0) { buf[where - 2] = '\0'; } if (received_username_flag == 0) // get username in first input { // search for username in user_list int create_user_output = create_user(buf, &user_list); if (create_user_output == 1) { // user already exists printf("Welcome back.\n"); printf("Go ahead and enter user commands>"); } else if (create_user_output == 2) { // if the given name cannot fit in the 'name' array perror("Error: name too long"); exit(1); } else { // new user printf("Welcome.\n"); printf("Go ahead and enter user commands>"); } received_username_flag++; } // Step 4 update inbuf and remove the full line from the buffer inbuf -= where; memmove(buf, &(buf[where]), inbuf); } // Step 5: update after and room, in preparation for the next read. after = &(buf[inbuf]); room = sizeof(buf) - inbuf; // inbuf + room = sizeof(buf) } }
null
null
63c649005f004bf4898d01b6
2023-04-06T05:00:40.060Z
General Question
v2
how to i test a server using netcat
null
null
null
63c649005f004bf4898d01b6
2023-04-22T08:32:18.487Z
Explain Code
v2
null
for i in* do if[-d $i]; then export PATH = $PATH : $(pwd) / $i fi done
null
null
63c649005f004bf4898d01b6
2023-04-22T08:33:35.144Z
General Question
v2
what is a PATH variable
null
null
null
63c649005f004bf4898d01b6
2023-04-22T08:40:19.113Z
Explain Code
v2
null
int y = x | 1
null
null
63c649005f004bf4898d01b6
2023-04-22T22:18:16.492Z
General Question
v2
is char **p a pointer to a string?
null
null
null
63c649005f004bf4898d01b6
2023-04-23T03:31:25.752Z
Help Fix Code
v2
allocate memory for a string
char **s = malloc(sizeof(char *))
null
null
63c649005f004bf4898d01b6
2023-04-23T04:06:44.670Z
Explain Code
v2
null
char n = 25;
null
null
63c649015f004bf4898d01bb
2023-01-28T23:02:38.070Z
General Question
v1
how to scan until end of line using scanf
null
null
null
63c649015f004bf4898d01bb
2023-01-28T23:06:18.073Z
Question from Code
v1
is this valid?
scanf("%*s %*d");
null
null
63c649015f004bf4898d01bb
2023-01-28T23:07:11.639Z
General Question
v1
why did I get "abort" message when compiling my code
null
null
null
63c649015f004bf4898d01bb
2023-01-28T23:09:20.853Z
Question from Code
v1
what is the bug in my code when I compile it with standard input "total 329 -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 -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c"?
#include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. int check_permissions(char* min_permissions, char* file_permissions) { // parameters: user-specified permissions, actual file permissions --> compare them and return 1 or 0 // check for the 9 char string s -rwxrwxrwx // s[0], s[1], s[2] user permissions // s[3], s[4], s[5] group permissions // s[6], s[7], s[8] others permissions for (int i = 0; i < 9; i++) { if (min_permissions[i] != '-') { //only assess this file if it is a file and not a directory if (file_permissions[i] != min_permissions[i]) { return 1; //return 1 if the permissions are not enough } } } return 0; //return 0 if the permissions are enough } int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } // TODO: Process command line arguments. int min_size = strtol(argv[1], NULL, 10); // min file size provided by the 1st mandatory command line arg int count = 0; //keep track of the number of files that match to return char permissions[10]; // store permissions int size; // store file size scanf("%*s %*d"); while (scanf("%s %*s %*s %*s %d %*s %*s %*s %*s %*[^\n]", permissions, &size) != EOF) { // both command line args provided, check both file sizes and file permissions if (argc == 3) { if ((size > min_size) && (check_permissions(argv[2], &(permissions[1])) == 0)) { count++; // add this file to counter } } // only 1 command line arg provided, check only the file sizes & compare to min file size else { if (size > min_size) { count++; // add this file to counter } } } printf("%d", count); // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct value. return 0; }
null
null
63c649015f004bf4898d01bb
2023-01-28T23:12:12.330Z
Question from Code
v1
Why do I get "abort" error message when I run this program with command line argument "1" and standard input "total 329 -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 -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c"?
#include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. int check_permissions(char* min_permissions, char* file_permissions) { // parameters: user-specified permissions, actual file permissions --> compare them and return 1 or 0 // check for the 9 char string s -rwxrwxrwx // s[0], s[1], s[2] user permissions // s[3], s[4], s[5] group permissions // s[6], s[7], s[8] others permissions for (int i = 0; i < 9; i++) { if (min_permissions[i] != '-') { //only assess this file if it is a file and not a directory if (file_permissions[i] != min_permissions[i]) { return 1; //return 1 if the permissions are not enough } } } return 0; //return 0 if the permissions are enough } int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } // TODO: Process command line arguments. int min_size = strtol(argv[1], NULL, 10); // min file size provided by the 1st mandatory command line arg int count = 0; //keep track of the number of files that match to return char permissions[10]; // store permissions int size; // store file size scanf("%*s %*d"); while (scanf("%s %*s %*s %*s %d %*s %*s %*s %*s %*[^\n]", permissions, &size) != EOF) { // both command line args provided, check both file sizes and file permissions if (argc == 3) { if ((size > min_size) && (check_permissions(argv[2], &(permissions[1])) == 0)) { count++; // add this file to counter } } // only 1 command line arg provided, check only the file sizes & compare to min file size else { if (size > min_size) { count++; // add this file to counter } } } printf("%d", count); // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct value. return 0; }
null
null
63c649015f004bf4898d01bb
2023-01-28T23:22:12.392Z
General Question
v1
what does this error mean: "zsh: abort ./count_large 1 < handout.test" when I execute my program in the command line?
null
null
null
63c649015f004bf4898d01bb
2023-02-12T20:18:16.485Z
Question from Code
v1
why am I getting a segmentation fault when i run this?
void list_users(const User *curr) { if (curr != NULL) { User *c = (User *) curr; while (c != NULL) { printf("%s", c->name); curr = curr->next; } } }
null
null
63c649015f004bf4898d01bb
2023-02-12T20:21:03.665Z
General Question
v1
Why can't I cast a const pointer to a pointer and then dereference it?
null
null
null
63c649015f004bf4898d01bb
2023-02-12T21:30:21.464Z
Question from Code
v1
What are the bugs in my code that causes errors?
int make_friends(const char *name1, const char *name2, User *head) { // find both users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); // at least one of the users does not exist, return 4 if (user1 == NULL || user2 == NULL) { return 4; } // same user passed in twice, return 3 if (strcmp(name1, name2) == 0) { return 3; } int l1 = num_friends(user1); // number of friends in user1's friend list int l2 = num_friends(user2); // number of friends in user2's friend list // at least one user has MAX_FRIENDS friends, return 2 if (l1 == 10 || l2 == 10) { return 2; } // users are already friends, return 1 if (index_in_friends(user1, user2) != -1 && index_in_friends(user2, user1) != -1) { return 1; } (user1->friends)[l1] = user2; // add user2 to user1's friends (user2->friends)[l2] = user1; // add user1 to user2's friends // successfully made user1 and user2 friends return 0; } int num_friends(const User *user) { // loop through list of friends of user - list of friends stored as all NULL values at the end for (int i = 0; i < 10; i++) { if ((user->friends)[i] == NULL) { // first NULL element indicates number of friends return i; } } return 10; } int index_in_friends(const User *user1, const User *user2) { for (int i = 0; i < 10; i++) { if ((user1->friends)[i] == user2) { return i; } } return -1; }
null
null
63c649015f004bf4898d01bb
2023-03-10T20:28:24.027Z
General Question
v2
How can I create a new process to run a different executable that takes from stdin?
null
null
null
63c649015f004bf4898d01bb
2023-03-10T20:44:33.427Z
General Question
v2
How can I create a new process to run a different executable with input from stdin?
null
null
null
63c649015f004bf4898d01bb
2023-03-10T21:06:22.535Z
General Question
v2
How do I use dup2 in C to duplicate the file descriptor of the read end of a pipe to stdin?
null
null
null
63c649015f004bf4898d01bb
2023-03-16T00:06:26.475Z
Question from Code
v2
what is wrong with my code? what does it do?
#include <stdio.h> #include <stdlib.h> int main() { char **args = malloc(sizeof(char **) * 5); char *c1 = malloc(sizeof(char) * 4); c1[0] = 'a'; c1[1] = 'b'; c1[2] = 'c'; c1[3] = 'd'; args[0] = c1; return 0; }
null
null
63c649025f004bf4898d01c5
2023-01-24T17:04:14.822Z
Question from Code
v1
what does this one do?
int check_permissions(char *file, char *required){ int result = 1; int i; for (i = 0; i < 9 ; i++){ if (file[i] != required[i] && required[i] != '-'){ result = 0; } } return result; }
null
null
63c649025f004bf4898d01c5
2023-01-24T17:07:03.108Z
Help Fix Code
v1
null
int check_permissions(char *file, char *required){ int result = 1; int i; for (i = 0; i < 9 ; i++){ if (file[i] != required[i] && required[i] != '-'){ result = 0; } } return result; }
check whether two string match with some tolerance
null
63c649025f004bf4898d01c5
2023-01-24T17:33:00.168Z
General Question
v1
how to use scanf() to read a whole line
null
null
null
63c649025f004bf4898d01c5
2023-01-24T19:52:38.599Z
Question from Code
v1
how to solve segmentation fault
#include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. int check_permissions(char *file, char *required){ int result = 1; int i; for (i = 0; i < 9 ; i++){ if (file[i] != required[i] && required[i] != '-'){ result = 0; } } return result; } int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } int count = 0; // TODO: Process command line arguments. long size = strtol(argv[1], NULL, 10); // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct value. char file_permission[30]; int file_size; scanf("%[^\n]s", file_permission); while (scanf("%s %*d %*s %*s %d %*[^\n]", file_permission, &file_size) == 2){ printf("file permissions is %s\n", file_permission); printf("file size is %d\n", file_size); if (argc == 2 && file_size > size && file_permission[0] != 'd' ){ count++ ; printf("%s", "first if finished"); } else { char *required = &argv[2][0]; if (file_size > size && check_permissions(&file_permission[1], required) && *file_permission != 'd'){ count++; } } } printf("%d\n", count); return 0; }
null
null
63c649025f004bf4898d01c5
2023-01-25T16:10:04.518Z
Explain Code
v1
null
void fib(int **fib_sequence ,int count){ *fib_sequence = malloc(sizeof(int) * count); if (count == 1){ *fib_sequence[0] = 0; } else { *fib_sequence[0] = 0; *fib_sequence[1] = 1; int i; for (i = 2; i < count; i++){ *fib_sequence[i] = *fib_sequence[i - 1] + *fib_sequence[i -2]; } } }
null
null
63c649025f004bf4898d01c5
2023-01-25T16:17:54.436Z
Help Fix Code
v1
null
void fib(int **fib_sequence ,int count){ *fib_sequence = malloc(sizeof(int) * count); if (count == 1){ *fib_sequence[0] = 0; } else { *fib_sequence[0] = 0; *fib_sequence[1] = 1; int i; for (i = 2; i < count; i++){ *fib_sequence[i] = *fib_sequence[i - 1] + *fib_sequence[i -2]; } } void fib(int **fib_sequence ,int count){ *fib_sequence = malloc(sizeof(int) * count); if (count == 1){ *fib_sequence[0] = 0; } else { *fib_sequence[0] = 0; *fib_sequence[1] = 1; int i; for (i = 2; i < count; i++){ *fib_sequence[i] = *fib_sequence[i - 1] + *fib_sequence[i -2]; } } } int main() { /* do not change this main function */ int count = 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; }
return a array of 10 integers
null
63c649025f004bf4898d01c5
2023-01-25T16:24:48.659Z
Help Fix Code
v1
null
void fib(int **fib_sequence ,int count){ *fib_sequence = malloc(sizeof(int) * count); if (count == 1){ *fib_sequence[0] = 0; } else { *fib_sequence[0] = 0; *fib_sequence[1] = 1; int i; for (i = 2; i < count; i++){ *fib_sequence[i] = *fib_sequence[i - 1] + *fib_sequence[i -2]; } } }
create an array of length count
null
63c649025f004bf4898d01c5
2023-01-25T17:26:09.279Z
Explain Code
v1
null
int **split_array(const int *s, int length) { int i; int **result = malloc(sizeof(int*)*2); if (length % 2 !=0){ int *even = malloc(sizeof(int)*(length/2 + 1)); int *odd = malloc(sizeof(int)*(length/2)); } else { int *even = malloc(sizeof(int)*(length/2)); int *odd = malloc(sizeof(int)*(length/2)); } for (i = 0; i < length; i++){ if (i%2 == 0){ even[i/2] = s[i]; } else{ odd[i/2] = s[i]; } } *result[0] = even; *result[1] = odd; }
null
null
63c649025f004bf4898d01c5
2023-01-25T21:04:27.579Z
General Question
v1
how to create an array of strings where each element is the string representation of an integer
null
null
null
63c649025f004bf4898d01c5
2023-01-25T21:17:16.349Z
Help Fix Code
v1
null
int *build_array(char **strs, int size) { int result[size]; int i; for (i = 0; i < size; i++){ result[i] = *strs[i]; } return result; }
how to return a pointer
null
63c649025f004bf4898d01c5
2023-02-11T17:18:54.297Z
General Question
v1
how to use time() to get current time
null
null
null
63c649025f004bf4898d01c5
2023-02-16T21:19:17.149Z
Explain Code
v1
null
#include <stdio.h> #define SIZE 4 #define OVERFLOW 4 int main() { int index = 0; int i; int before[SIZE] = {10, 10, 10, 10}; int a[SIZE] = {0, 0, 0, 0}; int after[SIZE] = {10, 10, 10, 10}; printf("Address of the variables:\n"); for (index = 0; index < SIZE; index++) { printf("%lx -> &after[%d]\n", (unsigned long) &after[index], index); } for (index = 0; index < SIZE; index++) { printf("%lx -> &a[%d]\n", (unsigned long) &a[index], index); } for (index = 0; index < SIZE; index++) { printf("%lx -> &before[%d]\n", (unsigned long) &before[index], index); } printf("%lx -> &i\n", (unsigned long)&i); printf("%lx -> &index\n", (unsigned long)&index); printf("\n"); printf("Initial values:\n"); printf("i = %d\n", i); printf("before = {%d, %d, %d, %d}\n", before[0], before[1], before[2], before[3]); printf("a = {%d, %d, %d, %d}\n", a[0], a[1], a[2], a[3]); printf("after = {%d, %d, %d, %d}\n", after[0], after[1], after[2], after[3]); printf("\n"); for (i = 0; i < OVERFLOW; i++) { a[i] = i * 10; printf("i = %d\n", i); printf("before = {%d, %d, %d, %d}\n", before[0], before[1], before[2], before[3]); printf("a = {%d, %d, %d, %d}\n", a[0], a[1], a[2], a[3]); printf("after = {%d, %d, %d, %d}\n", after[0], after[1], after[2], after[3]); } return 0; }
null
null
63c649025f004bf4898d01c5
2023-03-12T03:57:28.517Z
General Question
v2
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position B?
null
null
null
63c649025f004bf4898d01c5
2023-03-16T16:18:35.313Z
General Question
v2
how to use -vtu4
null
null
null
63c649025f004bf4898d01c5
2023-03-16T16:28:58.235Z
Help Fix Code
v2
random generate number between 0 and 99 to a binary file
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: write_test_file filename\n"); exit(1); } FILE *fp; if ((fp = fopen(argv[1], "w")) == NULL) { perror("fopen"); exit(1); } int error = 0; // TODO: complete this program according its description above. for (int i = 0; i < 100; i++){ error = fwrite(random(99), sizeof(long), 1, fp); if(error != 1){ fprintf(stderr, "Written Error"); return 1; } } fclose(fp); return 0; }
null
null
63c649025f004bf4898d01c5
2023-03-23T17:47:16.640Z
Help Write Code
v2
null
null
null
// Step 1: update inbuf (how many bytes were just added?)
63c649045f004bf4898d01d4
2023-01-17T22:28:08.448Z
General Question
v1
how to code a web site in C
null
null
null
63c649045f004bf4898d01d4
2023-01-22T00:29:33.113Z
Explain Code
v1
null
#include <stdio.h> #include <stdlib.h> /* * Return an array of the squares from 1 to max_val. */ int *squares(int max_val) { int *result = malloc(sizeof(int) * max_val); int i; for (i = 1; i <= max_val; i++) { result[i - 1] = i * i ; } return result; } int main() { int *squares_to_10 = squares(10); // let's print them out int i; for (i = 0; i < 10; i++) { printf("%d\t", squares_to_10[i]); } printf("\n"); return 0; }
null
null
63c649045f004bf4898d01d4
2023-02-13T22:38:27.331Z
Explain Code
v1
null
int create_user(const char *name, User **user_ptr_add) { if(strlen(name) + 1 > MAX_NAME){ return 2; } User * current = *user_ptr_add; User *new_user = malloc(sizeof(User)); if(new_user == NULL){ perror("malloc"); exit(1); } new_user->first_post = NULL; new_user->next = NULL; for(int i = 0; i < MAX_FRIENDS;i++){ new_user->friends[i] = NULL; } strncpy(new_user->name, name, MAX_NAME); new_user->name[MAX_NAME - 1] = '\0'; User * predecessor; if(current != NULL){ while(current != NULL){ if(strcmp(current->name, name) == 0 ){ return 1; } predecessor = current; current = current -> next; } predecessor->next = new_user; }else{ *user_ptr_add = new_user; } return 0; }
null
null
63c649045f004bf4898d01d4
2023-02-13T22:39:57.798Z
Question from Code
v1
do you think there are any memory leaks here?
int create_user(const char *name, User **user_ptr_add) { if(strlen(name) + 1 > MAX_NAME){ return 2; } User * current = *user_ptr_add; User *new_user = malloc(sizeof(User)); if(new_user == NULL){ perror("malloc"); exit(1); } new_user->first_post = NULL; new_user->next = NULL; for(int i = 0; i < MAX_FRIENDS;i++){ new_user->friends[i] = NULL; } strncpy(new_user->name, name, MAX_NAME); new_user->name[MAX_NAME - 1] = '\0'; User * predecessor; if(current != NULL){ while(current != NULL){ if(strcmp(current->name, name) == 0 ){ return 1; } predecessor = current; current = current -> next; } predecessor->next = new_user; }else{ *user_ptr_add = new_user; } return 0; } typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User;
null
null
63c649045f004bf4898d01d4
2023-02-13T22:42:40.039Z
Question from Code
v1
do you see any memory leaks now?
int create_user(const char *name, User **user_ptr_add) { if(strlen(name) + 1 > MAX_NAME){ return 2; } User * current = *user_ptr_add; User *new_user = malloc(sizeof(User)); if(new_user == NULL){ perror("malloc"); free(new_user); exit(1); } new_user->first_post = NULL; new_user->next = NULL; for(int i = 0; i < MAX_FRIENDS;i++){ new_user->friends[i] = NULL; } strncpy(new_user->name, name, MAX_NAME); new_user->name[MAX_NAME - 1] = '\0'; User * predecessor; if(current != NULL){ while(current != NULL){ if(strcmp(current->name, name) == 0 ){ return 1; } predecessor = current; current = current -> next; } predecessor->next = new_user; }else{ *user_ptr_add = new_user; } return 0; } typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User;
null
null
63c649045f004bf4898d01d4
2023-02-13T22:46:14.337Z
General Question
v1
how can malloc allocate the memory if it returns NULL?
null
null
null
63c649045f004bf4898d01d9
2023-01-30T18:13:36.139Z
General Question
v1
How do I use fscanf?
null
null
null
63c649045f004bf4898d01d9
2023-01-30T21:09:06.341Z
General Question
v1
How to call function from another external c file?
null
null
null
63d1c5af6dac7d3734e69fce
2023-01-27T19:41:24.351Z
Question from Code
v1
what does this code do
int *build_array(char **strs, int size) { int *ints = (int *)malloc(size * sizeof(int)); for (int i = 0; i < size; i++) { *(ints + i) = (int)*(strs+i); } }
null
null
63d1c5af6dac7d3734e69fce
2023-01-27T19:43:28.002Z
Question from Code
v1
if i input ["32", "10"] what will the outputted array look like
int *build_array(char **strs, int size) { int *ints = (int *)malloc(size * sizeof(int)); for (int i = 0; i < size; i++) { *(ints + i) = (int)*(strs+i); } return ints; }
null
null
63d1c5af6dac7d3734e69fce
2023-01-27T19:48:31.858Z
General Question
v1
is c division, floor division?
null
null
null
63d1c5af6dac7d3734e69fce
2023-01-27T21:39:47.088Z
General Question
v1
how to allocate a pointer to an array of length 2 that contain 2 pointers to arrays
null
null
null
63d1c5af6dac7d3734e69fce
2023-01-27T22:14:13.015Z
Question from Code
v1
what errors are in my code? how to fix?
int **split_array(const int *s, int length) { int n; int counter = 0; int counter1 = 0; if (length % 2 == 0) n = length / 2; else n = length / 2 + 1; int **nums = (int **)malloc(2 * sizeof(int *)); int *nums = (int *)malloc(n * sizeof(int)); int *(nums + 1) = (int *)malloc((length / 2) * sizeof(int)); for (int i = 0; i < length; i++) { if (i % 2 == 0) { *(*(nums + counter)) = *(s + i); counter++; } else { *(*(nums + counter1) + 1) = *(s + i); counter1++; } } return nums; /* for (int i = 0; i < n; i) { *(even + i) = *(s + i); } for (int j = 0; j < (n / 2); j++) { *(odd + j) = *() }*/ }
null
null
63d1c5af6dac7d3734e69fce
2023-01-30T17:29:28.286Z
Question from Code
v1
why is my code giving an abort message after compiling, or maybe there's another error?
int populate_array(int sin, int *sin_array) { int x = sin; int count = 0; while (x >= 1) { x = x / 10; count++; } if (count != 9) return 1; char sin_num[10]; sprintf(sin_num, "%d", sin); //printf("%s\n", sin_num); for (int i = 0; i < 9; i++) { sin_array[i] = sin_num[i] - 48; //printf("%d\n", sin_array[i]); } sin_array[10] = '\0'; return 0; } // TODO: Implement check_sin /* * Return 0 if the given sin_array is a valid SIN, and 1 otherwise. */ int check_sin(int *sin_array) { char large_num[3]; if (sin_array[0] == 0) return 1; int sum = 0; for (int i = 0; i < 9; i++) { //printf("*\n"); if (i % 2 == 1) { if (sin_array[i] * 2 >= 10) { //printf("@\n"); sprintf(large_num, "%d", sin_array[i] * 2); sum += (((int)large_num[0] - 48) + ((int)large_num[1] - 48)); //printf("left: %d\n", (int)large_num[0] - 48); //printf("right: %d\n", (int)large_num[1] - 48); large_num[2] = '\0'; } else { sum += (sin_array[i] * 2); } } else { //printf("#\n"); sum += sin_array[i]; } //printf("The Sum: %d\n", sum); } //printf("%d\n", sum); if (sum % 10 == 0) return 0; return 1; } 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. printf("********\n"); int x = strtol(argv[1], NULL, 10); int y[10]; int z = populate_array(x, y); printf("@@@@@@@@@@\n"); if (z == 1) { printf("Invalid SIN\n"); return 1; } if (check_sin(y) == 1) { printf("Invalid SIN\n"); return 1; } printf("Valid SIN\n"); printf("########\n"); return 0; }
null
null
63d1c5af6dac7d3734e69fce
2023-02-15T21:21:25.408Z
Question from Code
v1
why is there segfault in lines 32-33?
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 make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) return 2; int j = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == NULL) break; else if (strcmp(author->friends[i]->name, target->name) == 0) { j++; break; } } //printf("**********\n"); if (j == 0) return 1; Post *new_post = (Post *)malloc(sizeof(Post)); printf("**********\n"); strcpy(new_post->author, author->name); printf("**********\n"); new_post->contents[MAX_NAME - 1] = '\0'; strcpy(new_post->contents, contents); //new_post->contents[MAX_NAME - 1] = '\0'; printf("***@@@@@@@@*\n"); new_post->date = (time_t *)malloc(sizeof(time_t)); *(new_post->date) = time(NULL); new_post->next = target->first_post; printf("***##########*\n"); target->first_post = new_post; return 0; }
null
null
63d1c5af6dac7d3734e69fce
2023-02-15T21:22:31.128Z
General Question
v1
why do i need to dynamically allocate strings that are declared like this char *x for example
null
null
null
63d1c5af6dac7d3734e69fce
2023-02-15T21:25:21.442Z
Help Fix Code
v1
null
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 make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) return 2; int j = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == NULL) break; else if (strcmp(author->friends[i]->name, target->name) == 0) { j++; break; } } //printf("**********\n"); if (j == 0) return 1; Post *new_post = (Post *)malloc(sizeof(Post)); printf("**********\n"); strcpy(new_post->author, author->name); printf("**********\n"); new_post->contents[MAX_NAME - 1] = '\0'; strcpy(new_post->contents, contents); //new_post->contents[MAX_NAME - 1] = '\0'; printf("***@@@@@@@@*\n"); new_post->date = (time_t *)malloc(sizeof(time_t)); *(new_post->date) = time(NULL); new_post->next = target->first_post; printf("***##########*\n"); target->first_post = new_post; return 0; }
fix code so that new_post->contents dynamically allocates so that it doesn't cause segfault
null
63d1c5af6dac7d3734e69fce
2023-03-15T19:53:26.842Z
Question from Code
v2
why doesn't the if statement ever give true
int n = fork(); if (n == 0) { char *buffer = malloc(sizeof(char) * MAXLINE); buffer = args_to_string(act_ptr->args, buffer, get_size(act_ptr->args)); printf("%s\n", buffer); printf("^^^^^^^^^^^^\n"); execvp(act_ptr->args[0], act_ptr->args); perror("execvp"); exit(1); }
null
null
63d1c5af6dac7d3734e69fce
2023-03-15T19:54:49.970Z
Question from Code
v2
will the child process reach inside the if statement?
int n = fork(); if (n == 0) { char *buffer = malloc(sizeof(char) * MAXLINE); buffer = args_to_string(act_ptr->args, buffer, get_size(act_ptr->args)); printf("%s\n", buffer); printf("^^^^^^^^^^^^\n"); execvp(act_ptr->args[0], act_ptr->args); perror("execvp"); exit(1); }
null
null
63d1c5af6dac7d3734e69fce
2023-03-15T19:56:54.995Z
Question from Code
v2
but the print message in the if statement is never printed? so this means the child process never reaches inside the if statement
int n = fork(); if (n == 0) { char *buffer = malloc(sizeof(char) * MAXLINE); buffer = args_to_string(act_ptr->args, buffer, get_size(act_ptr->args)); printf("%s\n", buffer); printf("^^^^^^^^^^^^\n"); execvp(act_ptr->args[0], act_ptr->args); perror("execvp"); exit(1); }
null
null
63d1c5af6dac7d3734e69fce
2023-03-15T20:01:30.388Z
Question from Code
v2
why is it that in run_make_zero, the ^^^^^^ is never printed, is it possible that other processes run first and that execvp causes them to terminate or what,
void run_make_zero(char *target, Rule *rules) { printf("***********\n"); int x = 0; Rule *p = finds_rule(rules, target); if (p == NULL) { p = rules; } Dependency *ptr = p->dependencies; if (ptr == NULL) { return; } printf("***********\n"); while (ptr != NULL) { run_make_zero(ptr->rule->target, rules); printf("&&&&&&&&&&\n"); x = check_time(p->target, ptr->rule->target); ptr = ptr->next_dep; } printf("$$$$$$$$$\n"); printf("%d\n\n\n", x); if (x == 1) { Action *act_ptr = p->actions; int status; printf("@@@@@@@@\n"); while (act_ptr != NULL) { int n = fork(); if (n == 0) { char *buffer = malloc(sizeof(char) * MAXLINE); buffer = args_to_string(act_ptr->args, buffer, get_size(act_ptr->args)); printf("%s\n", buffer); printf("^^^^^^^^^^^^\n"); waitpid(n, &status, 0); execvp(act_ptr->args[0], act_ptr->args); perror("execvp"); exit(1); } waitpid(n, &status, 0); if (WIFEXITED(status)) { int childstatus = WEXITSTATUS(status); if (childstatus != 0) { exit(childstatus); } } act_ptr = act_ptr->next_act; } } } void run_make(char *target, Rule *rules, int pflag) { if (pflag == 0) { run_make_zero(target, rules); }
null
null
63d1c5af6dac7d3734e69fce
2023-04-04T02:08:58.141Z
General Question
v2
how to output time in this format, "Date: Fri Mar 31 18:45:19 2023" time_t *date; this is what I receive
null
null
null
63d1c5d46dac7d3734e6a411
2023-03-12T19:21:56.221Z
General Question
v2
Could you give me a hint on how I can start writing the code for parse_file in assignment 3?
null
null
null
63d1c5d46dac7d3734e6a411
2023-03-12T19:51:20.813Z
Help Write Code
v2
null
null
null
read from the open file fp
63d1c5d46dac7d3734e6a411
2023-03-12T19:55:31.941Z
Help Fix Code
v2
I want to know if I can properly open file fp
/* 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. char arr[100]; fp = fopen("filename", "r"); fgets(char, 100, fp); return NULL; }
null
null
63d1c5d46dac7d3734e6a411
2023-03-12T19:57:56.112Z
Help Fix Code
v2
I applied the feedback from last time and want to know if this code successfully open file fp
/* 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. char arr[100]; fp = fopen("filename", "r"); if (fp == NULL) { print("Error: Cannot open the file or the file does not exist"); exit(1); } fgets(char, 100, fp); return NULL; } return NULL; }
null
null
63d1c5d46dac7d3734e6a411
2023-03-12T19:59:15.363Z
General Question
v2
how to exit the program in C?
null
null
null
63d1c5d46dac7d3734e6a411
2023-03-12T19:59:50.808Z
Help Write Code
v2
null
null
null
how to start writing code for parse_file?
63d1c5d46dac7d3734e6a411
2023-03-12T21:58:07.982Z
Help Write Code
v2
null
null
null
how to read from the open file fp for parse file in Assignment 3
63d1c5d46dac7d3734e6a411
2023-03-12T21:59:16.847Z
Help Write Code
v2
null
null
null
what does the code have to look like for parse_file in assignment 3?
63d1c5d46dac7d3734e6a411
2023-03-12T22:12:29.195Z
Help Write Code
v2
null
null
null
help me with implementation of parse_file
63d1c5d46dac7d3734e6a411
2023-03-12T22:24:43.208Z
Help Write Code
v2
null
null
null
please tell me the steps into implementing parse_file in Assignment 3
63d1c5d46dac7d3734e6a411
2023-03-13T02:45:44.631Z
General Question
v2
what does the input look like for parse_file in assignment 3?
null
null
null
63d1c5d46dac7d3734e6a411
2023-03-13T03:59:46.208Z
General Question
v2
what does the output of print_rules looks like for assignment 3?
null
null
null