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
⌀ |
---|---|---|---|---|---|---|---|
63c6481e5f004bf4898cf9a0 | 2023-02-09T17:48:45.170Z | General Question | v1 | tell me about struct pixel | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-09T17:55:59.896Z | General Question | v1 | is attribute of struct a pointer | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-09T18:30:09.515Z | General Question | v1 | how to fix malloc(): corrupted top size | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-09T18:31:26.911Z | General Question | v1 | sample code to implement 2D array with malloc
| null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-09T18:33:12.401Z | Question from Code | v1 | does the code properly implement a 2D array for struct pixel | struct pixel **pixel_array = malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = malloc(width*sizeof(struct pixel));
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-09T18:34:57.788Z | Question from Code | v1 | does the code properly implement a 2D array for struct pixel and malloc space | struct pixel *pixel_array = malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = malloc(width*sizeof(struct pixel));
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:02:34.286Z | General Question | v1 | how to make 2d array of string using malloc | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:08:34.772Z | Help Fix Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = malloc(width*sizeof(struct pixel));
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
} | malloc space for 2d array of struct pixel | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:14:01.003Z | Question from Code | v1 | how should I allocate space for this code | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = malloc(width*sizeof(struct pixel));
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:18:16.098Z | Help Fix Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = (struct pixel **) malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = (struct pixel *) malloc(width*sizeof(struct pixel));
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
} | malloc space for 2d array of struct pixel | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:20:51.058Z | Explain Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = (struct pixel **) malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = (struct pixel *) malloc(width*sizeof(struct pixel));
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:32:13.034Z | Question from Code | v1 | how to fix malloc(): corrupted top size in this code | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = (struct pixel **) malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = (struct pixel *) malloc(width*sizeof(struct pixel));
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:43:53.237Z | Question from Code | v1 | how to fix malloc(): corrupted top size in this code | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = (struct pixel **) malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = (struct pixel *) malloc(width*3);
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:45:03.705Z | Question from Code | v1 | how to fix malloc(): corrupted top size in this code | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = malloc(width*3);
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:49:51.704Z | General Question | v1 | write example code of 2d array of struct pixel | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T06:55:54.106Z | Question from Code | v1 | does the code properly malloc space for 2d array of struct pixel | struct pixel *pixel_array = malloc(height*width*3);
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array; | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T07:13:05.666Z | Question from Code | v1 | is there memory leak | #include <stdio.h>
#include <stdlib.h>
struct pixel {
unsigned char blue;
unsigned char green;
unsigned char red;
};
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height);
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height);
void print_pixel(struct pixel p);
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = malloc(width*3);
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
}
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
if (sizeof(int) != 4){
perror("Size of integer in this machine does not match file detail requirement");
exit(1);
}
if (fseek(image, 10, SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(pixel_array_offset, sizeof(int), 1, image) != 1){
perror("Error when reading array starting point data");
exit(1);
}
if (fseek(image, 18, SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(width, sizeof(int), 1, image) != 1){
perror("Error when reading width data");
exit(1);
}
if (fseek(image, 22, SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(height, sizeof(int), 1, image) != 1){
perror("Error when reading height data");
exit(1);
}
}
void print_pixel(struct pixel p) {
printf("(%u, %u, %u)\n", p.blue, p.green, p.red);
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: bitmap_printer input_bmp\n");
exit(1);
}
FILE *image = fopen(argv[1], "rb");
if (image == NULL) {
fprintf(stderr, "Cannot open file\n");
exit(1);
}
// Read in bitmap file metadata
int pixel_array_offset, width, height;
read_bitmap_metadata(image, &pixel_array_offset, &width, &height);
// Print out metadata.
printf("Pixel array offset: %d\n", pixel_array_offset);
printf("Width: %d\n", width);
printf("Height: %d\n", height);
// Read in the pixel data
struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height);
// Print out some pixels from each of the image's corners.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
print_pixel(pixels[i][j]);
print_pixel(pixels[i][width - 1 - j]);
print_pixel(pixels[height - 1 - i][j]);
print_pixel(pixels[height - 1 - i][width - 1 - j]);
}
}
// Clean up: you need to do this!
if (fclose(image) != 0){
perror("Unable to close image");
return 1;
}
for (int i = 0; i < width; i++){
free(pixels[i]);
}
free(pixels);
return 0;
}
| null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T07:40:30.733Z | Question from Code | v1 | is there memory leak |
struct pixel {
unsigned char blue;
unsigned char green;
unsigned char red;
}
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height);
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = malloc(width*3);
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
}
int main{
FILE *image = fopen(argv[1], "rb");
int pixel_array_offset = 54;
int width = 200;
int height = 200;
struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height);
for (int i = 0; i < width; i++){
free(pixels[i]);
}
free(pixels);
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-10T07:43:21.062Z | Help Fix Code | v1 | null |
struct pixel {
unsigned char blue;
unsigned char green;
unsigned char red;
}
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height);
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(height*sizeof(struct pixel *));
if (pixel_array == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int i = 0; i < height; i++){
pixel_array[i] = malloc(width*3);
if (pixel_array[i] == NULL){
perror("Unable to malloc space");
exit(1);
}
for(int j = 0; j < width; j++){
if (fseek(image, (pixel_array_offset + 8 * (height * i + j)), SEEK_SET) != 0){
perror("Unable to navigate in image file");
exit(1);
}
if (fread(&(pixel_array[i][j].blue), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].green), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
if (fread(&(pixel_array[i][j].red), sizeof(int), 1, image) != 1){
perror("Error when reading pixel data");
exit(1);
}
}
}
return pixel_array;
}
int main{
FILE *image = fopen(argv[1], "rb");
int pixel_array_offset = 54;
int width = 200;
int height = 200;
struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height);
for (int i = 0; i < width; i++){
free(pixels[i]);
}
free(pixels);
} | free memory allocated to pixels | null |
63c6481e5f004bf4898cf9a0 | 2023-02-12T01:08:44.954Z | General Question | v1 | how to force printf to print to standard out | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-12T01:09:20.871Z | Question from Code | v1 | does the code print to standard output | printf(stdout, "hi"); | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-12T01:09:46.035Z | Question from Code | v1 | does the code print to standard output | printf("stdout", "hi"); | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-12T06:36:38.480Z | General Question | v1 | write example code for fopen then fclose | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-12T20:24:47.968Z | Question from Code | v1 | is k an array of int pointers or array of int | int *k[10]; | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-14T03:40:16.049Z | General Question | v1 | how to use fgets | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-14T04:55:01.308Z | General Question | v1 | how to use time function | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-14T07:55:30.056Z | General Question | v1 | when does segmentation fault occur | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-14T08:00:02.040Z | Question from Code | v1 | identify segmentation fault | int make_friends(const char *name1, const char *name2, User *head) {
User *user1;
User *user2;
User *curr = head;
while((curr != NULL) && ((user1 == NULL) || (user2 == NULL))){
if(strcmp(curr->name, name1) == 0){
user1 = curr;
}
if(strcmp(head->name, name2) == 0){
user2 = curr;
}
curr = curr->next;
}
if ((user1 == NULL) || (user2 == NULL))
{
return 4;
}
if (strcmp(name1, name2) == 0){
return 3;
}
int index1 = 0;
int index2 = 0;
while((index1 < MAX_FRIENDS) && (user1->friends[index1] != NULL)){
if (user1->friends[index1] == user2)
{
return 1;
}
index1 = index1 + 1;
}
while((index2 < MAX_FRIENDS) && (user2->friends[index2] != NULL)){
if (user2->friends[index2] == user1)
{
return 1;
}
index2 = index2 + 1;
}
if ((index1 == MAX_FRIENDS) || (index2 == MAX_FRIENDS))
{
return 2;
}
else{
user1->friends[index1] = user2;
user2->friends[index2] = user1;
return 0;
}
return -1;
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-14T20:32:19.483Z | Question from Code | v1 | what is uninitialized | int delete(User *delete_target){
// remove from other friends
int i;
int j;
i = 0;
while((i < MAX_FRIENDS) && (delete_target->friends[i] != NULL)){
User *friend = delete_target->friends[i];
j = 0;
while ((j < MAX_FRIENDS) && (strcmp(friend->friends[j]->name, delete_target->name) != 0))
{
j = j + 1;
}
if (j == MAX_FRIENDS)
{
perror("mutrual friendship status not maintained");
return 1;
}
for (int k = j + 1 ; k < MAX_FRIENDS; k++)
{
friend->friends[k-1] = friend->friends[k];
}
friend->friends[MAX_FRIENDS-1] = NULL;
i = i + 1;
}
// free posts user received
Post *curr_post = delete_target->first_post;
Post *curr_pt;
while(curr_post != NULL){
curr_pt = curr_post;
curr_post = curr_post->next;
free(curr_pt->contents);
free(curr_pt->date);
free(curr_pt);
}
// free user
free(delete_target);
return 0;
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-02-14T20:33:20.221Z | Question from Code | v1 | what variables are uninitialized before use | int delete(User *delete_target){
// remove from other friends
int i;
int j;
i = 0;
while((i < MAX_FRIENDS) && (delete_target->friends[i] != NULL)){
User *friend = delete_target->friends[i];
j = 0;
while ((j < MAX_FRIENDS) && (strcmp(friend->friends[j]->name, delete_target->name) != 0))
{
j = j + 1;
}
if (j == MAX_FRIENDS)
{
perror("mutrual friendship status not maintained");
return 1;
}
for (int k = j + 1 ; k < MAX_FRIENDS; k++)
{
friend->friends[k-1] = friend->friends[k];
}
friend->friends[MAX_FRIENDS-1] = NULL;
i = i + 1;
}
// free posts user received
Post *curr_post = delete_target->first_post;
Post *curr_pt;
while(curr_post != NULL){
curr_pt = curr_post;
curr_post = curr_post->next;
free(curr_pt->contents);
free(curr_pt->date);
free(curr_pt);
}
// free user
free(delete_target);
return 0;
} | null | null |
63c6481e5f004bf4898cf9a0 | 2023-03-08T06:25:59.912Z | General Question | v1 | how to read a line from a file | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-03-08T07:40:55.325Z | General Question | v1 | write me sample code for strtok | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-03-08T08:32:26.742Z | General Question | v1 | does malloc on struct containing pointers set pointer to null by default | null | null | null |
63c6481e5f004bf4898cf9a0 | 2023-03-09T03:39:05.430Z | Question from Code | v1 | is there segmentation fault | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pmake.h"
/******************************************************************************
* Newly defined helper functions
*****************************************************************************/
/* Makes a new Rule struct
*/
Rule *make_rule(Rule **head, char *line);
/* Link dependency and make new rules if nessecary
* note: assume no circular dependency
*/
Dependency *link_dependency(Rule **head, char *token);
/* Add action to rule
*/
void make_action(Rule *rule, char *line);
Rule *make_rule(Rule **head, char *line)
{
const char delim[3] = "\t ";
char *token;
token = strtok(line, delim);
int len = strlen(token);
// check if target is in a rule already (due to dependency making the rule beforehand)
Rule *curr;
if (*head == NULL)
{
*head = calloc(1, sizeof(Rule));
curr = *head;
}
else
{
curr = *head;
while (curr->next_rule != NULL)
{
if (strcmp(curr->target, token) == 0)
{
break;
}
curr = curr->next_rule;
}
if (strcmp(curr->target, token) != 0)
{
curr->next_rule = calloc(1, sizeof(Rule));
curr = curr->next_rule;
}
}
// assumed correct format, first two tokens are target and ':'
curr->target = malloc(sizeof(char) * (len + 1));
strncpy(curr->target, token, len);
curr->target[len] = '\0';
token = strtok(NULL, delim);
if (strcmp(token, ":") != 0)
{
fprintf(stderr, "assumption not fulfilled");
exit(1);
}
Dependency *depend_curr = curr->dependencies;
token = strtok(NULL, delim);
while (token != NULL)
{
if (curr->dependencies == NULL)
{
depend_curr = link_dependency(head, token);
curr->dependencies = depend_curr;
}
else
{
while (depend_curr->next_dep != NULL)
{
depend_curr = depend_curr->next_dep;
}
depend_curr->next_dep = link_dependency(head, token);
depend_curr = depend_curr->next_dep;
}
}
return curr;
}
void make_action(Rule *rule, char *line)
{
Action *new_action = calloc(1, sizeof(Action));
const char delim[3] = "\t ";
int count = 0;
char *token;
char line_count[MAXLINE];
strncpy(line_count, line, MAXLINE);
token = strtok(line_count, delim);
while (token != NULL)
{
count++;
token = strtok(NULL, delim);
}
if (count != 0)
{
new_action->args = malloc(sizeof(char *) * (count + 1));
token = strtok(line, delim);
int len = strlen(token);
new_action->args[0] = malloc(sizeof(char) * (len + 1));
strncpy(new_action->args[0], token, len);
(new_action->args[0])[len] = '\0';
for (int i = 1; i < count - 1; i++)
{
token = strtok(line, delim);
len = strlen(token);
new_action->args[i] = malloc(sizeof(char) * (len + 1));
strncpy(new_action->args[i], token, len);
(new_action->args[i])[len] = '\0';
}
new_action->args[count] = NULL;
}
if (rule->actions == NULL)
{
rule->actions = new_action;
}
else
{
Action *curr = rule->actions;
while (curr->next_act != NULL)
{
curr = curr->next_act;
}
curr->next_act = new_action;
}
}
Dependency *link_dependency(Rule **head, char *token)
{
Rule *curr = *head;
Dependency *depend = calloc(1, sizeof(Dependency));
// link_dependency called implies at least one rule is stored/linked in head
while (curr->next_rule != NULL)
{
if (strcmp(curr->target, token) == 0)
{
break;
}
curr = curr->next_rule;
}
if (strcmp(curr->target, token) != 0)
{
char sim_line[MAXLINE];
// assuming correct format, each target has max length MAXLINE-4
strncat(sim_line, token, (MAXLINE-4));
strcat(sim_line, " : ");
sim_line[MAXLINE - 1] = '\0';
// assume every entry is unique
curr = make_rule(head, sim_line);
}
depend->rule = curr;
return depend;
}
/* Read from the open file fp, and create the linked data structure
that represents the Makefile contained in the file.
See the top of pmake.h for the specification of Makefile contents.
*/
Rule *parse_file(FILE *fp)
{
char line[MAXLINE];
Rule *head = NULL;
Rule *curr = NULL;
while (fgets(line, MAXLINE, fp) != NULL)
{
if (is_comment_or_empty(line))
{
continue;
}
// assumed at least one rule exists and is at beginnint,
// so the if part will at least execute once before the else part meaning curr is well defined
if (line[0] != '\t')
{
curr = make_rule(&head, line);
}
else{
make_action(curr, line);
}
}
return head;
}
/******************************************************************************
* These helper functions are provided for you. Do not modify them.
*****************************************************************************/
/* Print the list of actions */
void print_actions(Action *act)
{
while (act != NULL)
{
if (act->args == NULL)
{
fprintf(stderr, "ERROR: action with NULL args\n");
act = act->next_act;
continue;
}
printf("\t");
int i = 0;
while (act->args[i] != NULL)
{
printf("%s ", act->args[i]);
i++;
}
printf("\n");
act = act->next_act;
}
}
/* Print the list of rules to stdout in makefile format. If the output
of print_rules is saved to a file, it should be possible to use it to
run make correctly.
*/
void print_rules(Rule *rules)
{
Rule *cur = rules;
while (cur != NULL)
{
if (cur->dependencies || cur->actions)
{
// Print target
printf("%s : ", cur->target);
// Print dependencies
Dependency *dep = cur->dependencies;
while (dep != NULL)
{
if (dep->rule->target == NULL)
{
fprintf(stderr, "ERROR: dependency with NULL rule\n");
}
printf("%s ", dep->rule->target);
dep = dep->next_dep;
}
printf("\n");
// Print actions
print_actions(cur->actions);
}
cur = cur->next_rule;
}
}
/* Return 1 if the line is a comment line, as defined on the assignment handout.
Return 0 otherwise.
*/
int is_comment_or_empty(const char *line)
{
for (int i = 0; i < strlen(line); i++)
{
if (line[i] == '#')
{
return 1;
}
if (line[i] != '\t' && line[i] != ' ')
{
return 0;
}
}
return 1;
}
/* Convert an array of args to a single space-separated string in buffer.
Returns buffer. Note that memory for args and buffer should be allocted
by the caller.
*/
char *args_to_string(char **args, char *buffer, int size)
{
buffer[0] = '\0';
int i = 0;
while (args[i] != NULL)
{
strncat(buffer, args[i], size - strlen(buffer));
strncat(buffer, " ", size - strlen(buffer));
i++;
}
return buffer;
}
| null | null |
63c648205f004bf4898cf9aa | 2023-01-28T15:43:41.854Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
/*
* Convert a 9 digit int to a 9 element int array.
*/
int populate_array(int sin, int *sin_array) {
if ((sin / 1000000000 == 0) | (sin / 1000000000 >= 10)){
return 1; //sin num is not 9 digit
}
else{
sin_array = malloc(sizeof(int) * 9);
for (int i = 1; i < 10; i++){
sin_array[i - 1] = sin % (10^i);
sin = sin / 10;
}
return 0;
}
} | Convert a 9 digit int to a 9 element int array. | null |
63c648225f004bf4898cf9be | 2023-01-19T04:29:52.445Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
// Read in the command-line arguments and convert the strings to doubles
double principal = strtod(argv[1], NULL);
double rate = strtod(argv[2], NULL);
// Call invest to make you more money
invest(&principal, rate);
printf("%.2f\n", principal);
return 0;
}
void invest(double *principal, double rate){
*principal = *principal + rate;
} | null | null |
63c648225f004bf4898cf9be | 2023-01-19T04:31:09.268Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
// Read in the command-line arguments and convert the strings to doubles
double principal = strtod(argv[1], NULL);
double rate = strtod(argv[2], NULL);
// Call invest to make you more money
invest(&principal, rate);
printf("%.2f\n", principal);
return 0;
}
void invest(double *principal, double rate){
*principal = *principal * rate;
} | null | null |
63c648225f004bf4898cf9be | 2023-01-23T21:00:42.031Z | Explain Code | v1 | null | char* min;
scanf("%s", min);
int min_size = strtol(min, NULL, 10);
printf("%d", min_size); | null | null |
63c648225f004bf4898cf9be | 2023-01-23T22:57:41.334Z | Explain Code | v1 | null | #include <stdio.h>
int main() {
char string[10] = "hello";
printf("%s", &string[1]);
return 0;
} | null | null |
63c648225f004bf4898cf9be | 2023-01-26T15:56:47.764Z | Explain Code | v1 | null | int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6};
// sum the result
int sum = 0;
for (int i = 0; i < 9; i++){
sum += sin_array[i];
}
if (sum % 10 != 0){
return 1;
}
return 0; | null | null |
63c648225f004bf4898cf9be | 2023-01-26T15:57:56.833Z | Help Fix Code | v1 | null | int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6};
// sum the result
int sum = 0;
for (int i = 0; i < 9; i++){
sum += sin_array[i];
}
if (sum % 10 != 0){
return 1;
}
return 0; | sum the integers in the sum_array array | null |
63c648225f004bf4898cf9be | 2023-01-26T15:59:33.967Z | Help Fix Code | v1 | null | int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6};
// sum the result
int sum = 0;
for (int i = 0; i < 9; i++){
sum += sin_array[i];
}
if (sum % 10 != 0){
return 1;
}
return 0; | sum the integers in the sin_array array | null |
63c648225f004bf4898cf9be | 2023-01-26T16:12:07.704Z | Explain Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6};
if (sin_array[0] == 0){
return 1;
}
// make the result array
int result[9];
for (int i = 0; i < 9; i++){
if (i % 2 == 0){
result[i] = sin_array[i];
}
else {
int product = sin_array[i] * 2;
// sum the digits if it is a two-digit number
if (product > 9){
result[i] = 1 + (product % 10);
}
else {
result[i] = product;
}
}
}
printf("%d", result[0]);
printf("%d", result[1]);
printf("%d", result[2]);
printf("%d", result[3]);
printf("%d", result[4]);
printf("%d", result[5]);
printf("%d", result[6]);
printf("%d", result[7]);
printf("%d", result[8]);
// sum the result
int sum;
for (int i = 0; i < 9; i++){
sum += result[i];
}
if (sum % 10 != 0){
return 1;
}
return 0;
} | null | null |
63c648225f004bf4898cf9be | 2023-01-26T16:18:34.795Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6};
if (sin_array[0] == 0){
return 1;
}
// sum the result
} | return 1 if the first element in the array is 0 | null |
63c648225f004bf4898cf9be | 2023-01-26T16:20:05.188Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6};
if (sin_array[0] == 0){
return 1;
}
// sum the result
return 0;
} | if the first element in sin_array is 0 return 1 | null |
63c648225f004bf4898cf9be | 2023-01-26T16:22:33.737Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6};
if (sin_array[0] == 0){
return 1;
}
return 0;
} | if the first element in sin_array is 0 return 1 | null |
63c648225f004bf4898cf9be | 2023-01-26T16:23:45.349Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6};
if (sin_array[0] == 0){
printf("hello");
}
return 0;
} | if the first element in sin_array is 0 print "hello" | null |
63c648225f004bf4898cf9be | 2023-01-26T16:33:59.685Z | Explain Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6};
if (sin_array[0] == 0){
return 1;
}
// make the result array
int result[9];
for (int i = 0; i < 9; i++){
if (i % 2 == 0){
result[i] = sin_array[i];
}
else {
int product = sin_array[i] * 2;
// sum the digits if it is a two-digit number
if (product > 9){
result[i] = 1 + (product % 10);
}
else {
result[i] = product;
}
}
}
// sum the result
int sum = 0;
for (int i = 0; i < 9; i++){
sum += result[i];
}
if (sum % 10 != 0){
return 1;
}
return 0;
}
| null | null |
63c648225f004bf4898cf9be | 2023-01-26T16:35:13.906Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9] = {0, 0, 0, 6, 2, 0, 7, 1, 6};
if (sin_array[0] == 0){
return 1;
}
// make the result array
int result[9];
for (int i = 0; i < 9; i++){
if (i % 2 == 0){
result[i] = sin_array[i];
}
else {
int product = sin_array[i] * 2;
// sum the digits if it is a two-digit number
if (product > 9){
result[i] = 1 + (product % 10);
}
else {
result[i] = product;
}
}
}
// sum the result
int sum = 0;
for (int i = 0; i < 9; i++){
sum += result[i];
}
if (sum % 10 != 0){
return 1;
}
return 0;
}
| the code above checks if a social insurance number is valid. it does this by first checking if the first digit is 0. if it is, the function returns 1, indicating that the SIN is invalid. if the first digit is not 0, the function proceeds to multiply every other digit by 2 and sum the digits of the resulting number if it is a two-digit number. the function then sums the result array and checks if the sum is divisible by 10. if it is, the function returns 0, indicating that the SIN is valid. if it is not, the function returns 1, indicating that the SIN is invalid. | null |
63c648225f004bf4898cf9be | 2023-01-26T18:10:07.524Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
int arr[3] = {0, 0, 0};
if (arr[0] == 0){
return 1;
}
return 0;
}
| return 1 if integer at arr[0] is 0, return 1 otherwise | null |
63c648225f004bf4898cf9be | 2023-01-26T18:26:37.624Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9];
sin_array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
return 0;
}
| make an array with 9 elements. return 0 | null |
63c648225f004bf4898cf9be | 2023-01-26T18:27:21.241Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
int sin_array[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
return 0;
}
| initialize sin_array with 9 elements. return 0 | null |
63c648225f004bf4898cf9be | 2023-01-26T18:33:15.808Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
if (11 % 10 != 0){
return 1;
}
return 0;
}
| return 1 if the remainder of 11 / 10 is not 0, return 0 otherwise | null |
63c648225f004bf4898cf9be | 2023-01-27T03:13:12.474Z | Explain Code | v1 | null | int main(int argc, char **argv) {
/* do not change this main function */
int count = strtol(argv[1], NULL, 10);
int *fib_sequence;
fib(&fib_sequence, count);
for (int i = 0; i < count; i++) {
printf("%d ", fib_sequence[i]);
}
free(fib_sequence);
return 0;
} | null | null |
63c648235f004bf4898cf9c3 | 2023-01-24T01:50:02.530Z | Question from Code | v1 | Why do I get Abort trap: 6? | #include <stdio.h>
#include <stdlib.h>
int check_permissions(char *file_perm, char *required_perm) {
// first is permissions of file, second is permissions required
for (int i = 0; i < 9; i++) {
// if the required perm char is -, don't care about the file perm char
if (required_perm[i] != '-' && file_perm[i] != required_perm[i]) {
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
int cutoff_size = strtol(argv[1], NULL, 10);
char file_perms[11];
int file_size;
char file_name[32];
// scan and ignore the first line that says total: ...
scanf("%*s %*d");
int count = 0;
while (scanf("%s %*d %*s %*s %d %*s %*s %*s %s", file_perms,
&file_size, file_name) == 3) {
// note argv[2] points to the array of required perms
if (file_size > cutoff_size &&
(argc == 2 || check_permissions(&file_perms[1], argv[2]) == 0)) {
count++;
}
}
printf("%d\n", count);
return 0;
}
| null | null |
63c648245f004bf4898cf9d2 | 2023-02-14T02:15:25.077Z | Question from Code | v1 | What will be the output of the following program fragment? | #define SUPERVISOR(regular) regular + 5
int main() {
int regular_pay = 20;
int hours_worked = 10;
printf("pay is %d\n", (hours_worked * SUPERVISOR(regular_pay)));
// rest omitted
} | null | null |
63c648245f004bf4898cf9d2 | 2023-02-14T16:50:49.744Z | General Question | v1 | Find bugs and memory leaks in these functions | null | null | null |
63c648255f004bf4898cf9dc | 2023-01-24T02:38:30.706Z | Explain Code | v1 | null | void fib(int **sequence_pt, int n) {
sequence_pt = malloc(sizeof(int) * n);
} | null | null |
63c648255f004bf4898cf9dc | 2023-01-24T02:50:07.447Z | Question from Code | v1 | line 9 causes segmentation error | void fib(int **sequence_pt, int n) {
int num = 1;
int prev = 0;
int new_num;
*sequence_pt = malloc(sizeof(int) * n);
*sequence_pt[0] = 0; // n >= 1 by assumption
for (int i = 1; i < n; i++) {
*sequence_pt[i] = num;
new_num = num + prev;
prev = num;
num = new_num;
}
} | null | null |
63c648255f004bf4898cf9dc | 2023-01-24T03:07:48.655Z | General Question | v1 | why is argv of type char ** | null | null | null |
63c648255f004bf4898cf9dc | 2023-01-30T22:58:42.839Z | General Question | v1 | variable-sized object may not be initialized | null | null | null |
63c648255f004bf4898cf9dc | 2023-02-06T18:29:09.571Z | General Question | v1 | what special cases do i need to ocnsider for linked list in C | null | null | null |
63c648275f004bf4898cf9eb | 2023-01-27T02:38:48.734Z | General Question | v1 | how to use valgrind
| null | null | null |
63c648275f004bf4898cf9eb | 2023-01-27T02:42:07.632Z | Question from Code | v1 | what does this code do
| for (int i = 0; i < 5; i++) {
printf("%d\n", i);
} | null | null |
63c648275f004bf4898cf9eb | 2023-01-27T02:59:34.133Z | General Question | v1 | what arguments to you pass strtol
| null | null | null |
63c648275f004bf4898cf9eb | 2023-02-04T21:26:34.049Z | General Question | v1 | what does the function strtok() do
| null | null | null |
63c648275f004bf4898cf9eb | 2023-02-04T21:29:58.505Z | Question from Code | v1 | What happens when the first argument to strtok is NULL
| strtok(NULL, '\n') | null | null |
63c648275f004bf4898cf9eb | 2023-02-04T21:40:33.057Z | General Question | v1 | what's the specification for fgets
| null | null | null |
63c648275f004bf4898cf9eb | 2023-02-04T21:45:58.985Z | General Question | v1 | does stdin get closed at the end of file execution | null | null | null |
63c648275f004bf4898cf9eb | 2023-02-09T16:42:51.570Z | General Question | v1 | how does fseek work
| null | null | null |
63c648275f004bf4898cf9eb | 2023-02-14T01:13:00.833Z | General Question | v1 | how can I get a readable format from time_t
| null | null | null |
63c648285f004bf4898cf9f0 | 2023-02-12T21:52:10.881Z | Question from Code | v1 | am I creating a local variable with line 19 that is not updating the list of users | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
exit(-1);
}
// set the name of the new user
strcpy(new_user->name, name);
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User *latest_user = *user_ptr_add;
// No current users
if (latest_user == NULL){
latest_user = new_user;
printf("successfully added user"); // TESTING
return 0;
}
// find the latest_user added
while(latest_user->next != NULL){
if (latest_user->name == new_user->name){ // another user already has this name
return 1;
}
latest_user = latest_user->next;
}
// found the last user
if(latest_user->name == new_user->name){
return 1;
}
latest_user->next = new_user;
printf("successfully added"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-12T21:53:08.892Z | Question from Code | v1 | how do I update the pointer to the user list instead of creating a local variable
| int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
exit(-1);
}
// set the name of the new user
strcpy(new_user->name, name);
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User *latest_user = *user_ptr_add;
// No current users
if (latest_user == NULL){
latest_user = new_user;
printf("successfully added user"); // TESTING
return 0;
}
// find the latest_user added
while(latest_user->next != NULL){
if (latest_user->name == new_user->name){ // another user already has this name
return 1;
}
latest_user = latest_user->next;
}
// found the last user
if(latest_user->name == new_user->name){
return 1;
}
latest_user->next = new_user;
printf("successfully added"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-12T21:54:00.871Z | Question from Code | v1 | how do I create a local alias to the user_ptr_list to prevent having to dereference
| int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
exit(-1);
}
// set the name of the new user
strcpy(new_user->name, name);
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User *latest_user = *user_ptr_add;
// No current users
if (latest_user == NULL){
latest_user = new_user;
printf("successfully added user"); // TESTING
return 0;
}
// find the latest_user added
while(latest_user->next != NULL){
if (latest_user->name == new_user->name){ // another user already has this name
return 1;
}
latest_user = latest_user->next;
}
// found the last user
if(latest_user->name == new_user->name){
return 1;
}
latest_user->next = new_user;
printf("successfully added"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-12T22:09:36.869Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
exit(-1);
}
// set the name of the new user
strcpy(new_user->name, name);
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User *latest_user = *user_ptr_add;
// No current users
if (latest_user == NULL){
latest_user = new_user;
printf("successfully added user"); // TESTING
return 0;
}
// find the latest_user added
while(latest_user->next != NULL){
if (latest_user->name == new_user->name){ // another user already has this name
return 1;
}
latest_user = latest_user->next;
}
// found the last user
if(latest_user->name == new_user->name){
return 1;
}
latest_user->next = new_user;
printf("successfully added"); // TESTING
return 0;
} | be able to iterate the user_ptr list and add a new user at the end of the list
| null |
63c648285f004bf4898cf9f0 | 2023-02-12T22:15:33.095Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
exit(-1);
}
// set the name of the new user
strcpy(new_user->name, name);
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User *latest_user = *user_ptr_add;
// No current users
if (latest_user == NULL){
latest_user = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while(latest_user->next != NULL){
if (strcmp(latest_user->name, new_user->name) == 0){ // another user already has this name
return 1;
}
latest_user = latest_user->next;
}
// found the last user
if(strcmp(latest_user->name, new_user->name) == 0){
return 1;
}
latest_user->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | add users to the end of user_ptr_list | null |
63c648285f004bf4898cf9f0 | 2023-02-12T22:27:42.606Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
exit(-1);
}
// set the name of the new user
strcpy(new_user->name, name);
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user_ptr = *user_ptr_add;
User *latest_user = *latest_user_ptr;
// No current users
if (latest_user == NULL){
latest_user = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while(latest_user->next != NULL){
if (strcmp(latest_user->name, new_user->name) == 0){ // another user already has this name
return 1;
}
latest_user = latest_user->next;
}
// found the last user
if(strcmp(latest_user->name, new_user->name) == 0){
return 1;
}
latest_user->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | add the new user to the end of user_ptr_add | null |
63c648285f004bf4898cf9f0 | 2023-02-12T22:29:20.244Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
exit(-1);
}
// set the name of the new user
strcpy(new_user->name, name);
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user_ptr = user_ptr_add;
User *latest_user = *latest_user_ptr;
// No current users
if (latest_user == NULL){
latest_user = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while(latest_user->next != NULL){
if (strcmp(latest_user->name, new_user->name) == 0){ // another user already has this name
return 1;
}
latest_user = latest_user->next;
}
// found the last user
if(strcmp(latest_user->name, new_user->name) == 0){
return 1;
}
latest_user->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | add the new user to the end of user_ptr_add | null |
63c648285f004bf4898cf9f0 | 2023-02-13T02:57:17.702Z | General Question | v1 | do I need to malloc character arrays within structs? | null | null | null |
63c648285f004bf4898cf9f0 | 2023-02-13T02:58:04.249Z | General Question | v1 | If I assign a struct on the heap, will I need to malloc character arrays within the struct as well ? | null | null | null |
63c648285f004bf4898cf9f0 | 2023-02-13T20:17:31.928Z | General Question | v1 | how do I iterate in a while loop, is it i++ or i+=1 | null | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T03:12:09.821Z | General Question | v1 | how do I deallocate memory in a linked list | null | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T03:55:43.657Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | add new users at the end of the list user_ptr_add | null |
63c648285f004bf4898cf9f0 | 2023-02-15T04:09:35.518Z | Question from Code | v1 | is line 9 aliasing. Does changing *latest_user also change user_ptr_add | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T05:22:36.383Z | Question from Code | v1 | is user_ptr_add changed at the end of the code | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T05:23:15.727Z | Question from Code | v1 | how is user_ptr_add changed | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T05:24:40.810Z | Question from Code | v1 | what does user_ptr_add point to by the end of the code? Is it the original value of user_ptr_add | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T05:25:25.263Z | Question from Code | v1 | why does user_ptr_add point to the last user in the code | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T05:26:42.893Z | Question from Code | v1 | what does user_ptr_add point to at the end of the code | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T05:28:56.503Z | Question from Code | v1 | have user_ptr_add point to the start of the list after adding the new user to the end of the linked list | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T06:13:34.906Z | Question from Code | v1 | is User **latest_user = user_ptr_add; an alias | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T06:14:10.320Z | Question from Code | v1 | is line 9 aliasing | int create_user(const char *name, User **user_ptr_add) {
// check if the name is longer than the length limit.
if (strlen(name) > (MAX_NAME - 1)){
return 2;
}
// find the position to add the new user with name name while checking to make sure no other
// user has that name.
User **latest_user = user_ptr_add;
// No current users
if ((*latest_user) == NULL){
// create new user using calloc - will initialise all the structs internal elements to NULL
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user) = new_user;
printf("successfully added user\n"); // TESTING
return 0;
}
// find the latest_user added
while((*latest_user)->next != NULL){
if (strcmp((*latest_user)->name, name) == 0){ // another user already has this name
return 1;
}
(*latest_user) = (*latest_user)->next;
}
// found the last user
if(strcmp((*latest_user)->name, name) == 0){ // last user has the same name
return 1;
}
// create the new user and add to the list
User *new_user = calloc(1, sizeof(User));
if (new_user == NULL) {
// calloc failed
perror("create_user calloc");
return -1;
}
// set the name of the new user
strncpy(new_user->name, name, MAX_NAME);
new_user->name[MAX_NAME - 1] = '\0';
(*latest_user)->next = new_user;
printf("successfully added\n"); // TESTING
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T06:49:49.431Z | Help Fix Code | v1 | null | Post *temp = (user_to_del->first_post)->next;
free((user_to_del->first_post)->contents);
free((user_to_del->first_post)->date);
free(user_to_del->first_post);
user_to_del->first_post = temp; | deallocate dynamically allocated memory for struct first_post and date and contents in the linked list with first struct first_post | null |
63c648285f004bf4898cf9f0 | 2023-02-15T21:09:56.390Z | Question from Code | v1 | why is there an infinite loop in my code | int delete_user(const char *name, User **user_ptr_del) {
// find the user
User *user_to_del = find_user(name, *user_ptr_del);
if (user_to_del == NULL){
// user not found
return 1;
}
printf("hello1"); // TESTING
// fflush(stdout);
// user exists
// loop through and delete the user from their friend's list
User *curr_user = *user_ptr_del;
// go through each user and delete the user from friend list
while (curr_user != NULL){
printf("hello2"); // TESTING
// fflush(stdout); // TESTING
int i;
for (i=0; i < MAX_FRIENDS; i++){
if ((curr_user->friends)[i] == user_to_del){
printf("%d", i); // TESTING
// found the user to delete in friend's list, shift the position of other friends in the list
int j;
for (j=i; j < (MAX_FRIENDS - 1); j++){
(curr_user->friends)[j] = (curr_user->friends)[j + 1];
printf("%d", j); // TESTING
}
// shift the last friend - must be NULL
(curr_user->friends)[MAX_FRIENDS - 1] = NULL;
}
}
curr_user = curr_user->next;
}
// deallocate memory associated with user's posts
Post *post = user_to_del->first_post;
while(post != NULL){
user_to_del->first_post = post->next;
free(post->date);
free(post->contents);
free(post);
post = user_to_del->first_post;
}
// find the user and delete
curr_user = *user_ptr_del;
if (curr_user == user_to_del){
// user to delete is the first user
*user_ptr_del = user_to_del->next;
free(user_to_del);
return 0;
}
while (curr_user->next != NULL){
if (curr_user->next == user_to_del){
// found user to delete
curr_user->next = user_to_del->next;
// free memory for user
free(user_to_del);
return 0;
}
}
return 0;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T21:27:53.512Z | Question from Code | v1 | why does line 24 have no effect
| int delete_user(const char *name, User **user_ptr_del) {
// find the user
User *user_to_del = find_user(name, *user_ptr_del);
if (user_to_del == NULL){
// user not found
return 1;
}
printf("hello1"); // TESTING
// fflush(stdout);
// user exists
// loop through and delete the user from their friend's list
User *curr_user = *user_ptr_del;
// go through each user and delete the user from friend list
while (curr_user != NULL){
printf("hello2"); // TESTING
// fflush(stdout); // TESTING
int i;
for (i=0; i < MAX_FRIENDS; i++){
if ((curr_user->friends)[i] == user_to_del){
printf("i : %d", i); // TESTING
// found the user to delete in friend's list, shift the position of other friends in the list
for (i; i < (MAX_FRIENDS - 1); i++){
(curr_user->friends)[i] = (curr_user->friends)[i + 1];
printf("j : %d", i); // TESTING
}
// shift the last friend - must be NULL
(curr_user->friends)[MAX_FRIENDS - 1] = NULL;
}
}
curr_user = curr_user->next;
}
// deallocate memory associated with user's posts
Post *post = user_to_del->first_post;
while(post != NULL){
user_to_del->first_post = post->next;
free(post->date);
free(post->contents);
free(post);
post = user_to_del->first_post;
}
// find the user and delete
curr_user = *user_ptr_del;
if (curr_user == user_to_del){
// user to delete is the first user
*user_ptr_del = user_to_del->next;
free(user_to_del);
return 0;
}
while (curr_user->next != NULL){
if (curr_user->next == user_to_del){
// found user to delete
curr_user->next = user_to_del->next;
// free memory for user
free(user_to_del);
return 0;
}
}
return 0;
}
| null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T21:35:29.721Z | Question from Code | v1 | is the code stuck anywhere | while (curr_user != NULL){
printf("hello2"); // TESTING
// fflush(stdout); // TESTING
int i;
for (i=0; i < MAX_FRIENDS; i++){
if ((curr_user->friends)[i] == user_to_del){
printf("i : %d", i); // TESTING
// found the user to delete in friend's list, shift the position of other friends in the list
int j = i;
for (j = i; j < (MAX_FRIENDS - 1); j++){
(curr_user->friends)[j] = (curr_user->friends)[j + 1];
printf("j : %d", j); // TESTING
}
// shift the last friend - must be NULL
(curr_user->friends)[MAX_FRIENDS - 1] = NULL;
i = MAX_FRIENDS ; // breaks the for_loop because already deleted user from friend list
}
}
curr_user = curr_user->next;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-02-15T21:37:06.791Z | Question from Code | v1 | is curr_user updated in the while_loop | while (curr_user != NULL){
printf("hello2"); // TESTING
// fflush(stdout); // TESTING
int i;
for (i=0; i < MAX_FRIENDS; i++){
if ((curr_user->friends)[i] == user_to_del){
printf("i : %d", i); // TESTING
// found the user to delete in friend's list, shift the position of other friends in the list
int j = i;
for (j = i; j < (MAX_FRIENDS - 1); j++){
(curr_user->friends)[j] = (curr_user->friends)[j + 1];
printf("j : %d", j); // TESTING
}
// shift the last friend - must be NULL
(curr_user->friends)[MAX_FRIENDS - 1] = NULL;
i = MAX_FRIENDS ; // breaks the for_loop because already deleted user from friend list
}
}
curr_user = curr_user->next;
} | null | null |
63c648285f004bf4898cf9f0 | 2023-03-15T21:23:11.684Z | General Question | v2 | how do I convert a newline character in a string to a null terminator | null | null | null |
63c648285f004bf4898cf9f0 | 2023-03-16T03:23:37.686Z | General Question | v2 | How do I use strtok | null | null | null |
63c648285f004bf4898cf9f0 | 2023-03-16T16:11:42.943Z | General Question | v2 | how do I use if else if I have more code to execute regardless of which of the if or else statement is executed | 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.