Thread: printing alphabet with malloc realloce
really whats problemcode:#include "stdafx.h" #include <stdlib.h> #include <stdio.h> int _tmain(int argc, _tchar* argv[]) { int i; char *pa; pa[0]='a'; for(i=1;i<26;i++){ pa= (char *) realloc (pa, i+1 * sizeof(char)); pa[i]='a'+i; } printf("%s",pa); }
you don't allocate memory pa point in first place.
will allocate space pointer - subsequently pointing anywherecode:char *pa ;
need call malloc allocate memory segment p - before
in short should :code:pa[0] = "a" ;
code:char *pa; pa= (char *) malloc (1 * sizeof(char)); pa[0] = 'a' ;
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk printing alphabet with malloc realloce
Ubuntu
Comments
Post a Comment