Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Newfag @ C

Name: Le C Newfag !!lnrLUupipROnYLq 2013-07-30 2:04

Hey guise, i'm having problems with the following C code:



#include <stdio.h>
#include <stdlib.h>



typedef struct nodo {
    char *data;
    struct nodo *next;
} node;

node *first = NULL;

void append_node(char *dato){
    node *new = (node*)malloc (sizeof(node));
    new->data = dato;
    new ->next = NULL;
    if (first != NULL){
        node *iter = NULL;
        for (iter = first; iter->next == NULL; iter = iter->next){
            printf ("Can you see it?\n\a");
        }
        iter->next = new;
    }else first = new;

}

void print_node(){
    node *iter = NULL;
    for (iter = first; iter->next == NULL; iter = iter->next){
        printf ("\n%s", iter->data);
    }
}

int main (int argc, char **argv){
    int done = 0;
    while (done == 0){
        char *answer = (char*) malloc(2);
        char *sure = (char*)malloc(2);
        printf("\nWant some nodes? y/n\t");
        fgets(answer, sizeof(char*) * 2, stdin);
        if (answer == "y"){
            char *info = (char*) malloc(51);
            printf("\ngimme yo data:\t");
            fgets(info, sizeof(char*) * 51,stdin);
            append_node(info);
        }
        printf("\nWanna print dat?  y/n\t");
        fgets(sure, sizeof(char*) * 2, stdin);
        if (sure == "y"){
            print_node();
        }else if(answer !="y"){done++;}
   
    }
    printf("\nkthxbai\n\a");
    return 0;
}


Compiling with gcc 4.6, i get no errors, but it throws me to "kthxbai" no matter what i answer

Name: Anonymous 2013-07-30 3:17

>>11
Try adding a print statement to the inside of the for loop. Something like:


printf("iter is %p\n", iter);

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List