I believe that I am starting to understand the C language
satisfactory, but a there are a few things that I still not
understand(Feel free to direct me to a tutorial or a FAQ
if these questions are explained in a good way or are in a
FAQ somewhere):
1) What is the difference between a struct and a union?
2) What does the static keyword mean? What does it do?
3) Why is it better to say 'const char *' than 'char *'
(if it is)?
4) How can I use enums?
Thanks for any replies.
--
No matches found 9 1299
Eirik WS wrote: I believe that I am starting to understand the C language satisfactory, but a there are a few things that I still not understand(Feel free to direct me to a tutorial or a FAQ if these questions are explained in a good way or are in a FAQ somewhere):
See my signature for a list of FAQs.
1) What is the difference between a struct and a union?
In a struct each field (member) has its own storage area.
In a union, each field shares the same storage area. The
area allocated is the size of the largest field.
2) What does the static keyword mean? What does it do?
The 'static' keyword has different meanings depending
on _where_ it is used. If it is used at file scope,
i.e. not in a function, it restricts the visibility
of the variable to only within the file. When it is
used in a function or statement block, it marks the
variable as "permanent", such that the variable has
the same life-time as a global or automatic variable.
3) Why is it better to say 'const char *' than 'char *' (if it is)?
This is in the FAQ. Search for "const correctness".
There are 4 combinations:
char * -- pointer to a character variable.
const char * -- pointer to constant data.
char * const -- pointer is constant.
const char * const -- constant pointer to const data.
The "better" usage depends on the situation. Pointers
to literal should be pointers to constant data. Pointers
to strings (i.e. modifiable text), should be "char *".
If the you don't want the pointer to change, then use
a constant pointer "char * const".
4) How can I use enums?
Elaborate please.
In general, enums can be used anywhere that an integer
is used. Depending on the enum's value, it may also
be used where a char, or short is required.
Enums are a method for the compiler to generate values
for named constants. The C syntax also allows the
programmer to specify the values. Thanks for any replies.
Read the C FAQ. Also search the newsgroup for "welcome".
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book
>> 4) How can I use enums? Elaborate please. In general, enums can be used anywhere that an integer is used. Depending on the enum's value, it may also be used where a char, or short is required.
OK, what IS an enum? What makes enumerations different from
integers? Can I change the values in them, what are the syntax
when using them, et cetera. Thanks for any replies.
Eirik WS wrote: 4) How can I use enums?
Elaborate please. In general, enums can be used anywhere that an integer is used. Depending on the enum's value, it may also be used where a char, or short is required.
OK, what IS an enum? What makes enumerations different from integers? Can I change the values in them, what are the syntax when using them, et cetera.
Usenet is valuable and can complement -- but not replace -- appropriate
books on the subject.
So here's the appropriate algorithm:
Go to http://www.accu.org
Read reviews on books concerning C.
Go to library/bookstore.
Acquire/read one (or more) of said books, paying particular attention to
the exercises that are likely provided.
Write code to solve the problems in the exercises.
If you run into trouble, post back here (with a minimal compilable
snippet of code that exhibits the particular difficulty).
Rinse. Repeat. [Oh wait, that's not quite right.]
HTH,
--ag
--
Artie Gold -- Austin, Texas
"Yeah. It's an urban legend. But it's a *great* urban legend!"
Artie Gold wrote: Eirik WS wrote:4) How can I use enums?
Elaborate please. In general, enums can be used anywhere that an integer is used. Depending on the enum's value, it may also be used where a char, or short is required.
OK, what IS an enum? What makes enumerations different from integers? Can I change the values in them, what are the syntax when using them, et cetera.
Usenet is valuable and can complement -- but not replace -- appropriate books on the subject.
So here's the appropriate algorithm:
<advanced mathematical algorithm>
Yeah, I'm getting K&R 2 from my dad, but meanwhile I
have to solve my problems in other ways. HTH, --ag
In 'comp.lang.c', Eirik WS <ha************************@yahoo.no> wrote: 4) How can I use enums? Elaborate please. In general, enums can be used anywhere that an integer is used. Depending on the enum's value, it may also be used where a char, or short is required.
OK, what IS an enum? What makes enumerations different from integers? Can I change the values in them, what are the syntax when using them, et cetera.
enums are nothing but constant integer literals. There are useful to design
readable code where you can associate a list of possible values to some
variable (say, the status of a state machine, some configuration data etc.)
It is very handy to have this for example :
struct big_config
{
<...>
enum rate bitrate;
<...>
};
with :
enum rate
{
RATE_32K,
RATE_48K,
RATE_56K,
RATE_64K,
RATE_NB
};
and in some function :
int select_rate (enum rate rate)
{
int err = 0;
switch (rate)
{
case RATE_32K:
err = /* some process */
break;
case RATE_48K:
break;
case RATE_56K:
break;
case RATE_64K:
break;
default:
err = 1;
}
return err;
}
not to mention some automation with macros and clever includes...
--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Eirik WS wrote: Artie Gold wrote: Eirik WS wrote:
>4) How can I use enums?
Elaborate please.In general, enums can be used anywhere that an integer is used. Depending on the enum's value, it may also be used where a char, or short is required.
OK, what IS an enum? What makes enumerations different from integers? Can I change the values in them, what are the syntax when using them, et cetera.
Usenet is valuable and can complement -- but not replace -- appropriate books on the subject.
So here's the appropriate algorithm:
<advanced mathematical algorithm>
Yeah, I'm getting K&R 2 from my dad, but meanwhile I have to solve my problems in other ways.
Excellent start. Here is an example of an enum use:
typedef enum {FOR, WHILE, DO, OTHERS} loopers;
.....
loopers token;
.....
switch (token) {
case FOR: /* compile a for loop */
break;
case WHILE: /* compile a while loop */
break;
case DO: /* compile a do loop */
break;
default: /* do something else */
break;
}
Notice how the list of identifiers in the typedef can change
without affecting the switch code, apart from adding a suitable
case.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Thomas Matthews <Th****************************@sbcglobal.net> writes: 3) Why is it better to say 'const char *' than 'char *' (if it is)?
This is in the FAQ. Search for "const correctness". There are 4 combinations: char * -- pointer to a character variable. const char * -- pointer to constant data. char * const -- pointer is constant. const char * const -- constant pointer to const data. The "better" usage depends on the situation. Pointers to literal should be pointers to constant data. Pointers to strings (i.e. modifiable text), should be "char *".
....but only if the pointer is actually used to modify the object.
Consider the following example:
#include <stdio.h>
#include <string.h>
unsigned int my_strlen (const char *s)
{
unsigned int len = 0;
while (*s++)
++len;
return len;
}
int main (void)
{
char a [] = "I am a string.";
printf ("\"%s\" is %u characters long.\n", a, my_strlen (a));
strcpy (a, "Me too.");
printf ("\"%s\" is %u characters long.\n", a, my_strlen (a));
return 0;
}
Although `s' points to elements of a modifiable array, the array is
not modified through `s'. Therefore, it is correct (and, IMHO, good
programming style) to make `s' a pointer to `const char'.
Martin
In <Xn***************************@213.228.0.75> Emmanuel Delahaye <em**********@noos.fr> writes: In 'comp.lang.c', Eirik WS <ha************************@yahoo.no> wrote:
4) How can I use enums? Elaborate please. In general, enums can be used anywhere that an integer is used. Depending on the enum's value, it may also be used where a char, or short is required. OK, what IS an enum? What makes enumerations different from integers? Can I change the values in them, what are the syntax when using them, et cetera.
enums are nothing but constant integer literals.
Nonsense. You're confusing enumerations and enumeration constants.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
On Mon, 02 Feb 2004 17:12:23 +0100, in comp.lang.c , Eirik WS
<hx******************************@xyxaxhxoxox.no > wrote: 1) What is the difference between a struct and a union?
a struct is a new type which contains one of each of the objects you
declare in it. struct { int x; double y;} contains one int and one
double.
a union is a new type which contains one object, whose type depends on
how you access it. union {int x; double y;} contains an object into
which you can store either a double or an int, but not both at once.
Note that if you store an int, you can't safely read back a double and
vice versa.
2) What does the static keyword mean? What does it do?
At file scope it prevents the variable being visible in other files.
At function scope it specifies that the variable exists for the
duration of your program. This is handy if you want to have a state
variable within a function.
3) Why is it better to say 'const char *' than 'char *'
its not, unless you do have a const char*.
4) How can I use enums?
for enumeratable lists, such as in switches, constants etc. constants
eg enum colours{RED, BLUE, GREEN}
Thanks for any replies.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =--- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sam |
last post by:
I'm thinking about this question. I've a class A and its member
function fn. In fn there's a static variable s.
If I create an object by
A a;
When I call the function a.fn, s would remain...
|
by: fernandoronci |
last post by:
Hi,
I've been given the task of mantaining and fixing a website which I
didn't design.
I'm using Internet Explorer 5.5 and 6.x.
Specifically, the problem is that navigation menues (written in...
|
by: dee |
last post by:
When i sign on using
<authentication mode="Forms">
<forms loginUrl="LogIn.aspx"/>
</authentication>
My login code is:
If MyCheckPassword(name, pwd) Then...
|
by: Brett |
last post by:
I have a second thread in which I call t.abort() from the parent thread. I
see "t"s state as AbortRequested. It stays this way for some time. Under
which conditions will a thread remain this way?
...
|
by: Marcel Boscher |
last post by:
For now i am almost statisfied with my tsearch2 installation war over night
somehow it seems to work, finally...
3 probably easy questions remain...
1.) Is it possible to index already filled...
|
by: Steven Kalcevich |
last post by:
I am using oscommerce.com for a shopping cart program.
I tried many contribs on oscommerce's site and googled. I got focus to
the search box via this script below but when i go to a product it...
|
by: Christopher Finke |
last post by:
I am writing a Web-based text editor that will be used mainly for
editing code. To facilitate this task, whenever the Enter button is
pressed within the textarea, the event is caught and the next...
|
by: -Lost |
last post by:
In Firefox and Safari for example, if I serve my XHTML documents as
application/xml or xhtml+xml they only display the top inch or so of the
document.
In Opera it says "object has been blocked."...
|
by: neovantage |
last post by:
Hey all,
I have recently started work on div based websites as suggested the great man acoder of this community advise me to learn divs.
I made a page using div. In my page i want that content...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |