D
dgoodmaniii
+AMDG
This may be a stupid question, but I'm having trouble
finding an answer. Even Google is yielding precious little
information comprehensible to my beginning mind.
I am writing a function itoa(int num, char *s), which
converts an integer into a character string. I've
successfully removed all the array subscripting from it in
favor of pointers, as K&R hath commanded me. However,
there's one in the calling function that doesn't seem to
work. If I call it thus:
*******
char s[MAXLINE];
itoa(num,s);
*******
It works. If I call it thus:
*******
char *s;
itoa(num,s);
*******
It works sometimes, and sometimes it throws a segfault. I
can only assume that this is due to memory allocation; it
works when I'm lucky enough to have the (s+i) characters
assigned to unallocated memory, and it doesn't when I'm not.
Is this assumption correct?
Is there a way to do this where the length of s isn't
limited to some arbitrary value? Or is this what all that
mysterious malloc() stuff is about, and I just need to wait
until later in the book?
Thanks.
This may be a stupid question, but I'm having trouble
finding an answer. Even Google is yielding precious little
information comprehensible to my beginning mind.
I am writing a function itoa(int num, char *s), which
converts an integer into a character string. I've
successfully removed all the array subscripting from it in
favor of pointers, as K&R hath commanded me. However,
there's one in the calling function that doesn't seem to
work. If I call it thus:
*******
char s[MAXLINE];
itoa(num,s);
*******
It works. If I call it thus:
*******
char *s;
itoa(num,s);
*******
It works sometimes, and sometimes it throws a segfault. I
can only assume that this is due to memory allocation; it
works when I'm lucky enough to have the (s+i) characters
assigned to unallocated memory, and it doesn't when I'm not.
Is this assumption correct?
Is there a way to do this where the length of s isn't
limited to some arbitrary value? Or is this what all that
mysterious malloc() stuff is about, and I just need to wait
until later in the book?
Thanks.