arrays - C - Malloc issue (maybe something else) -


update edition:

so, i'm trying code work without using scanf/fgets. gets chars user, puts pointer array using while loop nested in loop.

 #define wordlength 15 #define maxline 1000  int main() {     char *line[maxline];     int = 0;     int j;     int n;     char c;       (n=0; c!=eof; n){         char *tmp = (char *) malloc(256);         while ((c=getchar())!=' '){             tmp[i]=c;     // no longer updating reason.              i++;             }         line[n++]=tmp; //          i=0;         printf("\n%s\n",line[n]); //seg fault here     }      for(j = 0; j (lessthan) n; j++){         printf("\n%s\n", line[j]);         free (line[j]);     }      return 0; 

so, i'm getting seg fault. not sure why tmp[i] not updating properly. still working on it.

i've never learned programming during entire semester far. please keep helping me learn. i'm loving it.

sizeof(worldlength), one, wrong. malloc takes integer, , worldlength integer. sizeof(worldlength) give size of integer, 4 if compile 32-bit system, you're allocating 4 bytes.

btw - while ((c=getchar())!=' '||c!=eof) - what's intent here? condition (a!=b || a!=c) return true if b!=c because there no way can both b , c.

and, others pointed out, you're printing out line[i], 0. meant line[n]. , don't terminate tmp string.

and there's no overflow checking, you'll run evil bugs if word longer wordlength.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -