On Sep 20, 10:15 pm, "lisp9...@[EMAIL PROTECTED]
" <lisp9...@[EMAIL PROTECTED]
> wrote:
> I was wondering the best way to define and loop through a character
> array. Most lines of the file I am processing are 80 characters long
> but when an error occurs in the client which created the log sometimes
> they can be much longer so what's the best way to determine the array
> and define it?
>
> char s[80];
>
> vs
>
> #define MAX 100
> char s[MAX];
The only advantage of this second approach is that it lets you collect
all the magic numbers in one place so you can update them more
easily. If you had used MAX instead of sizeof s, that would have been
a second advantage.
>
> and
>
> int i;
> for (i=0; i < sizeof(s); i++) { printf("%s", s[i]);)
Unfortunately, this invokes undefined behavior. %s reaquires the
corresponding argument to be a char*, you are providing a char. Did
you mean %c?
>
> I can read each line of my log file using fgets, now should I use
> fixed arrays like:
>
> char s[80];
> FILE *fp;
>
> fp = fopen("foo.txt","r");
> fgets(s,sizeof(s),fp);
>
> Or would it be better assign a pointer value for the first char each
> line:
>
> char *s[80];
This defines an array of 80 char*. Each element of the array contains
an indeterminate value. (If the array were declared at file scope or
with static duration, each element would contain NULL.)
> fgets(*s,sizeof(s),fp);
This invokes undefined behavior. *s is the same s s[0], the first
element of the array s. This element does not point to any memory you
can access. Therefore, fgets cannot read into this non-existent
memory.
>
> Considering that later on I will want to tokenize each line and send
> the values of the various tokens to different output files.
This does not really change the method you use to read and store the
data.
--
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.


|