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
⌀ |
---|---|---|---|---|---|---|---|
63c647fa5f004bf4898cf860 | 2023-02-15T07:40:32.315Z | General Question | v1 | how to use time function
| null | null | null |
63c647fa5f004bf4898cf860 | 2023-02-15T07:52:30.715Z | Question from Code | v1 | why do i get a warning for this code | strcpy(new_post->contents, contents); | null | null |
63c647fa5f004bf4898cf860 | 2023-02-15T07:53:00.303Z | Question from Code | v1 | why do i get a maybe uninitialized warning
| strcpy(new_post->contents, contents); | null | null |
63c647fa5f004bf4898cf860 | 2023-02-15T07:53:49.894Z | Question from Code | v1 | how do i fix the maybe uninitialized warning | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL) {
return 2;
}
// Can't use friends function to check because there is no head passed in. We only need to check for friendship one way as it is symmetrical.
int auth_friends_w_target = 0;
for (int i = 0; i < 10; i++) {
if (author->friends[i] == target) {
auth_friends_w_target = 1;
}
}
if (!auth_friends_w_target) {
return 1;
}
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("malloc");
exit(-1);
}
// could have errors with null terminator
strncpy(new_post->author, author->name, 31);
strcpy(new_post->contents, contents);
time_t current_time;
time_t non_readable_time = time(¤t_time);
new_post->date = &non_readable_time;
return 0;
} | null | null |
63c647fa5f004bf4898cf860 | 2023-02-15T18:16:01.874Z | General Question | v1 | how do i malloc space for size 10 array | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-25T21:32:11.879Z | General Question | v1 | how to access one char of a string when we have the pointer to that string only
| null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-25T21:34:35.584Z | Question from Code | v1 | how can i fix the error in line 3
| int test(char *a, char*b){
for(int i; i<9; i++){
if(*b[i] != "-" && *b[i] != *a[i]){
return 1;
}
}
return 0;
}
int main(int argc, char** argv){
return test(argv[1], argv[2]);
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-25T21:40:27.708Z | General Question | v1 | how to pass command line arguements to another function one by one | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:01:16.676Z | General Question | v1 | how to calculate the number of digits in an int | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:06:56.037Z | General Question | v1 | how to perform integer division in c | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:07:17.894Z | General Question | v1 | what is 9/2 in c | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:09:43.447Z | General Question | v1 | how to get the number from i-th place in an integer | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:14:44.655Z | Question from Code | v1 | what is the output of this code | #include <stdio.h>
int main() {
int number = 12345;
int i;
for (i = 0; i < 5; i++) {
printf("%d", (number / (10^i)) % 10);
}
return 0;
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:17:44.582Z | Question from Code | v1 | what is the output when this code is run with arguements sin = 123456789 and sin_array is an integer array of 9 integers | int populate_array(int sin, int *sin_array) {
int number_of_digits = 0;
while(sin != 0){
sin = sin / 10;
number_of_digits ++;
}
if(number_of_digits != 9){
return 1;
} else {
for(int i = 0; i < 9; i++){
sin_array[i] = (sin / (10^(8 - i))) % 10;
}
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:18:16.805Z | Question from Code | v1 | what is the output when this code is run with arguements sin = 012345678 and sin_array is an integer array of 9 integers | int populate_array(int sin, int *sin_array) {
int number_of_digits = 0;
while(sin != 0){
sin = sin / 10;
number_of_digits ++;
}
if(number_of_digits != 9){
return 1;
} else {
for(int i = 0; i < 9; i++){
sin_array[i] = (sin / (10^(8 - i))) % 10;
}
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:34:26.134Z | General Question | v1 | how to get the i-th digit of a 2 digit number | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:37:40.421Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 6} | int check_sin(int *sin_array) {
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}
int total = 0;
for(int i = 0; i<9; i++){
int temp = sin_array[i] * mult_array[i]
if(temp / 10 == 0){
total += temp;
} else {
int one = temp / 10;
int two = temp % 10;
total += (one * two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:38:24.961Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} | int check_sin(int *sin_array) {
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}
int total = 0;
for(int i = 0; i<9; i++){
int temp = sin_array[i] * mult_array[i]
if(temp / 10 == 0){
total += temp;
} else {
int one = temp / 10;
int two = temp % 10;
total += (one * two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:39:18.245Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total | int check_sin(int *sin_array) {
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}
int total = 0;
for(int i = 0; i<9; i++){
int temp = sin_array[i] * mult_array[i]
if(temp / 10 == 0){
total += temp;
} else {
int one = temp / 10;
int two = temp % 10;
total += (one * two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:40:47.223Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 6} and the value of total | int check_sin(int *sin_array) {
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}
int total = 0;
for(int i = 0; i<9; i++){
int temp = sin_array[i] * mult_array[i]
if(temp / 10 == 0){
total += temp;
} else {
int one = temp / 10;
int two = temp % 10;
total += (one * two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:42:51.630Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 6} and the value of total | int check_sin(int *sin_array) {
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}
int total = 0;
for(int i = 0; i<9; i++){
if((sin_array[i] * mult_array[i]) / 10 == 0){
total += (sin_array[i] * mult_array[i]);
} else {
int one = (sin_array[i] * mult_array[i]) / 10;
int two = (sin_array[i] * mult_array[i]) % 10;
total += (one * two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:45:51.260Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 6} and the value of total | int check_sin(int *sin_array) {
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}
int total = 0;
for(int i = 0; i<9; i++){
if((sin_array[i] * mult_array[i]) / 10 == 0){
total += (sin_array[i] * mult_array[i]);
} else {
int one = (sin_array[i] * mult_array[i]) / 10;
int two = (sin_array[i] * mult_array[i]) % 10;
total += (one + two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:47:10.377Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total | int check_sin(int *sin_array) {
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}
int total = 0;
for(int i = 0; i<9; i++){
if((sin_array[i] * mult_array[i]) / 10 == 0){
total += (sin_array[i] * mult_array[i]);
} else {
int one = (sin_array[i] * mult_array[i]) / 10;
int two = (sin_array[i] * mult_array[i]) % 10;
total += (one + two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:49:31.646Z | Explain Code | v1 | null | int check_sin(int *sin_array) {
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1}
int total = 0;
for(int i = 0; i<9; i++){
if((sin_array[i] * mult_array[i]) / 10 == 0){
total += (sin_array[i] * mult_array[i]);
} else {
int one = (sin_array[i] * mult_array[i]) / 10;
int two = (sin_array[i] * mult_array[i]) % 10;
total += (one + two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T22:59:33.475Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total | int check_sin(int *sin_array) {
check_sin takes an array of integers as an argument
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1};
int total = 0;
for(int i = 0; i<9; i++){
if((sin_array[i] * mult_array[i]) / 10 == 0){
total += (sin_array[i] * mult_array[i]);
} else {
int one = (sin_array[i] * mult_array[i]) / 10;
int two = (sin_array[i] * mult_array[i]) % 10;
total += (one + two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T23:00:28.109Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total. can i track every step | int check_sin(int *sin_array) {
check_sin takes an array of integers as an argument
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1};
int total = 0;
for(int i = 0; i<9; i++){
if((sin_array[i] * mult_array[i]) / 10 == 0){
total += (sin_array[i] * mult_array[i]);
} else {
int one = (sin_array[i] * mult_array[i]) / 10;
int two = (sin_array[i] * mult_array[i]) % 10;
total += (one + two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T23:01:41.994Z | Question from Code | v1 | what would the output be for sin_array = {8, 1, 0, 6, 2, 0, 7, 1, 7} and the value of total | int check_sin(int *sin_array) {
check_sin takes an array of integers as an argument
int *mult_array = {1, 2, 1, 2, 1, 2, 1, 2, 1};
int total = 0;
for(int i = 0; i<9; i++){
if((sin_array[i] * mult_array[i]) < 10 == 0){
total += (sin_array[i] * mult_array[i]);
} else {
int one = (sin_array[i] * mult_array[i]) / 10;
int two = (sin_array[i] * mult_array[i]) % 10;
total += (one + two);
}
}
if(total % 10 == 0){
return 0;
} else {
return 1;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T23:05:39.666Z | General Question | v1 | how to initialize an array of integers in c | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T23:40:41.355Z | General Question | v1 | what does populating an integer array mean | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-28T23:41:27.623Z | General Question | v1 | how do i change a global array inside another function and retain the changes | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-29T02:16:45.289Z | General Question | v1 | how to differentiate between files and directories
| null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-29T03:03:24.126Z | Question from Code | v1 | how do i change sin_array initialisation or modification so the changes made to it in populate_array are global changes so i can use the modified sin_array in check_sin | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement populate_array
/*
* Convert a 9 digit int to a 9 element int array.
*/
int populate_array(int sin, int *sin_array) {
int number_of_digits = 0;
while(sin != 0){
sin = sin / 10;
number_of_digits ++;
}
if(number_of_digits != 9){
return 1;
} else {
for(int i = 0; i < 9; i++){
sin_array[i] = (sin / (10^(8 - i))) % 10;
}
return 0;
}
}
// TODO: Implement check_sin
/*
* Return 0 if the given sin_array is a valid SIN, and 1 otherwise.
*/
int check_sin(int *sin_array) {
printf("%d", sin_array[0]);
int mult_array[9] = {1, 2, 1, 2, 1, 2, 1, 2, 1};
int total = 0;
for(int i = 0; i<9; i++){
if((sin_array[i] * mult_array[i]) / 10 == 0){
total += (sin_array[i] * mult_array[i]);
printf("%d\n", sin_array[i]*mult_array[i]);
} else {
int one = (sin_array[i] * mult_array[i]) / 10;
int two = (sin_array[i] * mult_array[i]) % 10;
total += (one + two);
printf("%d\n", sin_array[i]*mult_array[i]);
}
}
printf("%d\n", total);
if(total % 10 == 0){
return 0;
} else {
return 1;
}
}
int populate_array(int, int *);
int check_sin(int *);
int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
if(argc != 2){
return 2;
} else {
int sin_array[9];
int is_nine = populate_array(atoi(argv[1]), sin_array);
if(is_nine == 1){
printf("%s", "Invalid SIN\n");
return 1;
}
if(check_sin(sin_array) == 0){
printf("%s","Valid SIN\n");
return 0;
} else {
printf("%s","Invalid SIN\n");
return 1;
}
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-01-29T22:21:41.389Z | General Question | v1 | what is the result of atoi("012345") | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-31T19:13:11.357Z | General Question | v1 | how to make a function take a non negative int as one parameter | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-31T20:00:00.608Z | General Question | v1 | how to check if a char is equal to a ? | null | null | null |
63c647fb5f004bf4898cf865 | 2023-01-31T22:06:30.496Z | Question from Code | v1 | what is the value of greeting when the piece of code is run. | char greeting[20];
strncpy(greeting, "Top of the morning to you", sizeof(greeting) - 1); | null | null |
63c647fb5f004bf4898cf865 | 2023-01-31T22:07:04.731Z | Question from Code | v1 | what is the sizeof(greeting) | char greeting[20];
strncpy(greeting, "Top of the morning to you", sizeof(greeting) - 1); | null | null |
63c647fb5f004bf4898cf865 | 2023-01-31T22:07:31.681Z | Question from Code | v1 | what is the value of greeting when the piece of code is run | char greeting[20];
strncpy(greeting, "Top of the morning to you", 19); | null | null |
63c647fb5f004bf4898cf865 | 2023-02-08T07:11:41.645Z | General Question | v1 | how to store something read by fread() in a variable | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-08T07:21:40.783Z | General Question | v1 | how to initialise space for 10 structs at once | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:18:56.498Z | General Question | v1 | how to find the length of a string in c | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:20:10.261Z | Question from Code | v1 | what is sizeof(Kaavya) | char *name = "Kaavya" | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:20:42.831Z | Question from Code | v1 | what is sizeof(name) | char *name = "Kaavya" | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:21:11.328Z | Question from Code | v1 | what is sizeof(*name) | char *name = "Kaavya" | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:21:53.200Z | Question from Code | v1 | what is the output of sizeof(*name) | char *name = "Kaavya" | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:22:28.603Z | General Question | v1 | does something like strlen exist in c | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:22:52.971Z | Question from Code | v1 | what is strlen(name) | char *name = "Kaavya" | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:26:25.539Z | General Question | v1 | syntax of strcmp | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T20:28:34.831Z | General Question | v1 | how to check if a current item is the last node of a linked list in c | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T21:04:03.595Z | General Question | v1 | how to create an instance of am empty struct | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-12T21:05:30.037Z | General Question | v1 | syntax of strcpy
| null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T00:01:14.280Z | General Question | v1 | how to cast datatypes in c
| null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T00:04:08.509Z | General Question | v1 | how to cast (const User *head) from a (const User *) to a (User *) | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T00:19:33.750Z | Question from Code | v1 | how do i return a pointer to the value in curr in the original linked list instead of returning the address of the local variable pointer curr | User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User curr = user_head[0];
while(curr != NULL){
if(strcmp(curr.name, name) == 0){
return &curr;
} else {
curr = *(curr.next);
}
}
}
return NULL;
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T00:25:23.705Z | General Question | v1 | usage of -> in structs | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T01:22:07.133Z | Question from Code | v1 | I want to return a pointer to the original User struct curr. How do i do that | User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User curr = user_head[0];
while(curr.next != NULL){
if(strcmp(curr.name, name) == 0){
return curr;
} else {
curr = *(curr.next);
}
}
if(strcmp(curr.name, name) == 0){
return curr;
}
}
return NULL;
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T01:22:59.923Z | Question from Code | v1 | I want to return a pointer to the original User struct curr. How do i do that.
I cannot use &curr as it gives me the error that i am returning a local variable | User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User curr = user_head[0];
while(curr.next != NULL){
if(strcmp(curr.name, name) == 0){
return curr;
} else {
curr = *(curr.next);
}
}
if(strcmp(curr.name, name) == 0){
return curr;
}
}
return NULL;
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T01:23:39.509Z | Question from Code | v1 | I want to return a pointer to the original User struct curr. How do i do that. I cannot use &curr as it gives me the error that i am returning a local variable
How do I use malloc for this? | User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User curr = user_head[0];
while(curr.next != NULL){
if(strcmp(curr.name, name) == 0){
return curr;
} else {
curr = *(curr.next);
}
}
if(strcmp(curr.name, name) == 0){
return curr;
}
}
return NULL;
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T01:26:18.026Z | Help Fix Code | v1 | null | User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User curr = user_head[0];
while(curr.next != NULL){
if(strcmp(curr.name, name) == 0){
return curr;
} else {
curr = *(curr.next);
}
}
if(strcmp(curr.name, name) == 0){
return curr;
}
}
return NULL;
} | i need to return a pointer to the user (the one in curr) | null |
63c647fb5f004bf4898cf865 | 2023-02-13T01:27:07.927Z | Help Fix Code | v1 | null | User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User curr = user_head[0];
while(curr.next != NULL){
if(strcmp(curr.name, name) == 0){
return curr;
} else {
curr = *(curr.next);
}
}
if(strcmp(curr.name, name) == 0){
return curr;
}
}
return NULL;
} | i need to return a pointer to the user (the one in curr). cannot use &curr as I cannot return a local variable | null |
63c647fb5f004bf4898cf865 | 2023-02-13T01:59:19.963Z | General Question | v1 | what is the syntax for calloc and an example | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T03:33:21.989Z | General Question | v1 | is there an equivalent of count feature in C? | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T03:39:23.397Z | General Question | v1 | how to initialise a boolean value | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T03:45:48.596Z | General Question | v1 | when u initialise an array of pointers of length 32, what are all elements by default set to | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T04:12:56.792Z | General Question | v1 | how to printf a combination of strings and variables | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T04:19:23.768Z | General Question | v1 | how to read a ascii file | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T04:26:29.036Z | General Question | v1 | equivalent of break in c | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T05:52:58.993Z | Question from Code | v1 | what leads to a segmentation fault in make_post | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post;
int make_post(const User *author, User *target, char *contents) {
if((author == NULL) || (target == NULL)){
return 2;
} else {
int friends = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((author->friends)[i] == target){
friends = 1;
}
}
if(friends == 0){
return 1;
}
Post *new_post = calloc(1, sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
*(new_post->date) = time(NULL);
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T05:56:09.290Z | General Question | v1 | how to set char *contents to char *contentt; | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T06:00:48.855Z | Question from Code | v1 | what leads to a segmentation fault in make_post | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post;
int make_post(const User *author, User *target, char *contents) {
if((author == NULL) || (target == NULL)){
return 2;
} else {
int friends = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((author->friends)[i] == target){
friends = 1;
}
}
if(friends == 0){
return 1;
}
Post *new_post = calloc(1, sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
*(new_post->date) = time(NULL);
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T06:13:24.312Z | General Question | v1 | what is time(NULL) output | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T06:15:11.068Z | Question from Code | v1 | what will be the output of ctime(time_address) | time_t post_time = time(NULL);
time_t *time_address = &post_time;
| null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T06:28:45.993Z | Question from Code | v1 | why is the date not being printed properly in print_user | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post;
int make_post(const User *author, User *target, char *contents) {
if((author == NULL) || (target == NULL)){
return 2;
} else {
int friends = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((author->friends)[i] == target){
friends = 1;
}
}
if(friends == 0){
return 1;
}
Post *new_post = calloc(1, sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
time_t post_time = time(NULL);
new_post->date = &post_time;
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
}
}
int print_user(const User *user) {
if(user == NULL){
return 1;
} else {
if(user->profile_pic != NULL){
;
//display pp
}
printf("%s%s\n", "Name: ", user->name);
printf("%s\n", "------------------------------------------");
printf("%s\n", "Friends:");
for(int i = 0; i < MAX_FRIENDS; i++){
if((user->friends)[i] != NULL){
printf("%s\n", ((user->friends)[i])->name);
}
}
printf("%s\n", "------------------------------------------");
printf("%s\n", "Posts:");
if(user->first_post == NULL){
;
} else {
Post curr_post = (user->first_post)[0];
while(curr_post.next != NULL){
printf("%s%s\n", "From: ", curr_post.author);
printf("%s%s\n\n", "Date: ", ctime(curr_post.date));
printf("%s\n", curr_post.contents);
printf("\n%s\n\n", "===");
curr_post = *(curr_post.next);
}
printf("%s%s\n", "From: ", curr_post.author);
printf("%s%s\n\n", "Date: ", ctime(curr_post.date));
printf("%s\n", curr_post.contents);
}
printf("%s\n", "------------------------------------------");
return 0;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T06:37:23.095Z | General Question | v1 | how to allocate space for time(NULL)
| null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T07:11:34.653Z | General Question | v1 | fscanf vs fgets | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T18:05:51.843Z | General Question | v1 | do you have to allocate memory for individual attributes of structs as well to avoid memory leak | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T18:40:09.495Z | Question from Code | v1 | how to allocate space for each item in struct | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User; | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T18:41:11.373Z | Question from Code | v1 | do i need to allocate space for every attribute separately even if i allocate space with sizeof(User) | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User; | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T19:55:09.580Z | Question from Code | v1 | WHERE IS MEMORY LEAKING | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if(strlen(name) >= MAX_NAME){
return 2;
}
User *new_node = calloc(1, sizeof(User));
strcpy(new_node->name, name);
if(user_ptr_add[0] == NULL){
user_ptr_add[0] = new_node;
} else {
User *curr = user_ptr_add[0];
while(curr->next != NULL){
if(strcmp((*curr).name, name) == 0){
return 1;
} else {
curr = curr->next;
}
}
if(strcmp((*curr).name, name) == 0){
return 1;
} else {
curr->next = new_node;
}
}
return 0;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User *curr = user_head;
while(curr->next != NULL){
if(strcmp((*curr).name, name) == 0){
return curr;
} else {
curr = curr->next;
}
}
if(strcmp((*curr).name, name) == 0){
return curr;
}
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
User *user_curr = (User *) curr;
printf("%s\n", "User List");
if(user_curr == NULL){
;
} else {
User curr_user = user_curr[0];
while(curr_user.next != NULL){
printf("\t%s\n", curr_user.name);
curr_user = *(curr_user.next);
}
printf("\t%s\n", curr_user.name);
}
}
/*
* 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){
return 2;
} else if((fopen(filename, "r")) == NULL){
return 1;
} else {
strcpy(user->profile_pic, filename);
return 0;
}
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){
return 4;
} else if(strcmp(name1, name2) == 0){
return 3;
} else {
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == user2){
return 1;
}
}
int not_max = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == NULL){
not_max = 1;
}
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user2->friends)[i] == NULL){
not_max = 1;
}
}
if(not_max==0){
return 2;
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == NULL){
(user1->friends)[i] = user2;
break;
}
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user2->friends)[i] == NULL){
(user2->friends)[i] = user1;
break;
}
}
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;
} else {
if(user->profile_pic != NULL){
FILE *pic_file;
char line[100];
pic_file = fopen(user->profile_pic, "r");
if (pic_file != NULL){
while (fgets(line, 100, pic_file) != NULL) {
printf("%s", line);
}
fclose(pic_file);
printf("\n");
}
}
printf("%s%s\n", "Name: ", user->name);
printf("%s\n", "------------------------------------------");
printf("%s\n", "Friends:");
for(int i = 0; i < MAX_FRIENDS; i++){
if((user->friends)[i] != NULL){
printf("%s\n", ((user->friends)[i])->name);
}
}
printf("%s\n", "------------------------------------------");
printf("%s\n", "Posts:");
if(user->first_post == NULL){
;
} else {
Post *curr_post = (user->first_post);
while(curr_post->next != NULL){
printf("%s%s\n", "From: ", curr_post->author);
printf("%s%s\n", "Date: ", ctime(curr_post->date));
printf("%s\n", curr_post->contents);
printf("\n%s\n\n", "===");
curr_post = curr_post->next;
}
printf("%s%s\n", "From: ", curr_post->author);
printf("%s%s\n", "Date: ", ctime(curr_post->date));
printf("%s\n", curr_post->contents);
}
printf("%s\n", "------------------------------------------");
return 0;
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if((author == NULL) || (target == NULL)){
return 2;
} else {
int friends = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((author->friends)[i] == target){
friends = 1;
}
}
if(friends == 0){
return 1;
}
Post *new_post = calloc(1, sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
time_t *post_time = malloc(sizeof(time_t));
*post_time = time(NULL);
new_post->date = post_time;
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
}
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
if(find_user(name, user_ptr_del[0]) == NULL){
return 1;
} else {
User *user_to_delete = find_user(name, user_ptr_del[0]);
//remove as friend
for(int i = 0; i < MAX_FRIENDS; i++){
if((user_to_delete->friends)[i] != NULL){
User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]);
for(int i = 0; i < MAX_FRIENDS; i++){
if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){
friend_user->friends[i] = NULL;
}
}
}
}
//delete the user from the linked list
User *curr = user_ptr_del[0];
while((curr->next != NULL) && (curr->next) != user_to_delete){
curr = curr->next;
}
curr->next = user_to_delete->next;
//free all memory related to the user
//free all time allocs in each post (User->Post->Time)
//free all allocs for each post (User->Post)
Post *curr_post = user_to_delete->first_post;
while(curr_post != NULL){
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
free(user_to_delete);
return 0;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "friends.h"
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}
return 0;
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T19:58:10.073Z | General Question | v1 | do i have to free memory only when i malloc or in other cases too?
| null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T20:13:22.235Z | General Question | v1 | how to free a linked list example | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T20:18:56.742Z | General Question | v1 | how to free a calloc memory | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T20:19:32.738Z | Question from Code | v1 | WHERE IS MEMORY LEAKING :( | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if(strlen(name) >= MAX_NAME){
return 2;
}
User *new_node = calloc(1, sizeof(User));
strcpy(new_node->name, name);
if(user_ptr_add[0] == NULL){
user_ptr_add[0] = new_node;
} else {
User *curr = user_ptr_add[0];
while(curr->next != NULL){
if(strcmp((*curr).name, name) == 0){
return 1;
} else {
curr = curr->next;
}
}
if(strcmp((*curr).name, name) == 0){
return 1;
} else {
curr->next = new_node;
}
}
return 0;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User *curr = user_head;
while(curr->next != NULL){
if(strcmp((*curr).name, name) == 0){
return curr;
} else {
curr = curr->next;
}
}
if(strcmp((*curr).name, name) == 0){
return curr;
}
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
User *user_curr = (User *) curr;
printf("%s\n", "User List");
if(user_curr == NULL){
;
} else {
User curr_user = user_curr[0];
while(curr_user.next != NULL){
printf("\t%s\n", curr_user.name);
curr_user = *(curr_user.next);
}
printf("\t%s\n", curr_user.name);
}
}
/*
* 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){
return 2;
} else if((fopen(filename, "r")) == NULL){
return 1;
} else {
strcpy(user->profile_pic, filename);
return 0;
}
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){
return 4;
} else if(strcmp(name1, name2) == 0){
return 3;
} else {
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == user2){
return 1;
}
}
int not_max = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == NULL){
not_max = 1;
}
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user2->friends)[i] == NULL){
not_max = 1;
}
}
if(not_max==0){
return 2;
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == NULL){
(user1->friends)[i] = user2;
break;
}
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user2->friends)[i] == NULL){
(user2->friends)[i] = user1;
break;
}
}
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;
} else {
if(user->profile_pic != NULL){
FILE *pic_file;
char line[100];
pic_file = fopen(user->profile_pic, "r");
if (pic_file != NULL){
while (fgets(line, 100, pic_file) != NULL) {
printf("%s", line);
}
fclose(pic_file);
printf("\n");
}
}
printf("%s%s\n", "Name: ", user->name);
printf("%s\n", "------------------------------------------");
printf("%s\n", "Friends:");
for(int i = 0; i < MAX_FRIENDS; i++){
if((user->friends)[i] != NULL){
printf("%s\n", ((user->friends)[i])->name);
}
}
printf("%s\n", "------------------------------------------");
printf("%s\n", "Posts:");
if(user->first_post == NULL){
;
} else {
Post *curr_post = (user->first_post);
while(curr_post->next != NULL){
printf("%s%s\n", "From: ", curr_post->author);
printf("%s%s\n", "Date: ", ctime(curr_post->date));
printf("%s\n", curr_post->contents);
printf("\n%s\n\n", "===");
curr_post = curr_post->next;
}
printf("%s%s\n", "From: ", curr_post->author);
printf("%s%s\n", "Date: ", ctime(curr_post->date));
printf("%s\n", curr_post->contents);
}
printf("%s\n", "------------------------------------------");
return 0;
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if((author == NULL) || (target == NULL)){
return 2;
} else {
int friends = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((author->friends)[i] == target){
friends = 1;
}
}
if(friends == 0){
return 1;
}
Post *new_post = calloc(1, sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
time_t *post_time = malloc(sizeof(time_t));
*post_time = time(NULL);
new_post->date = post_time;
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
}
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
if(find_user(name, user_ptr_del[0]) == NULL){
return 1;
} else {
User *user_to_delete = find_user(name, user_ptr_del[0]);
//remove as friend
for(int i = 0; i < MAX_FRIENDS; i++){
if((user_to_delete->friends)[i] != NULL){
User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]);
for(int i = 0; i < MAX_FRIENDS; i++){
if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){
friend_user->friends[i] = NULL;
}
}
}
}
//delete the user from the linked list
User *curr = user_ptr_del[0];
while((curr->next != NULL) && (curr->next) != user_to_delete){
curr = curr->next;
}
curr->next = user_to_delete->next;
//free all memory related to the user
free(user_to_delete->name);
free(user_to_delete->profile_pic);
Post *curr_post = user_to_delete->first_post;
while(curr_post != NULL){
free(author);
free(curr_post->contents);
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
for(int i = 0; i < MAX_FRIENDS; i++){
free((user_to_delete->friends[i])[i]);
}
free(user_to_delete);
return 0;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T20:21:06.095Z | Question from Code | v1 | WHERE IS MEMORY LEAKING | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 if successful
* - 1 if a user by this name already exists in this list
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator)
*/
int create_user(const char *name, User **user_ptr_add) {
if(strlen(name) >= MAX_NAME){
return 2;
}
User *new_node = calloc(1, sizeof(User));
strcpy(new_node->name, name);
if(user_ptr_add[0] == NULL){
user_ptr_add[0] = new_node;
} else {
User *curr = user_ptr_add[0];
while(curr->next != NULL){
if(strcmp((*curr).name, name) == 0){
return 1;
} else {
curr = curr->next;
}
}
if(strcmp((*curr).name, name) == 0){
return 1;
} else {
curr->next = new_node;
}
}
return 0;
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
User *user_head = (User *) head;
if(user_head == NULL){
return NULL;
} else {
User *curr = user_head;
while(curr->next != NULL){
if(strcmp((*curr).name, name) == 0){
return curr;
} else {
curr = curr->next;
}
}
if(strcmp((*curr).name, name) == 0){
return curr;
}
}
return NULL;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
User *user_curr = (User *) curr;
printf("%s\n", "User List");
if(user_curr == NULL){
;
} else {
User curr_user = user_curr[0];
while(curr_user.next != NULL){
printf("\t%s\n", curr_user.name);
curr_user = *(curr_user.next);
}
printf("\t%s\n", curr_user.name);
}
}
/*
* 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){
return 2;
} else if((fopen(filename, "r")) == NULL){
return 1;
} else {
strcpy(user->profile_pic, filename);
return 0;
}
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if((find_user(name1, head) == NULL) || (find_user(name2, head) == NULL)){
return 4;
} else if(strcmp(name1, name2) == 0){
return 3;
} else {
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == user2){
return 1;
}
}
int not_max = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == NULL){
not_max = 1;
}
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user2->friends)[i] == NULL){
not_max = 1;
}
}
if(not_max==0){
return 2;
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user1->friends)[i] == NULL){
(user1->friends)[i] = user2;
break;
}
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((user2->friends)[i] == NULL){
(user2->friends)[i] = user1;
break;
}
}
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;
} else {
if(user->profile_pic != NULL){
FILE *pic_file;
char line[100];
pic_file = fopen(user->profile_pic, "r");
if (pic_file != NULL){
while (fgets(line, 100, pic_file) != NULL) {
printf("%s", line);
}
fclose(pic_file);
printf("\n");
}
}
printf("%s%s\n", "Name: ", user->name);
printf("%s\n", "------------------------------------------");
printf("%s\n", "Friends:");
for(int i = 0; i < MAX_FRIENDS; i++){
if((user->friends)[i] != NULL){
printf("%s\n", ((user->friends)[i])->name);
}
}
printf("%s\n", "------------------------------------------");
printf("%s\n", "Posts:");
if(user->first_post == NULL){
;
} else {
Post *curr_post = (user->first_post);
while(curr_post->next != NULL){
printf("%s%s\n", "From: ", curr_post->author);
printf("%s%s\n", "Date: ", ctime(curr_post->date));
printf("%s\n", curr_post->contents);
printf("\n%s\n\n", "===");
curr_post = curr_post->next;
}
printf("%s%s\n", "From: ", curr_post->author);
printf("%s%s\n", "Date: ", ctime(curr_post->date));
printf("%s\n", curr_post->contents);
}
printf("%s\n", "------------------------------------------");
return 0;
}
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if((author == NULL) || (target == NULL)){
return 2;
} else {
int friends = 0;
for(int i = 0; i < MAX_FRIENDS; i++){
if((author->friends)[i] == target){
friends = 1;
}
}
if(friends == 0){
return 1;
}
Post *new_post = calloc(1, sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
time_t *post_time = malloc(sizeof(time_t));
*post_time = time(NULL);
new_post->date = post_time;
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
}
}
/*
* From the list pointed to by *user_ptr_del, delete the user
* with the given name.
* Remove the deleted user from any lists of friends.
*
* Return:
* - 0 on success.
* - 1 if a user with this name does not exist.
*/
int delete_user(const char *name, User **user_ptr_del) {
if(find_user(name, user_ptr_del[0]) == NULL){
return 1;
} else {
User *user_to_delete = find_user(name, user_ptr_del[0]);
//remove as friend
for(int i = 0; i < MAX_FRIENDS; i++){
if((user_to_delete->friends)[i] != NULL){
User *friend_user = find_user((user_to_delete->friends)[i]->name, user_ptr_del[0]);
for(int i = 0; i < MAX_FRIENDS; i++){
if(((friend_user->friends)[i] != NULL) && (friend_user->friends)[i] == user_to_delete){
friend_user->friends[i] = NULL;
}
}
}
}
//delete the user from the linked list
User *curr = user_ptr_del[0];
while((curr->next != NULL) && (curr->next) != user_to_delete){
curr = curr->next;
}
curr->next = user_to_delete->next;
//free all memory related to the user
free(user_to_delete->name);
free(user_to_delete->profile_pic);
Post *curr_post = user_to_delete->first_post;
while(curr_post != NULL){
free(curr_post->author);
free(curr_post->contents);
free(curr_post->date);
free(curr_post);
curr_post = curr_post->next;
}
for(int i = 0; i < MAX_FRIENDS; i++){
free((user_to_delete->friends)[i]);
}
free(user_to_delete);
return 0;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T20:27:47.278Z | General Question | v1 | how to fix invalid reads | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T20:43:22.396Z | General Question | v1 | how does calloc work | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T21:03:38.114Z | Question from Code | v1 | how do i allocate space for each of the attributes of user | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User; | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T21:16:03.221Z | General Question | v1 | how to allocate space for an array of pointers to ints | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T21:26:41.771Z | General Question | v1 | syntax to calloc for a string | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T21:42:13.943Z | Question from Code | v1 | what is wrong with the memory allocations | typedef struct user {
char name[MAX_NAME];
char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents.
struct post *first_post;
struct user *friends[MAX_FRIENDS];
struct user *next;
} User;
User *new_node = calloc(1, sizeof(User));
new_node->name = calloc(1, sizeof(char) * MAX_NAME);
new_node->profile_pic = calloc(1, sizeof(char) * MAX_NAME);
new_node->first_post = calloc(1, sizeof(Post));
new_node->friends = calloc(32, sizeof(User *));
for(int i = 0; i < MAX_FRIENDS; i++){
(new_node->friends)[i] = calloc(1, sizeof(User));
}
new_node->next = calloc(1, sizeof(User)); | null | null |
63c647fb5f004bf4898cf865 | 2023-02-13T21:49:30.510Z | Question from Code | v1 | how do i allocate space for this using calloc | char *a[32]; | null | null |
63c647fb5f004bf4898cf865 | 2023-02-14T07:37:50.078Z | Question from Code | v1 | do i need to close the file here? if yes, where all do i need to close the file | int update_pic(User *user, const char *filename) {
if(strlen(filename) >= MAX_NAME){
return 2;
} else if((fopen(filename, "r")) == NULL){
return 1;
} else {
strcpy(user->profile_pic, filename);
return 0;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-14T07:46:12.616Z | General Question | v1 | how to set a string attribute of a struct to null | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-14T07:46:40.040Z | General Question | v1 | does "" mean NULL | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-14T07:49:46.543Z | General Question | v1 | how to initialise an array of 32 NULL | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-14T07:54:26.950Z | General Question | v1 | how to set a string to NULL | null | null | null |
63c647fb5f004bf4898cf865 | 2023-02-14T07:55:36.717Z | Question from Code | v1 | is a == NULL? | char *a = '\0'
| null | null |
63c647fb5f004bf4898cf865 | 2023-02-14T08:06:05.999Z | Question from Code | v1 | how to close the file in this | int update_pic(User *user, const char *filename) {
if(strlen(filename) >= MAX_NAME){
return 2;
} else if((fopen(filename, "r")) == NULL){
return 1;
} else {
strcpy(user->profile_pic, filename);
return 0;
}
} | null | null |
63c647fb5f004bf4898cf865 | 2023-02-14T08:06:48.690Z | Help Fix Code | v1 | null | int update_pic(User *user, const char *filename) {
if(strlen(filename) >= MAX_NAME){
return 2;
} else if((fopen(filename, "r")) == NULL){
return 1;
} else {
strcpy(user->profile_pic, filename);
return 0;
}
} | i want to close the file | null |
63c647fb5f004bf4898cf865 | 2023-02-14T08:07:20.833Z | General Question | v1 | what is fclose(fp) | null | null | 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.