Sunday 15 July 2012

c - Why are my pointer assignments causing program to crash -



c - Why are my pointer assignments causing program to crash -

i have written code below little programme test out before writing larger programme based on same basic principle. had working yesterday, it's getting hung morning, , can't figure out why.

the code getting hung at: new_rec->next = head;

please have @ code below, , help.

#include <stdlib.h> #include <stdio.h> #include <string.h> file *fptr; struct list { char string[256]; char *ptr; struct list *next; }; unsigned int breakremove(char string1[]); int main(void) { if ((fptr = fopen("c:\\users\\mgreene\\documents\\emailtemplates\\testlist.txt", "w")) == null) { fprintf(stderr, "error opening file."); exit(1); } int i, j, k, count=0; char ans[256]; char *pans; pans = ans; struct list *ptr = null; { puts("\nenter text: "); fgets(ans, 256, stdin); breakremove(ans); if (pans != '\0'); { count++; printf("\n%d. typed:\"%s\"", count, pans); //test right pans pointer assignment } struct list list1; struct list *head = null; struct list *new_rec = null; struct list *curr_rec = null; struct list *next_rec = null; new_rec = (struct list*)malloc(sizeof(struct list)); if (!new_rec) { puts("\nmemory allocation error"); exit(1); } puts("\nfirst memory allocation successful."); //acknowledge successful memory allocation ptr = pans; printf("\nptr = %s", ptr); //test pointer assignment printf("\npans = %s", pans); //test pointer assignment head = ptr; printf("\nhead = %s", head);// test pointer assignment printf("\nproblem new_rec->next=head."); //test isolate problem. new_rec->next = head; printf("\nnew_rec->next = ", new_rec->next); head = new_rec; curr_rec = head; while (curr_rec->next != null) { curr_rec = curr_rec->next; } puts("\nlist pointer memory allocation successful."); curr_rec->next = new_rec; new_rec->next = null; strcpy(new_rec->string, ans); printf("\n%s", curr_rec->string); if (list1.string != '\0') { fprintf(fptr, "\n%d. %s", count, curr_rec->string); } }while (*pans != '\0'); } unsigned int breakremove(char string1[]) //function removing line breaks fgets. { unsigned int lenstring; lenstring = strlen(string1); if (string1[lenstring-1]=='\n') { string1[lenstring-1]='\0'; } homecoming (unsigned char)string1; }

enable warnings in compiler. got:

../main.c:53:9: warning: assignment incompatible pointer type [enabled default] ../main.c:54:5: warning: format ‘%s’ expects argument of type ‘char *’, argument 2 has type ‘struct list *’ [-wformat] ../main.c:57:5: warning: format ‘%s’ expects argument of type ‘char *’, argument 2 has type ‘struct list *’ [-wformat] ../main.c:62:5: warning: many arguments format [-wformat-extra-args] ../main.c:44:18: warning: unused variable ‘next_rec’ [-wunused-variable] ../main.c:23:13: warning: unused variable ‘k’ [-wunused-variable] ../main.c:23:10: warning: unused variable ‘j’ [-wunused-variable] ../main.c:23:7: warning: unused variable ‘i’ [-wunused-variable] ../main.c: in function ‘breakremove’: ../main.c:93:10: warning: cast pointer integer of different size [-wpointer-to-int-cast]

fix them start. :)

for example, first warning coming from

ptr = pans;

and head. that's why programme crashes (one of reasons probably).

you have

char *pans; struct list *ptr = null;

and assign 1 other. doesn't create sense. should try/study harder, since errors many reply prepare them.

another illustration here:

if (pans != '\0') ; <-- suspicious semicolon, remove { count++; printf("\n%d. typed:\"%s\"", count, pans); //test right pans pointer assignment }

to turn warnings on in code blocks, this:

"check alternative "enable compiler warnings" in settings=>configure plugins=>compiler"

or this:

that found here.

c pointers

No comments:

Post a Comment