sizeof and character arrays?

sizeof and character arrays?

Post by lisp900 » Sat, 22 Sep 2007 14:15:44


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];

and

int i;
for (i=0; i < sizeof(s); i++) { printf("%s", s[i]);)

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];
fgets(*s,sizeof(s),fp);

Considering that later on I will want to tokenize each line and send
the values of the various tokens to different output files.

Lisp 9000
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.
 
 
 

sizeof and character arrays?

Post by Sid » Mon, 14 Jan 2008 07:44:06


The first one is definitely better. You probably want to change
sizeof(x) to (sizeof(x)/sizeof(*x)) if ou decide to make the char a
wchar later on. A better approach would be to define a macro
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.

 
 
 

sizeof and character arrays?

Post by Barry Schw » Mon, 14 Jan 2008 07:46:53


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.


Unfortunately, this invokes undefined behavior. %s reaquires the
corresponding argument to be a char*, you are providing a char. Did
you mean %c?


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.)



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.


This does not really change the method you use to read and store the
data.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.
 
 
 

sizeof and character arrays?

Post by Cumaa » Mon, 14 Jan 2008 10:01:01


Hi ,
I think instead of assigning a pointer to the first char of
each line, its better to use the traditional method of reading lines
in C.

Since the traditional method works out quicker and since its an array
that you have, its better to go on the traditional way.

cheers,
KumaarGuhan
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.
 
 
 

sizeof and character arrays?

Post by Ulrich Eck » Mon, 14 Jan 2008 10:03:54


The second way is preferable, as it allows you to refer to the size via a
name and if you need to change the size you only need to change it in one
place.


This is broken code, sorry.
1. s[n] yields a single char, but printf's '%s' directive expects a
NUL-terminated char string (or, rather, a pointer to one).
2. 'sizeof object' yields the size of the object in bytes. This has nothing
to do with the number of characters in a string, for that you need the
strlen() function.
3. If you want to look at each character in turn, you need to loop from zero
to strlen(s). Note: this assumes that 's' actually is a NUL-terminated
string.


Why not? It limits you to 80 characters per line, but using appropriate
error handling that might actually be okay. Tackle one problem at a time.
Note btw that passing an array to a function actually passes a pointer to
the first element.


This is an array of 80 pointers, what would you want to do with that? The
only thing it would be useful for would be to store 80 lines, but that's
not what you want.


This passes the first pointer in an otherwise uninitialised array to fgets()
and the size of the array in bytes as maximum number of characters to
store. Neither brings you closer to what you probably want to achieve.
Rather, if you want to read lines of varying length, you should research
memory handling, the functions you will be able to make use of are malloc,
realloc, free, strcat, any C book should actually address those somewhere.
Once you learned that, you can call fgets() multiple times to read the
whole line in chunks and append them to each other.

Good luck!

Uli
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- 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.