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

Pages: 1-

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 2:27

>>1
Please refrain from saying words like newfag. This isn't 4chan. Some people might get offended.

Name: Anonymous 2013-07-30 2:28

I would suggest changing ``newfag'' to ``newfang'' as in newfangled.

Name: Anonymous 2013-07-30 2:28

Also >>1-2 are nigger faggot jews.

Name: Anonymous 2013-07-30 2:28

Yes, or as I like to think of it, growing a newfang to take a bite out of programming!

Name: Anonymous 2013-07-30 2:42

>>1
1. Look up strcmp to fix this abomination.
2. Too new for malloc, malloc use is something you plan out in advance.

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

>>6
something like this?



#include <stdio.h>
#include <stdlib.h>
#include <string.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;
    char* yes = "y";
    while (done == 0){
        char *answer = (char*) malloc(2);
        char *sure = (char*)malloc(2);
        printf("\nWant some nodes? y/n\t");
        fgets(answer, sizeof(answer), stdin);
        if (strcmp(answer, yes)){
            char *info = (char*) malloc(51);
            printf("\ngimme yo data:\t");
            fgets(info, sizeof(info),stdin);
            append_node(info);
        }
        printf("\nWanna print dat?  y/n\t");
        fgets(sure, sizeof(sure), stdin);
        if (strcmp(sure, yes)){
            print_node();
        }else if(!(strcmp(answer, yes))){done=1;}
   
    }
    printf("\nkthxbai\n\a");
    return 0;
}

Name: Anonymous 2013-07-30 2:47

>>7
Getting warmer.

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

>>8
Still doesn't work :(

Name: Anonymous 2013-07-30 3:04

>>9
strcmp behaves a bit differently.

Think of strcmp(a,b) like a - b.

if a == b, then a - b = 0.
if a > b, then a - b > 0
if a < b, then a - b < 0.

so if you wanted to see if two strings were the same, you would use strcmp(a,b) == 0. Or as some people write, !strcmp(a,b)

Name: Le C Newfag !!lnrLUupipROnYLq 2013-07-30 3:11

>>10

So, this

#include <stdio.h>
#include <stdlib.h>
#include <string.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;
    char* yes = "y\n";
    while (done == 0){
        char *answer = (char*) malloc(2);
        char *sure = (char*)malloc(2);
        printf("\nWant some nodes? y/n\t");
        fgets(answer, sizeof(answer), stdin);
        printf("%s", answer);
        if ((strcmp(answer, yes))==0){
            char *info = (char*) malloc(51);
            printf("\ngimme yo data:\t");
            fgets(info, sizeof(info),stdin);
            printf("%s", info);
            append_node(info);
        }
        printf("\nWanna print dat?  y/n\t");
        fgets(sure, sizeof(sure), stdin);
        printf("%s", sure);
        if (strcmp(sure, yes) == 0){
            print_node();
        }else {
            if (0 != strcmp(answer, yes)){
            done=1;
            }
        }
   
    }
    printf("\nkthxbai\n\a");
    return 0;
}


Works (the \n part was hard to guess), but now i get a segfault at
for (iter = first; iter->next == NULL; iter = iter->next){
            printf ("Can you see it?\n\a");
        }

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);

Name: Le C Newfag !!lnrLUupipROnYLq 2013-07-30 3:25

>>12
I get a valid pointer, so the data is being allocated (although i'm not checking malloc's return, i assume 4GB of ram should not be sparse). So it must be either the for per se, or the next instruction, but i can't see the error.

Name: Anonymous 2013-07-30 3:28

>>13
Try adding fflush(NULL); after the printf. The malloc is ok.

Name: Le C Newfag !!lnrLUupipROnYLq 2013-07-30 3:46

>>14

valgrind -v --leak-check=full --show-reachable=yes ./a.out
http://pastebin.com/y4B2eUwM

Name: Anonymous 2013-07-30 4:15

>>15
That's too bad the problem isn't in the linked list. I could have lead you to the problem if it was there, but once again, the source of the issue is due to inappropriate use c syntax or its core library. So my only choice is to tell you flat out.

That fgets, using sizeof(sure) will only work if it is an array with a compile time known length.

so char sure[2]; would work.

But sure has type char*, so sizeof(sure) evaluates to the size of a pointer on your machine, which is likely 4 or 8. I'll let you link this with what you see in that valgrind report.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-07-30 6:14

while (done == 0)
*facepalm*
== 0
*facepalm*

do {
 ...
 printf("Done [y/n]? ");
} while(getchar()== 'n');

Name: Anonymous 2013-07-30 13:49

for (iter = first; iter->next == NULL; iter = iter->next){
Perhaps should be:
for (iter = first; iter->next != NULL; iter = iter->next){

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