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
⌀ |
---|---|---|---|---|---|---|---|
63c648ae5f004bf4898cfeb4 | 2023-02-03T08:42:33.368Z | Question from Code | v1 | In line 19, why am I allowed to index into s when s was defined as a string literal? | #include <stdlib.h>
#include <stdio.h>
#include <string.h>
int truncate(char *, int);
/*
Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5.
*/
int truncate(char *s, int n) {
int length = strlen(s);
if (length > n) {
s[n] = '\0';
return (length - n);
} else {
return 0;
}
}
int main(int argc, char **argv) {
/* Do not change the main function */
if (argc != 3) {
fprintf(stderr, "Usage: truncate number string\n");
return 1;
}
int amt = strtol(argv[1], NULL, 10);
char *target = argv[2];
int soln_val = truncate(target, amt);
printf("%d %s\n", soln_val, target);
return 0;
} | null | null |
63c648ae5f004bf4898cfeb4 | 2023-02-03T08:45:06.093Z | Question from Code | v1 | For the function truncate, s is defined as a string literal, but I am still allowed to index into s and change the value. Why is this allowed when s is a pointer to read only memory? | #include <stdlib.h>
#include <stdio.h>
#include <string.h>
int truncate(char *, int);
/*
Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5.
*/
int truncate(char *s, int n) {
int length = strlen(s);
if (length > n) {
s[n] = '\0';
return (length - n);
} else {
return 0;
}
}
int main(int argc, char **argv) {
/* Do not change the main function */
if (argc != 3) {
fprintf(stderr, "Usage: truncate number string\n");
return 1;
}
int amt = strtol(argv[1], NULL, 10);
char *target = argv[2];
int soln_val = truncate(target, amt);
printf("%d %s\n", soln_val, target);
return 0;
} | null | null |
63c648b15f004bf4898cfecd | 2023-01-25T20:31:34.272Z | Help Fix Code | v1 | null | void fib(int **fib_sequence, int count) {
*fib_sequence = malloc(sizeof(int) * count);
for (int i = 0; i < count; i++) {
if (i == 0) {
fib_sequence[i] = 0;
}
else if (i == 1) {
fib_sequence[i] = 1;
}
else {
fib_sequence[i] = fib_sequence[i - 2] + fib_sequence[i - 1];
}
}
}
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;
} | fib assigns the fibonacci sequance to a dynammically allocated array | null |
63c648b15f004bf4898cfecd | 2023-01-26T02:50:33.462Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int *result_array[2];
int counter1 = 0;
int counter2 = 0;
// determine how much memory for array 1 and 2
if (length % 2 == 0){
result_array[0] = malloc(sizeof(int) * (length / 2));
result_array[1] = malloc(sizeof(int) * (length / 2));
}
// if even there are half even numbers, if odd there are n - 1 / 2
else {
result_array[0] = malloc(sizeof(int) * (((length - 1) / 2) + 1));
result_array[1] = malloc(sizeof(int) * ((length - 1) / 2));
}
for (int i = 0; i < length; i++) {
if ( (i + 1) % 2 != 0) {
result_array[0][counter1] = s[i];
counter1 += 1;
}
else {
result_array[1][counter2] = s[i];
counter2 += 1;
}
}
return &result_array;
} | Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices. | null |
63c648b25f004bf4898cfed7 | 2023-03-15T00:55:15.924Z | Explain Code | v2 | null | Rule *parse_file(FILE *fp) {
char line[MAXLINE];
char buffer[MAXLINE];
Rule *first_rule;
Rule *curr;
while (fgets(line, MAXLINE, fp)) {
if (is_comment_or_empty(line)) {
continue;
}
if (line[0] == '\t' || line[0] == '\r' || line[0] == '\n') {
continue;
} else {
strncpy(buffer, strtok(line, ":"), MAXLINE);
buffer[MAXLINE - 1] = '\0';
char *target = malloc(sizeof(char) * (strlen(buffer) + 1));
target[strlen(buffer)] = '\0';
strncpy(target, buffer, strlen(buffer) + 1);
if (first_rule == NULL) {
first_rule = make_new_rule(target);
curr = first_rule;
} else {
curr = get_rule(target, first_rule);
if (curr == NULL) {
get_tail(first_rule)->next_rule = make_new_rule(target);
curr = get_tail(first_rule);
}
}
free(target);
char *dependency = strtok(strtok(NULL, ":"), " ");
while (dependency != NULL) {
make_dependency(curr, dependency, first_rule);
strncpy(dependency, strtok(NULL, " "), MAXLINE);
}
}
}
return first_rule;
} | null | null |
63c648b25f004bf4898cfed7 | 2023-03-15T01:00:14.162Z | Help Fix Code | v2 | this code should be reading dependency information into structs and linking them to rules | Rule *parse_file(FILE *fp) {
// Implement this function and remove the stubbed return statement below.
char line[MAXLINE];
char buffer[MAXLINE]; // buffer
Rule *first_rule;
Rule *curr;
while (fgets(line, MAXLINE, fp)){
if (is_comment_or_empty(line)){
continue; // skip empty or comment lines
}
if (line[0] == '\t' || line[0] == '\r' || line[0] == '\n'){
// actions
continue;
//hello
} else{
// handle dependencies
char *dependency = strtok(strtok(NULL, ":"), " ");
while (dependency != NULL){
make_dependency(curr, dependency, first_rule);
strncpy(dependency, strtok(NULL, " "), MAXLINE);
}
}
}
return first_rule;
}
Dependency *make_dependency(Rule *parent_rule, char *filename, Rule *first_rule){
Rule *dep_rule = get_rule(filename, first_rule);
if (dep_rule == NULL){
dep_rule = make_new_rule(filename);
get_tail(first_rule)->next_rule = dep_rule;
}
Dependency *curr_dep = malloc(sizeof(Dependency));
curr_dep->rule = dep_rule;
Dependency *tail_dep = parent_rule->dependencies;
if (tail_dep == NULL){
parent_rule->dependencies = curr_dep;
} else {
while (tail_dep->next_dep != NULL){
tail_dep = tail_dep->next_dep;
}
tail_dep->next_dep = curr_dep;
}
return curr_dep;
} | null | null |
63c648b25f004bf4898cfed7 | 2023-03-15T01:11:36.770Z | General Question | v2 | give me an example using strtok | null | null | null |
63c648b25f004bf4898cfed7 | 2023-03-15T01:12:27.515Z | General Question | v2 | can you give me an example using strtok in c | null | null | null |
63c648b25f004bf4898cfedc | 2023-02-28T19:16:59.275Z | Explain Code | v1 | null | char *tmp = strchr(arg, ’ ’);
e->date = strtol(tmp + 1, NULL, 10); | null | null |
63c648b25f004bf4898cfedc | 2023-02-28T21:52:00.798Z | General Question | v1 | if i try to access an array at position 0 without initializing the array, will I get a segmentation fault? | null | null | null |
63c648b25f004bf4898cfedc | 2023-02-28T21:52:48.167Z | General Question | v1 | how do i initialize an array after declaring int name[10];?
| null | null | null |
63c648b25f004bf4898cfedc | 2023-03-14T13:50:02.107Z | Question from Code | v2 | What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position B? | int x = 5;
void handler(int sig) {
x += 3;
fprintf(stderr, "inside %d ", x);
}
int main() {
fprintf(stderr, "start ");
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
x += 2;
fprintf(stderr, "outside %d", x);
return 0;
} | null | null |
63c648b25f004bf4898cfedc | 2023-03-14T13:51:25.865Z | Question from Code | v2 | What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position C? | int x = 5;
void handler(int sig) {
x += 3;
fprintf(stderr, "inside %d ", x);
}
int main() {
fprintf(stderr, "start ");
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
x += 2;
fprintf(stderr, "outside %d", x);
return 0;
} | null | null |
63c648b25f004bf4898cfedc | 2023-03-14T13:59:52.306Z | Question from Code | v2 | What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position B? | int x = 5;
void handler(int sig) {
x += 3;
fprintf(stderr, "inside %d ", x);
}
int main() {
fprintf(stderr, "start ");
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
x += 2;
fprintf(stderr, "outside %d", x);
return 0;
} | null | null |
63c648b25f004bf4898cfedc | 2023-03-16T03:07:34.432Z | Question from Code | v2 | what does this line of code mean? | if (cur->dependencies || cur->actions) | null | null |
63c648b35f004bf4898cfee6 | 2023-01-19T23:36:42.584Z | Help Fix Code | v1 | null | int main() {
char phonenumber[11];
int position;
scanf("%11s %d", phonenumber, &position);
if (position > 9 || position < -1) {
printf("ERROR\n");
return 1;
}
if (position == -1) {
printf("%s\n", phonenumber);
}
else {
printf("%c\n", phonenumber[position]);
}
return 0;
} | null | null |
63c648b35f004bf4898cfee6 | 2023-01-19T23:47:49.913Z | General Question | v1 | How can I check if scanf() is done reading input from standard input? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-22T03:05:37.209Z | General Question | v1 | How do I convert a string to an integer in C? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-22T03:10:18.134Z | General Question | v1 | How do I ignore a line from standard input in C? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-22T03:16:39.201Z | General Question | v1 | How do I read the first character of a string and then the rest of the string using scanf() in C? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-22T03:46:57.223Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *check_against, char *to_check)
{
for (int i = 0; i < 9; i++)
{
if (check_against[i] != '-' && check_against[i] != to_check[i])
{
return 0;
}
}
return 1;
}
int main(int argc, char **argv)
{
if (!(argc == 2 || argc == 3))
{
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// Variables for while loop
int count = 0;
int matches = 1;
int check_flag = 0;
// Variables for scanf()
char directory_flag;
char permissions[9];
int file_size;
// TODO: Process command line arguments.
int threshold = atoi(argv[1]);
if (argc == 3)
{
check_flag = 1;
}
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
scanf("%*[^\n]");
while (scanf("%c%s %*s %*s %*s %d %*s %*s %*s %*s", &directory_flag, permissions, &file_size) == 3)
{
if (file_size > threshold && directory_flag == '-')
{
if (check_flag)
{
matches = check_permissions(argv[2], permissions);
}
if (matches)
{
count++;
}
}
}
printf("%d\n", count);
return 0;
}
| null | null |
63c648b35f004bf4898cfee6 | 2023-01-22T03:48:17.755Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *check_against, char *to_check)
{
for (int i = 0; i < 9; i++)
{
if (check_against[i] != '-' && check_against[i] != to_check[i])
{
return 0;
}
}
return 1;
}
int main(int argc, char **argv)
{
if (!(argc == 2 || argc == 3))
{
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// Variables for while loop
int count = 0;
int matches = 1;
int check_flag = 0;
// Variables for scanf()
char directory_flag;
char permissions[9];
int file_size;
// TODO: Process command line arguments.
int threshold = atoi(argv[1]);
if (argc == 3)
{
check_flag = 1;
}
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
scanf("%*[^\n]");
while (scanf("%c%s %*s %*s %*s %d %*s %*s %*s %*s", &directory_flag, permissions, &file_size) == 3)
{
if (file_size > threshold && directory_flag == '-')
{
if (check_flag)
{
matches = check_permissions(argv[2], permissions);
}
if (matches)
{
count++;
}
}
}
printf("%d\n", count);
return 0;
}
| There is stack smashing detected according to the C compiler. | null |
63c648b35f004bf4898cfee6 | 2023-01-22T03:49:53.622Z | General Question | v1 | How do I initialize a character array in C? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-22T03:51:06.592Z | General Question | v1 | How do I fix stack smashing detected in C? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T01:06:07.354Z | General Question | v1 | Does the strlen() function in C count the end-of-line character? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T01:10:54.331Z | General Question | v1 | Is there a function to check for string equality in C? If so, what is it? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T01:56:54.368Z | General Question | v1 | If I pass in a const int as a function argument, can I set the value of an int variable to the value of the function argument? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T03:24:36.642Z | General Question | v1 | How do I read in a line from a file? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T03:30:45.015Z | General Question | v1 | Can I print file contents to standard output without reading a line into a string? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T03:39:53.861Z | General Question | v1 | How does fopen behave if the filename doesn't exist or the file couldn't be opened? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T03:46:46.606Z | General Question | v1 | How do I open a file in read-only mode using fopen? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T03:55:14.819Z | General Question | v1 | How do I print out the contents of a file to standard output? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T04:09:41.527Z | General Question | v1 | How do I print out a time_t object using printf? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T19:38:18.398Z | General Question | v1 | How do I handle tail deletion in a linked list? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T21:46:56.646Z | Explain Code | v1 | null | int populate_array(int sin, int *sin_array)
{
// If a number is not 9 digits, performing integer division
// will either yield 0 (<9 digits) or a number > 10 (>9 digits)
if (sin / 100000000 < 1 || sin / 100000000 > 9)
{
return 1;
}
// Populate the array in reverse
for (int i = 8; i > -1; i--)
{
sin_array[i] = sin % 10;
sin = sin / 10;
}
return 0;
} | null | null |
63c648b35f004bf4898cfee6 | 2023-01-29T21:49:14.702Z | Explain Code | v1 | null | int check_sin(int *sin_array)
{
int sum = 0;
// Implement the Luhn algorithm
for (int i = 0; i < 9; i++)
{
if (i % 2 != 0)
{
sin_array[i] *= 2;
if (sin_array[i] >= 10)
{
sin_array[i] = sin_array[i] / 10 + sin_array[i] % 10;
}
}
sum += sin_array[i];
}
return (sum % 10 != 0);
} | null | null |
63c648b35f004bf4898cfee6 | 2023-02-12T22:17:22.553Z | General Question | v1 | How do I use strftime()? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-02-12T22:28:44.080Z | General Question | v1 | If a pointer exists inside a struct, do I need malloc space for the object it's pointing to? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-02-24T04:21:14.602Z | General Question | v1 | What is the purpose of the $< automatic variable in a Makefile? Does it have any special purpose? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-02-24T04:21:44.687Z | General Question | v1 | When would I use the $< automatic variable in a Makefile? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T14:59:52.403Z | General Question | v2 | What does the execl() function do? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T15:00:47.043Z | General Question | v2 | Why do I need to call fork() before execl()? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T15:01:38.490Z | General Question | v2 | How can I copy file descriptors to a child process created by fork()? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T15:03:18.527Z | General Question | v2 | Are there defined macros for the standard file descriptors? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T15:03:45.689Z | General Question | v2 | How do I use pipes to communicate between parent and child processes? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T22:32:42.851Z | General Question | v2 | How can I see what's being written to standard input when I use write() with STDIN_FILENO? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T22:33:41.031Z | General Question | v2 | What does a "bad file descriptor" error mean? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T22:34:37.463Z | General Question | v2 | What are the standard file descriptors? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T22:35:01.224Z | General Question | v2 | What are the macros for the standard file descriptors? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T22:39:45.556Z | General Question | v2 | How do I write to stdin with a pipe? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T22:40:34.782Z | General Question | v2 | How do I make the write end of a pipe point to stdin? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T22:47:46.466Z | General Question | v2 | How do I use execl? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-10T23:14:17.292Z | General Question | v2 | Does the process executed by execl() inherit any variables from the caller? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-12T01:26:31.497Z | General Question | v2 | How do I use strtok()? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-12T03:08:05.843Z | General Question | v2 | Can I use fseek() with non-binary files? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-13T22:55:19.805Z | General Question | v2 | How do I use stat()? | null | null | null |
63c648b35f004bf4898cfee6 | 2023-03-30T01:59:32.273Z | General Question | v2 | Can I put a string literal directly in a write call? | null | null | null |
63c648b55f004bf4898cfef0 | 2023-01-17T20:34:55.566Z | General Question | v1 | how to loop over i up to 10?
| null | null | null |
63c648b55f004bf4898cfef0 | 2023-01-17T20:36:28.298Z | General Question | v1 | how to use pointers? | null | null | null |
63c648b55f004bf4898cfef5 | 2023-01-24T20:56:08.603Z | General Question | v1 | how would I filter for some columns in a line that is taken in by scanf | null | null | null |
63c648b55f004bf4898cfef5 | 2023-01-24T20:57:12.835Z | General Question | v1 | how would I take in an entire line of input through scanf instead of the first word? | null | null | null |
63c648b55f004bf4898cfef5 | 2023-01-24T20:58:41.162Z | General Question | v1 | How would I read an entire line of input with scanf, but only save a certain "column" of words from the line? | null | null | null |
63c648b55f004bf4898cfef5 | 2023-01-24T22:20:24.516Z | Question from Code | v1 | Given the standard input "-rwx------ 1 reid staff 1734 Jun 22 14:52 prog", what would be the stamnard output on line 7, and why? | int main() {
char permissions[10];
int size;
char filename[20];
scanf("%9s %*s %*s %*s %d %*s %*s %*s %s", permissions, &size, filename;
printf("%s\t%d\t%s\n", permissions, size, filename);
} | null | null |
63c648b55f004bf4898cfef5 | 2023-01-24T22:41:13.031Z | Question from Code | v1 | Running the code with the following stdin from a file:
"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"
What should be printed out, and why? | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *, char *);
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
char* specified_size = argv[1];
char* specified_permission = argv[2];
printf("%s\t%s\n", specified_size, specified_permission);
int total_num;
scanf("%*s %d\n", &total_num);
printf("%d\n", total_num);
char permissions[10];
int size;
char filename[20];
while (scanf("%9s %*s %*s %*s %d %*s %*s %*s %s", permissions, &size, filename) != EOF) {
printf("%s\n", permissions);
printf("%s\t%d\t%s\n", permissions, size, filename);
}
// TODO: Process the command line parameters
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
return 0;
}
| null | null |
63c648b55f004bf4898cfef5 | 2023-02-09T22:46:26.300Z | Question from Code | v1 | When I put into the standard input
"add_user h", and then try
"add_user he" in the following standard input, it tells me that the same username already exists. But this username does not already exist; why is it finding that this username already exists if it doesn't? | #include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
User new_user;
strncpy(new_user.name, name, MAX_NAME);
new_user.name[MAX_NAME - 1] = '\0';
User *curr_user_ptr = *user_ptr_add;
if (curr_user_ptr == NULL) {
*user_ptr_add = &new_user;
return 0;
}
while (curr_user_ptr->next != NULL) {
if (strcmp(curr_user_ptr->name, name) == 0) {
return 1;
}
curr_user_ptr = curr_user_ptr->next;
}
curr_user_ptr->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) {
while (head != NULL) {
if (strcmp(head->name, name) == 0) {
return (User *) head;
}
head = head->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
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) {
if (strlen(filename) > MAX_NAME - 1) { // - 1 b/c null terminator!
return 2;
}
FILE *profile_pic = fopen(filename, "r");
if (profile_pic == NULL) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
user->profile_pic[MAX_NAME - 1] = '\0';
return 0;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
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) {
return -1;
}
/*
* 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) {
return -1;
}
/*
* 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) {
return -1;
}
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
/*printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}*/
return 0;
}
| null | null |
63c648b55f004bf4898cfef5 | 2023-02-12T01:46:40.745Z | Question from Code | v1 | When I run this with standard inputs:
"add_user j"
"add_user k"
"make_friends j k"
the program segmentation faults. It's supposed to create two new User objects named j and k, and then make them friends but instead segmentation faults. Why is this? | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
/*printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}*/
return 0;
}
#include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* HELPER FUNCTION *******
* Calculate the length of a friends list
*/
int num_friends(User **friends_list) {
int friends = 0;
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friends_list[i] != NULL) {
friends++;
} else {
return friends;
}
}
return friends;
}
/*
* HELPER FUNCTION ********
* Return if name is a friend in friend_list
*
* Return:
* - 1 if they are friends
* - 0 otherwise
*/
int in_friend_list(const char *name, User **friend_list) {
for (int i = 0; i < MAX_FRIENDS; i++) {
if (strcmp((friend_list[i])->name, name) == 0) {
return 1;
}
}
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)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
User *new_user = malloc(sizeof(User));
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL; // initialize the array to NULL for all elements
}
User *curr_user_ptr = *user_ptr_add;
if (curr_user_ptr == NULL) {
*user_ptr_add = new_user;
return 0;
}
while (curr_user_ptr->next != NULL) {
if (strcmp(curr_user_ptr->name, name) == 0) {
return 1;
}
curr_user_ptr = curr_user_ptr->next;
}
curr_user_ptr->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) {
while (head != NULL) {
if (strcmp(head->name, name) == 0) {
return (User *) head;
}
head = head->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
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) {
if (strlen(filename) > MAX_NAME - 1) { // - 1 b/c null terminator!
return 2;
}
FILE *profile_pic = fopen(filename, "r");
if (profile_pic == NULL) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
user->profile_pic[MAX_NAME - 1] = '\0';
return 0;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
if (strcmp(name1, name2) == 0) {
return 3;
}
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if (user1 == NULL || user2 == NULL) {
return 4;
}
// check if friends
// make helper function: are_friends
if (in_friend_list(name2, user1->friends) == 1 || in_friend_list(name1, user2->friends) == 1) {
return 1;
}
// if not friends, check if at least one has MAX_FRIENDS
if (num_friends(user1->friends) > MAX_FRIENDS || num_friends(user2->friends) > MAX_FRIENDS) {
return 2;
}
// otherwise, make them friends!
(user1->friends)[num_friends(user1->friends)] = user2;
(user2->friends)[num_friends(user1->friends)] = user1;
return 0;
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
if (user == NULL) {
return 1;
}
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
int i = 0;
while (user->friends[i] != NULL) {
printf("%s\n", user->friends[i]->name);
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *curr_post = user->first_post;
while (curr_post != NULL) {
printf("From: %s\n", curr_post->author);
printf("Date: %ln\n\n", curr_post->date);
printf("%s", curr_post->contents);
// TODO: if curr_post->next is not NULL, then print the weird next post format
}
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) {
return -1;
}
/*
* 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) {
return -1;
}
| null | null |
63c648b55f004bf4898cfef5 | 2023-02-12T20:08:27.626Z | Question from Code | v1 | When I run this with standard inputs:
"make_user david"
"make_user bogdan"
"make_friends daivd bogdan"
and then make a post for each, when I print the profile of the person who had a post made first, it prints fine, but when I print the profile of the second post, the program crashes... why is this?
in standard input, it would be as such:
"post david bogdan bogdan is cool :)"
"profile bogdan"
will print without issue, but when i then run
"post bogdan david david is soooo cool !!!!"
"profile david"
the program segmentation faults. | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "friends.h"
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
/*printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}*/
return 0;
}
/*
* HELPER FUNCTION *******
* Calculate the length of a friends list
*/
int num_friends(User **friends_list) {
int friends = 0;
// loop through all friends, count them by checking for null (remember, we intialized it as NULL)
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friends_list[i] != NULL) {
friends++;
} else {
return friends;
}
}
return friends;
}
/*
* HELPER FUNCTION ********
* Return if name is a friend in friend_list
*
* Return:
* - 1 if they are friends
* - 0 otherwise
*/
int in_friend_list(const char *name, User **friend_list) {
if (name == NULL || friend_list == NULL) {
// error handling
return 2;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend_list[i] == NULL) {
return 0;
}
if (strcmp((friend_list[i])->name, name) == 0) {
return 1;
}
}
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)
*/
int create_user(const char *name, User **user_ptr_add) {
if (name == NULL) {
fprintf(stderr, "Cannot create user with empty name.\n");
}
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
fprintf(stderr, "Could not allocate space in memory.\n");
return 3;
}
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL; // initialize the array to NULL for all elements
}
// initialize all to NULL -- this will be our placeholder for "not set yet"
// -> we don't know what the initial value is at the memory location, so we set it as such.
new_user->first_post = NULL;
new_user->next = NULL;
User *curr_user_ptr = *user_ptr_add;
// if this is the first user being added (user_ptr_add is NULL)
if (curr_user_ptr == NULL) {
*user_ptr_add = new_user;
return 0;
}
// loop through linkedlist until the end, to add user
while (curr_user_ptr->next != NULL) {
// if the user is already in this list, return 1
if (strcmp(curr_user_ptr->name, name) == 0) {
return 1;
}
curr_user_ptr = curr_user_ptr->next;
}
curr_user_ptr->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) {
while (head != NULL) {
if (strcmp(head->name, name) == 0) {
return (User *) head;
}
head = head->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
while (curr != NULL) {
printf(" %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) {
if (filename == NULL) {
return 1;
}
if (strlen(filename) > MAX_NAME - 1) { // - 1 b/c null terminator!
return 2;
}
FILE *profile_pic = fopen(filename, "r");
if (profile_pic == NULL) {
return 1;
}
int closed = fclose(profile_pic);
// ERROR: file could not be closed
if (closed != 0) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
user->profile_pic[MAX_NAME - 1] = '\0';
return 0;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
if (strcmp(name1, name2) == 0) {
// because we want to return largest error code, check if this user does not exist
User *user = find_user(name1, head);
if (user == NULL) {
return 4;
}
return 3;
}
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if (user1 == NULL || user2 == NULL) {
// at least one does not exist
return 4;
}
// check if friends
// make helper function: are_friends
if (in_friend_list(name2, user1->friends) == 1 || in_friend_list(name1, user2->friends) == 1) {
return 1;
}
// if not friends, check if at least one has MAX_FRIENDS
if (num_friends(user1->friends) > MAX_FRIENDS || num_friends(user2->friends) > MAX_FRIENDS) {
return 2;
}
// otherwise, make them friends!
(user1->friends)[num_friends(user1->friends)] = user2;
(user2->friends)[num_friends(user2->friends)] = user1;
return 0;
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
if (user == NULL) {
return 1;
}
// TODO: print profile picture
FILE *profile_pic;
profile_pic = fopen(user->profile_pic, "r");
if (profile_pic != NULL) {
char line[100];
while (fgets(line, 101, profile_pic) != NULL) {
printf("%s", line);
}
int err = fclose(profile_pic);
if (err != 0) {
fprintf(stderr, "Closing profile picture file failed.\n");
return 1;
}
printf("\n");
}
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
int i = 0;
while (user->friends[i] != NULL) {
printf("%s\n", user->friends[i]->name);
i++;
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *curr_post = user->first_post;
while (curr_post != NULL) {
printf("From: %s\n", curr_post->author);
// use ctime to print date in readable format
printf("Date: %s\n", ctime(curr_post->date));
printf("%s", curr_post->contents);
curr_post = curr_post->next;
// TODO: if curr_post->next is not NULL, then print the weird next post format
}
printf("------------------------------------------\n");
return 0;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
// if one of the pointers are null
if (author == NULL || target == NULL) {
return 2;
}
// users are not friends
if (in_friend_list(target->name, ((User *) author)->friends) == 0 || in_friend_list(author->name, target->friends) == 0) {
return 1;
}
// insert the new post in the FRONT of target's list of posts
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
fprintf(stderr, "Could not allocate space in memory.\n");
return 3;
}
new_post->contents = contents;
strncpy(new_post->author, author->name, MAX_NAME);
new_post->author[31] = '\0';
time_t *curr_date = malloc(sizeof(time_t));
// update curr_date to current time
time(curr_date);
new_post->date = curr_date;
// update pointers to insert new post to front of list
// THESE CAUSE SEG FAULTS
if (target->first_post == NULL) {
target->first_post = new_post;
return 0;
}
new_post->next = target->first_post->next;
target->first_post->next = new_post;
return 0;
}
// TODO: fix the seg fault from when the second person's post is added and printed!!
/*
* 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) {
return -1;
}
| null | null |
63c648b55f004bf4898cfef5 | 2023-02-13T22:42:18.297Z | Question from Code | v1 | When I run this, with standard input:
"
add_user j
add_user k
add_user l
add_user m
delete_user k
add_user k
list_users
"
the standard output begins the infinitely print "k" over and over. Why is this? | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "friends.h"
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
/*printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}*/
return 0;
}
/*
* HELPER FUNCTION *******
* Calculate the length of a friends list
*/
int num_friends(User **friends_list) {
int friends = 0;
// loop through all friends, count them by checking for null (remember, we intialized it as NULL)
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friends_list[i] != NULL) {
friends++;
} else {
return friends;
}
}
return friends;
}
/*
* HELPER FUNCTION ********
* Return if name is a friend in friend_list
*
* Return:
* - 1 if they are friends
* - 0 otherwise
*/
int in_friend_list(const char *name, User **friend_list) {
if (name == NULL || friend_list == NULL) {
// error handling
return 2;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend_list[i] == NULL) {
return 0;
}
if (strcmp((friend_list[i])->name, name) == 0) {
return 1;
}
}
return 0;
}
/*
* HELPER FUNCTION ********
* Deallocate the passed in user from memory
*/
void deallocate_user(User *user) {
free(user);
}
/*
* HELPER FUNCTION ********
* Deletes the User from the Friend friend's list
*/
void delete_user_from_friend_arr(User *user, User *friend) {
// step 1: find the index of friend array that user is at
int i = 0;
while (strcmp((friend->friends)[i]->name, user->name) != 0) {
i++;
}
// index i is the index of that friend array.
// step 2: push back everything above that index by 1
while (i < MAX_FRIENDS - 1) {
(friend->friends)[i] = (friend->friends)[i + 1];
i++;
}
// step 3: update the last index to be NULL
(friend->friends)[MAX_FRIENDS - 1] = NULL;
}
/*
* HELPER FUNCTION ********
* Removes the user from all of its friends' friend arrays.
*/
void remove_from_all_friend_arr(User *user) {
for (int i = 0; i < MAX_FRIENDS; i++) {
// if this friend exists, remove this user from their friend array
if ((user->friends)[i] != NULL) {
delete_user_from_friend_arr(user, (user->friends)[i]);
// don't have to do it the other way around because we will free this user later anyway
}
}
}
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (name == NULL) {
fprintf(stderr, "Cannot create user with empty name.\n");
}
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
fprintf(stderr, "Could not allocate space in memory.\n");
return 3;
}
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL; // initialize the array to NULL for all elements
}
// initialize all to NULL -- this will be our placeholder for "not set yet"
// -> we don't know what the initial value is at the memory location, so we set it as such.
new_user->first_post = NULL;
new_user->next = NULL;
User *curr_user_ptr = *user_ptr_add;
// if this is the first user being added (user_ptr_add is NULL)
if (curr_user_ptr == NULL) {
*user_ptr_add = new_user;
return 0;
}
// loop through linkedlist until the end, to add user
while (curr_user_ptr->next != NULL) {
// if the user is already in this list, return 1
if (strcmp(curr_user_ptr->name, name) == 0) {
return 1;
}
curr_user_ptr = curr_user_ptr->next;
}
curr_user_ptr->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) {
while (head != NULL) {
if (strcmp(head->name, name) == 0) {
return (User *) head;
}
head = head->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
while (curr != NULL) {
printf(" %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) {
if (filename == NULL) {
return 1;
}
if (strlen(filename) > MAX_NAME - 1) { // - 1 b/c null terminator!
return 2;
}
FILE *profile_pic = fopen(filename, "r");
if (profile_pic == NULL) {
return 1;
}
int closed = fclose(profile_pic);
// ERROR: file could not be closed
if (closed != 0) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
user->profile_pic[MAX_NAME - 1] = '\0';
return 0;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
if (strcmp(name1, name2) == 0) {
// because we want to return largest error code, check if this user does not exist
User *user = find_user(name1, head);
if (user == NULL) {
return 4;
}
return 3;
}
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if (user1 == NULL || user2 == NULL) {
// at least one does not exist
return 4;
}
// check if friends
// make helper function: are_friends
if (in_friend_list(name2, user1->friends) == 1 || in_friend_list(name1, user2->friends) == 1) {
return 1;
}
// if not friends, check if at least one has MAX_FRIENDS
if (num_friends(user1->friends) > MAX_FRIENDS || num_friends(user2->friends) > MAX_FRIENDS) {
return 2;
}
// otherwise, make them friends!
(user1->friends)[num_friends(user1->friends)] = user2;
(user2->friends)[num_friends(user2->friends)] = user1;
return 0;
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
if (user == NULL) {
return 1;
}
// TODO: print profile picture
FILE *profile_pic;
profile_pic = fopen(user->profile_pic, "r");
if (profile_pic != NULL) {
// TODO: revisit how you would go about printing profile picture!! this sets a hard limit for size
char line[80];
while (fgets(line, 81, profile_pic) != NULL) {
printf("%s", line);
}
int err = fclose(profile_pic);
if (err != 0) {
fprintf(stderr, "Closing profile picture file failed.\n");
return 1;
}
printf("\n");
}
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
int i = 0;
while (user->friends[i] != NULL) {
printf("%s\n", user->friends[i]->name);
i++;
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *curr_post = user->first_post;
while (curr_post != NULL) {
printf("From: %s\n", curr_post->author);
// use ctime to print date in readable format
printf("Date: %s\n", ctime(curr_post->date));
printf("%s\n", curr_post->contents);
// if there is another post, print formatting to match sample_output.txt
if (curr_post->next != NULL) {
printf("\n===\n\n");
}
curr_post = curr_post->next;
}
printf("------------------------------------------\n");
return 0;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
// if one of the pointers are null
if (author == NULL || target == NULL) {
return 2;
}
// users are not friends
if (in_friend_list(target->name, ((User *) author)->friends) == 0 || in_friend_list(author->name, target->friends) == 0) {
return 1;
}
// insert the new post in the FRONT of target's list of posts
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
fprintf(stderr, "Could not allocate space in memory.\n");
return 3;
}
new_post->contents = contents;
strncpy(new_post->author, author->name, MAX_NAME);
new_post->author[31] = '\0';
time_t *curr_date = malloc(sizeof(time_t));
// update curr_date to current time
time(curr_date);
new_post->date = curr_date;
// initialize the new_post's next attribute to be NULL
new_post->next = NULL;
// update pointers to insert new post to front of list
// THESE CAUSE SEG FAULTS
if (target->first_post == NULL) {
target->first_post = new_post;
return 0;
}
new_post->next = target->first_post->next;
target->first_post->next = 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_ptr_curr = *user_ptr_del;
if (user_ptr_curr == NULL) {
return 1;
}
if (strcmp(user_ptr_curr->name, name) == 0) {
// case: there the user we want to delete is at the head of the list
// step 2: delete it from all of its friends arrays
remove_from_all_friend_arr(user_ptr_curr);
// note: only need to remove from the friend array, since our array will be freed
// step 3: remove it from the pointer
// keep track of the user we want to deallocate, as in the next line we would lose it
User **user_to_deallocate = &user_ptr_curr;
// remove the first user from the friends list
*user_ptr_del = user_ptr_curr->next;
// step 4: free all posts on the users' timeline
// step 5: free all of its allocated memory
deallocate_user(*user_to_deallocate);
return 0;
}
// loop through the user list until either you reach the end, or find the user with the given name
while (user_ptr_curr->next != NULL && strcmp(user_ptr_curr->next->name, name) != 0) {
user_ptr_curr = user_ptr_curr->next;
}
// case: you looped through the entire user list and did not find the user with such a name
if (user_ptr_curr->next == NULL) {
return 1;
}
// otherwise, user_ptr_curr->next is the pointer to the user to be deleted.
// step 2: delete it from all of its friends arrays
remove_from_all_friend_arr(user_ptr_curr);
// step 3: remove it from the pointer
// TODO: fix this. right now it sets the head to be the user we want to delete, instead of skipping over them
// keep track of the user we want to deallocate, as in the next line we would lose it
User **user_to_deallocate = &(user_ptr_curr->next);
// skip the user we are removing in the list
user_ptr_curr->next = (user_ptr_curr->next)->next;
// step 4: free all posts on the users' timeline
// step 5: free all of its allocated memory
deallocate_user(*user_to_deallocate);
return 0;
}
| null | null |
63c648b55f004bf4898cfef5 | 2023-02-13T22:43:33.672Z | Question from Code | v1 | How would I properly delete a user from the linked list in delete_user? Why is it not properly deleting the user in my implementation? | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "friends.h"
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
/*printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}*/
return 0;
}
/*
* HELPER FUNCTION *******
* Calculate the length of a friends list
*/
int num_friends(User **friends_list) {
int friends = 0;
// loop through all friends, count them by checking for null (remember, we intialized it as NULL)
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friends_list[i] != NULL) {
friends++;
} else {
return friends;
}
}
return friends;
}
/*
* HELPER FUNCTION ********
* Return if name is a friend in friend_list
*
* Return:
* - 1 if they are friends
* - 0 otherwise
*/
int in_friend_list(const char *name, User **friend_list) {
if (name == NULL || friend_list == NULL) {
// error handling
return 2;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
if (friend_list[i] == NULL) {
return 0;
}
if (strcmp((friend_list[i])->name, name) == 0) {
return 1;
}
}
return 0;
}
/*
* HELPER FUNCTION ********
* Deallocate the passed in user from memory
*/
void deallocate_user(User *user) {
free(user);
}
/*
* HELPER FUNCTION ********
* Deletes the User from the Friend friend's list
*/
void delete_user_from_friend_arr(User *user, User *friend) {
// step 1: find the index of friend array that user is at
int i = 0;
while (strcmp((friend->friends)[i]->name, user->name) != 0) {
i++;
}
// index i is the index of that friend array.
// step 2: push back everything above that index by 1
while (i < MAX_FRIENDS - 1) {
(friend->friends)[i] = (friend->friends)[i + 1];
i++;
}
// step 3: update the last index to be NULL
(friend->friends)[MAX_FRIENDS - 1] = NULL;
}
/*
* HELPER FUNCTION ********
* Removes the user from all of its friends' friend arrays.
*/
void remove_from_all_friend_arr(User *user) {
for (int i = 0; i < MAX_FRIENDS; i++) {
// if this friend exists, remove this user from their friend array
if ((user->friends)[i] != NULL) {
delete_user_from_friend_arr(user, (user->friends)[i]);
// don't have to do it the other way around because we will free this user later anyway
}
}
}
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if (name == NULL) {
fprintf(stderr, "Cannot create user with empty name.\n");
}
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
fprintf(stderr, "Could not allocate space in memory.\n");
return 3;
}
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL; // initialize the array to NULL for all elements
}
// initialize all to NULL -- this will be our placeholder for "not set yet"
// -> we don't know what the initial value is at the memory location, so we set it as such.
new_user->first_post = NULL;
new_user->next = NULL;
User *curr_user_ptr = *user_ptr_add;
// if this is the first user being added (user_ptr_add is NULL)
if (curr_user_ptr == NULL) {
*user_ptr_add = new_user;
return 0;
}
// loop through linkedlist until the end, to add user
while (curr_user_ptr->next != NULL) {
// if the user is already in this list, return 1
if (strcmp(curr_user_ptr->name, name) == 0) {
return 1;
}
curr_user_ptr = curr_user_ptr->next;
}
curr_user_ptr->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) {
while (head != NULL) {
if (strcmp(head->name, name) == 0) {
return (User *) head;
}
head = head->next;
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
while (curr != NULL) {
printf(" %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) {
if (filename == NULL) {
return 1;
}
if (strlen(filename) > MAX_NAME - 1) { // - 1 b/c null terminator!
return 2;
}
FILE *profile_pic = fopen(filename, "r");
if (profile_pic == NULL) {
return 1;
}
int closed = fclose(profile_pic);
// ERROR: file could not be closed
if (closed != 0) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
user->profile_pic[MAX_NAME - 1] = '\0';
return 0;
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
if (strcmp(name1, name2) == 0) {
// because we want to return largest error code, check if this user does not exist
User *user = find_user(name1, head);
if (user == NULL) {
return 4;
}
return 3;
}
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if (user1 == NULL || user2 == NULL) {
// at least one does not exist
return 4;
}
// check if friends
// make helper function: are_friends
if (in_friend_list(name2, user1->friends) == 1 || in_friend_list(name1, user2->friends) == 1) {
return 1;
}
// if not friends, check if at least one has MAX_FRIENDS
if (num_friends(user1->friends) > MAX_FRIENDS || num_friends(user2->friends) > MAX_FRIENDS) {
return 2;
}
// otherwise, make them friends!
(user1->friends)[num_friends(user1->friends)] = user2;
(user2->friends)[num_friends(user2->friends)] = user1;
return 0;
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
if (user == NULL) {
return 1;
}
// TODO: print profile picture
FILE *profile_pic;
profile_pic = fopen(user->profile_pic, "r");
if (profile_pic != NULL) {
// TODO: revisit how you would go about printing profile picture!! this sets a hard limit for size
char line[80];
while (fgets(line, 81, profile_pic) != NULL) {
printf("%s", line);
}
int err = fclose(profile_pic);
if (err != 0) {
fprintf(stderr, "Closing profile picture file failed.\n");
return 1;
}
printf("\n");
}
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
int i = 0;
while (user->friends[i] != NULL) {
printf("%s\n", user->friends[i]->name);
i++;
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *curr_post = user->first_post;
while (curr_post != NULL) {
printf("From: %s\n", curr_post->author);
// use ctime to print date in readable format
printf("Date: %s\n", ctime(curr_post->date));
printf("%s\n", curr_post->contents);
// if there is another post, print formatting to match sample_output.txt
if (curr_post->next != NULL) {
printf("\n===\n\n");
}
curr_post = curr_post->next;
}
printf("------------------------------------------\n");
return 0;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
// if one of the pointers are null
if (author == NULL || target == NULL) {
return 2;
}
// users are not friends
if (in_friend_list(target->name, ((User *) author)->friends) == 0 || in_friend_list(author->name, target->friends) == 0) {
return 1;
}
// insert the new post in the FRONT of target's list of posts
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
fprintf(stderr, "Could not allocate space in memory.\n");
return 3;
}
new_post->contents = contents;
strncpy(new_post->author, author->name, MAX_NAME);
new_post->author[31] = '\0';
time_t *curr_date = malloc(sizeof(time_t));
// update curr_date to current time
time(curr_date);
new_post->date = curr_date;
// initialize the new_post's next attribute to be NULL
new_post->next = NULL;
// update pointers to insert new post to front of list
// THESE CAUSE SEG FAULTS
if (target->first_post == NULL) {
target->first_post = new_post;
return 0;
}
new_post->next = target->first_post->next;
target->first_post->next = 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_ptr_curr = *user_ptr_del;
if (user_ptr_curr == NULL) {
return 1;
}
if (strcmp(user_ptr_curr->name, name) == 0) {
// case: there the user we want to delete is at the head of the list
// step 2: delete it from all of its friends arrays
remove_from_all_friend_arr(user_ptr_curr);
// note: only need to remove from the friend array, since our array will be freed
// step 3: remove it from the pointer
// keep track of the user we want to deallocate, as in the next line we would lose it
User **user_to_deallocate = &user_ptr_curr;
// remove the first user from the friends list
*user_ptr_del = user_ptr_curr->next;
// step 4: free all posts on the users' timeline
// step 5: free all of its allocated memory
deallocate_user(*user_to_deallocate);
return 0;
}
// loop through the user list until either you reach the end, or find the user with the given name
while (user_ptr_curr->next != NULL && strcmp(user_ptr_curr->next->name, name) != 0) {
user_ptr_curr = user_ptr_curr->next;
}
// case: you looped through the entire user list and did not find the user with such a name
if (user_ptr_curr->next == NULL) {
return 1;
}
// otherwise, user_ptr_curr->next is the pointer to the user to be deleted.
// step 2: delete it from all of its friends arrays
remove_from_all_friend_arr(user_ptr_curr);
// step 3: remove it from the pointer
// TODO: fix this. right now it sets the head to be the user we want to delete, instead of skipping over them
// keep track of the user we want to deallocate, as in the next line we would lose it
User **user_to_deallocate = &(user_ptr_curr->next);
// skip the user we are removing in the list
user_ptr_curr->next = (user_ptr_curr->next)->next;
// step 4: free all posts on the users' timeline
// step 5: free all of its allocated memory
deallocate_user(*user_to_deallocate);
return 0;
}
| null | null |
63c648b55f004bf4898cfef5 | 2023-03-10T20:16:03.075Z | Help Write Code | v2 | null | null | null | Given a program, how would I pipe standard input into the execution of a different program and then check its exit status? |
63c648b55f004bf4898cfef5 | 2023-03-14T05:33:29.980Z | Help Write Code | v2 | null | null | null | How would I check the format of an entire line read from a file when the line is of an unknown length? (for instance, making sure you can check a line of format:
Jimmy Johnson : 55 : 20 St. George St.
but, you only have space allocated for 15 characters) |
63c648b65f004bf4898cfefa | 2023-02-10T21:40:52.380Z | General Question | v1 | how do you read from a file in c? | null | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T21:41:33.043Z | General Question | v1 | can you give me an example using fopen? | null | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:08:05.430Z | General Question | v1 | what is a makefile | null | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:08:39.130Z | General Question | v1 | how do you test using a makefile | null | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:09:43.913Z | General Question | v1 | generate a detailed documentation of `make test` with usage examples and explanations | null | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:15:49.679Z | General Question | v1 | how to compile makefile
| null | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:22:08.771Z | Question from Code | v1 | why am i getting a segmentation fault? | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel));
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
} | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:23:16.166Z | General Question | v1 | how do you allocate space on the heap
| null | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:24:17.812Z | Question from Code | v1 | where is m allocated | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel));
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
} | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:24:46.035Z | Question from Code | v1 | where is m[0] allocated | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel));
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
} | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:25:48.374Z | Question from Code | v1 | where is &m[0] allocated | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel));
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
} | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:26:27.256Z | Question from Code | v1 | how do I allocate space for the array on the heap | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel));
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
} | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:28:25.435Z | Question from Code | v1 | why is this giving me a segmentation fault | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
} | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:31:10.389Z | Question from Code | v1 | how do i make sure the array is not deallocated | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
} | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:33:49.190Z | Question from Code | v1 | why am i getting a segmentation fault? | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
} | null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:35:12.869Z | Help Fix Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
}
fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
} | get rid of segmentation fault | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:38:18.337Z | Help Write Code | v1 | null | null | null | Read in pixel array by following these instructions:
*
* 1. First, allocate space for m `struct pixel *` values, where m is the
* height of the image. Each pointer will eventually point to one row of
* pixel data.
* 2. For each pointer you just allocated, initialize it to point to
* heap-allocated space for an entire row of pixel data.
* 3. Use the given file and pixel_array_offset to initialize the actual
* struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is
* consistent with the bitmap file format.
* NOTE: We've tested this assumption on the Teaching Lab machines, but
* if you're trying to work on your own computer, we strongly recommend
* checking this assumption!
* 4. Return the address of the first `struct pixel *` you initialized. |
63c648b65f004bf4898cfefa | 2023-02-10T22:47:56.619Z | Question from Code | v1 | why am i getting a seg fault | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i], 3, 12, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
}
| null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:48:43.991Z | Question from Code | v1 | how do i return the address of the first struct pixel * | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i], 3, 12, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return &m[0];
}
| null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:50:00.419Z | Question from Code | v1 | why am i getting a seg fault | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i], 3, 12, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
| null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:50:46.515Z | Question from Code | v1 | how much memory needs to be allocated | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i], 3, 12, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
| null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:54:55.986Z | Question from Code | v1 | why am i getting a seg fault | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i], 3, height * width * 3, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
}
| null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:55:51.493Z | Question from Code | v1 | why am i getting a seg fault | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i], 3, width * 3, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
}
| null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:58:14.204Z | Question from Code | v1 | why am i getting a seg fault
| struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i][i], 3, width * 3, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
}
| null | null |
63c648b65f004bf4898cfefa | 2023-02-10T22:59:06.966Z | Question from Code | v1 | why am i getting a seg fault | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i][i], pixel_array_offset, width * 3, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
| null | null |
63c648b65f004bf4898cfefa | 2023-02-10T23:00:14.650Z | Help Fix Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(&m[i][i], pixel_array_offset, width * 3, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
| fix fread | null |
63c648b65f004bf4898cfefa | 2023-02-10T23:01:43.935Z | Help Fix Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(m[i], pixel_array_offset, width * 3, image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
| fix fread | null |
63c648b65f004bf4898cfefa | 2023-02-10T23:02:52.250Z | Help Fix Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(m[i], pixel_array_offset, sizeof(struct pixel), image);
}
// fseek(image, pixel_array_offset * 3, SEEK_SET);
return m;
}
| fix | null |
63c648b65f004bf4898cfefa | 2023-02-10T23:04:28.497Z | Help Fix Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel** m = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
m[i] = malloc(sizeof(struct pixel) * width);
fread(m[i], sizeof(struct pixel), pixel_array_offset, image);
}
return m;
}
| fix | null |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.