A few things remain unclear...

E

Eirik WS

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

Thomas Matthews

Eirik said:
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
 
E

Eirik WS

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

Artie Gold

Eirik said:
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
 
E

Eirik WS

Artie said:
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.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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...
 
C

CBFalconer

Eirik said:
<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.
 
M

Martin Dickopp

Thomas Matthews said:
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
 
M

Mark McIntyre

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}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top