473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Char arrays

A couple of questions...

Firstly, can I put an expression inside an array definition?
eg.
char ln[MAX_LINE_LEN+1]; /* MAX_LINE_LEN has been #defined */
Secondly, is the size of a char always 1 byte?

ie. To get a line from stdin, can i do this, assuming the above
definition of 'ln'?
fgets( ln, sizeof(ln), stdin );

Or do I have to do this?
fgets( ln, sizeof(ln)/sizeof(char), stdin );

Thanks.

--
David Scarlett

dscarlett@_ _ _ _ _ _ _ _
_ _ _ _ _ optusnet.com.au
Nov 14 '05 #1
2 1218
"David Scarlett" <lo**@my.signature> wrote in message
news:Xn***********************@211.29.133.50...
A couple of questions...

Firstly, can I put an expression inside an array definition?
eg.
char ln[MAX_LINE_LEN+1]; /* MAX_LINE_LEN has been #defined */
Yes, that's fine.
Secondly, is the size of a char always 1 byte?
Yes, sizeof(char) is defined to be 1 byte, but 1 byte may have more than 8
bits. See CHAR_BIT in <limits.h>.
ie. To get a line from stdin, can i do this, assuming the above
definition of 'ln'?
fgets( ln, sizeof(ln), stdin );
Yes. The brackets are superfluous when sizeof is applied to an object (as
opposed to a type).
Or do I have to do this?
fgets( ln, sizeof(ln)/sizeof(char), stdin );


Since sizeof(char) is 1, this is equivalent to the above. I prefer to avoid
using sizeof on types where possible:

type array[SIZE];
size_t elements = sizeof array / sizeof *array; /* correct for any type */

Alex
Nov 14 '05 #2
In 'comp.lang.c', David Scarlett <lo**@my.signature> wrote:
Firstly, can I put an expression inside an array definition?
eg.
char ln[MAX_LINE_LEN+1]; /* MAX_LINE_LEN has been #defined */
This is a constant expression. Yes, you can, as long as the resulting
expression fits into the implementation limits.
Secondly, is the size of a char always 1 byte?
Yes, by-definition.
ie. To get a line from stdin, can i do this, assuming the above
definition of 'ln'?
fgets( ln, sizeof(ln), stdin );
Assuming 'ln' is an array of char, yes. You also can drop the ().

fgets (ln, sizeof ln, stdin);

is the 'canonic' way.
Or do I have to do this?
fgets( ln, sizeof(ln)/sizeof(char), stdin );


You don't.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
5
by: buda | last post by:
Hi, given the following code, .... int main( void ) { char *a = { "abc", "def", "ghijkl", "o", "prs" }; // for example .... }
28
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
9
by: rob.kirkpatrick | last post by:
Hello I need to populate an array of char arrays at run-time. A very simplifed version of the code is below. char ** list should contain cnt char arrays. The values of char ** list are set by...
42
by: sarathy | last post by:
Hi, I need clarification regarding signed characters in the C language. In C, char is 1 byte. So 1. Unsigned char - ASCII CHARACTER SET - EXTENDED CHARACTER SET
33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.