Three stupid C questions: ++,...

J

jean francois

Hi All,

I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
world.
Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
any suggestions :) ) and start learning C/C++, I need to translate a C
prog into TP.

Here are the supid questions:

1st:

I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
is.
If the program does 'MyPointer = p++' what does this mean?

1) MyPointer = p; increment p of 1;
2) MyPointer = p; Increment p of SizeOf(CStruct);
3) increment p of 1; MyPointer = p;
4) increment p op SizeOf(CStruct); MyPointer = p;
5) something else?

Second stupid question:

'if !(a = b)' means

1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
'=')
2) a=b and if b=0 then....
3) something else?

Third stupid question:

When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
couldn't find the 'dealloc' function in the prog.

If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
net...

Thanks a lot for your help, and please excuse the stupidity of my
questions.
 
B

BartC

I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
is.
If the program does 'MyPointer = p++' what does this mean?

1) MyPointer = p; increment p of 1;
2) MyPointer = p; Increment p of SizeOf(CStruct);
3) increment p of 1; MyPointer = p;
4) increment p op SizeOf(CStruct); MyPointer = p;
5) something else?

Something between (1) and (2); arithmetic on pointers is in object-size
units, not bytes.

So if Cstruct was 12 bytes, then:

p=p+1;
++p;

would both point p to the next struct (adding 12 bytes to the actual value).
Second stupid question:

'if !(a = b)' means

The syntax is wrong; probably if (!(a=b)), which assigns b to a, then
effectively does:

if (!a)

So the condition is true when a (and b) is zero.
1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
'=')
2) a=b and if b=0 then....
3) something else?

Check out if (a!=b) too; you already know that actual equality is written as
a==b.
Third stupid question:

When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
couldn't find the 'dealloc' function in the prog.

Standard C has malloc() and free(); perhaps the equivalent is _ffree()?

In any case, no automatic de-allocation is done (in C; don't know about
C++), except when the program itself terminates. (Doesn't Pascal work the
same way?)
If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
net...

comp.lang.c covers C, and comp.lang.c++ covers the other language (if you
thought C was difficult, ...).
 
B

Ben Bacarisse

jean francois said:
I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
world.
Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
any suggestions :) ) and start learning C/C++, I need to translate a C
prog into TP.

Here are the supid questions:

1st:

I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
is.

The symbols here don't mean much so I stick with the words: p is
a pointer to an object of type CStruct.
If the program does 'MyPointer = p++' what does this mean?

1) MyPointer = p; increment p of 1;
2) MyPointer = p; Increment p of SizeOf(CStruct);

(aside: it's sizeof in C and C is case sensitive)
3) increment p of 1; MyPointer = p;
4) increment p op SizeOf(CStruct); MyPointer = p;
5) something else?

MyPointer gets the prior value of p, before anything happens to it, so
that's one thing out of the way.

Both (1) and (2) have an element of truth about them, but they both
suggest a confusion that needs to be sorted out.

p++ adds one to p (i.e. writing p = p + 1 has the same effect) so (1) is
a correct statement. However, pointers in C are not simply addresses --
they have a type too -- and adding one to a pointer makes it point to
the next element of the target type in an array which is what some
people might mean when they write (2).

The key is that p will be altered so that it points to the next CStruct
in some array of CStructs.
Second stupid question:

'if !(a = b)' means

1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
'=')
2) a=b and if b=0 then....
3) something else?

(3). It's a syntax error. You might have meant:

if (!a == b)
if (a != b)
if (!(a == b))

and I don't really want to guess.
Third stupid question:

When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
couldn't find the 'dealloc' function in the prog.

As a general rule, most modern systems release all memory that a program
has used when it exits. But _fmalloc is not a "well known" function so
I can't say for sure what it does.
If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
net...

Ah. If you have a C++ program, this is not the best place to ask.
comp.lang.c++ also exists. You might also want to try
alt.comp.lang.learn.c-c++

<snip>
 
J

John Gordon

In said:
I've got p as a pointer on a CStruct. p = *CStruct.

Are you sure you meant to have that asterisk before Cstruct? Did you
mean to have an ampersand instead?
No matter was CStruct is.
If the program does 'MyPointer = p++' what does this mean?

Since the ++ comes after the p, MyPointer is set equal to p and then p is
incremented by one width of Cstruct. (assuming p is declared as
Cstruct *p).
'if !(a = b)' means

Since only one equal sign was used, it is an assignment, not a test for
equality.

a is set equal to the value of b, and as a side effect, the value of a is
returned. this value is then negated, and if the negated value is true,
the if-block is executed.

In a slightly simpler form, this is equivalent to:

a = b;
if (!a)
{
...
}

Third stupid question:
When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
couldn't find the 'dealloc' function in the prog.

All allocated memory is freed upon program exit. I don't know specifically
what _fmalloc() does, but I'd be very surprised if it did anything
different.
 
B

Barry Schwarz

Hi All,

I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
world.
Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,

Visual C++ 2010 Express is free from Microsoft. I'm sure there are
other free compilers which others may choose to mention.
any suggestions :) ) and start learning C/C++, I need to translate a C

Be aware that C and C++ are two separate languages with just enough
commonality to insure newcomers become confused. Pick one language to
learn and stick with it until you are comfortable before trying to
learn the other.
prog into TP.

Here are the supid questions:

1st:

I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
is.
If the program does 'MyPointer = p++' what does this mean?

Except for void*, pointers in C always point to an object of a
particular type (so you can access that object by dereferencing the
pointer). Regardless of the type of the object, p++ will always cause
the pointer to point to the byte immediately following the object that
was pointed to. In the mathematical (as opposed to pointer
arithmetic) sense, p++ adds sizeof(object) to p.
1) MyPointer = p; increment p of 1;
2) MyPointer = p; Increment p of SizeOf(CStruct);
3) increment p of 1; MyPointer = p;
4) increment p op SizeOf(CStruct); MyPointer = p;
5) something else?

Second stupid question:

'if !(a = b)' means

The ! operator evaluates to 1 if its operand evaluates to 0. If the
operand evaluates to any non-zero value, then the ! operator evaluates
to 0.

In your case, the operand is (a=b) which is an assignment expression.
An assignment expression evaluates to the final value of the left hand
operand (a in this case). As you note below, this probably should be
if !(a == b)
which is exactly equivalent to
if (a != b)
1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
'=')
2) a=b and if b=0 then....
3) something else?

Third stupid question:

When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
couldn't find the 'dealloc' function in the prog.

_fmalloc is not a standard C function. For the standard malloc
function, the memory remains allocated until it is explicitly freed by
calling the standard free function.

When you say "exit" above, if you mean a function other than main
returning to the function that called it, then the allocated memory is
not deallocated. The standard does not address what happens when main
exits (returns to the operating system service that invoked the
program). Most modern general purpose operating systems will clean up
any residual resources, including allocated memory, when the program
exits. I would not be surprised if this did not happen on some
imbedded systems where main is never expected to exit.
If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
net...

C++ is discussed in comp.lang.c++, down the hall, third door on the
left, just past the water cooler.
 
G

Geoff

Hi All,

I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
world.
Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
any suggestions :) ) and start learning C/C++, I need to translate a C
prog into TP.

Here are the supid questions:

1st:

I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
is.

p = *CStruct if p is a pointer to CStruct is an error.
If the program does 'MyPointer = p++' what does this mean?

1) MyPointer = p; increment p of 1;
2) MyPointer = p; Increment p of SizeOf(CStruct);
3) increment p of 1; MyPointer = p;
4) increment p op SizeOf(CStruct); MyPointer = p;
5) something else?

If p is a pointer to an object of any size, then p++ or p+1 points to
the next object of that size.

If p points to CStruct, then p++ increments p by sizeof(CStruct).
There is no SizeOf operator. It's sizeof(CStruct).

The statement MyPointer = p++; makes MyPointer point to the object
pointed to by p before p was incremented to point to the next object
of that type.
Second stupid question:

'if !(a = b)' means

1) if not a equal b (Hmmm I really don't believe this: equal is '==' not
'=')
2) a=b and if b=0 then....
3) something else?

The statement if !(anything) is a syntax error.
The assignment operator is =.
a = b makes a equal to b.

The comparison operator is ==. Comparing a to b is written a == b.
The statement if (a==b) is true when a is equal to b.
The statement if (a!=b) is true when a is not equal to b.
Third stupid question:

When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
couldn't find the 'dealloc' function in the prog.

The statement free() deallocates memory allocated by the malloc family
of functions. The opposite of _fmalloc() was _ffree().

_fmalloc() is an ancient Microsoft function for allocating FAR memory
using an ancient x86 memory model. You are looking at some VERY OLD C
code if you are seeing it used, probably MS-DOS code.
If someone knows where to find basic c++ help (*.hlp, *.doc,...) on the
net...
Some quick links found from Google:

http://msdn.microsoft.com/en-us/library/fw5abdx6.aspx

http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html

http://www.cprogramming.com/reference/
 
M

MarkBluemel

Hi All,

I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
world.

You may know what you mean by TP/Pas. I can possibly guess, but it might
have been helpful for you to be clearer. The electrons wouldn't have
cost much.
Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
any suggestions :) ) and start learning C/C++, I need to translate a C
prog into TP.

No you don't really. You need to think about a) about whether you want
to learn C or C++ and b) look at some decent reference material.

Most of what you want answered here would have been covered by a good
tutorial text.

If you are an experienced programmer and want to learn C, Kernighan and
Ritchie is a good choice. I can't comment about less experience or C++.
Here are the supid questions:

1st:

I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
is.
If the program does 'MyPointer = p++' what does this mean?

Any tutorial text would cover this.

Second stupid question:

'if !(a = b)' means

And this.
Third stupid question:

When I use a _fmalloc(size). Would that block be 'de-allocated' on exit? I
couldn't find the 'dealloc' function in the prog.

A tutorial text may give you enough knowledge to detect that this is a
non-standard function, so the behaviour is defined by the implementation
not the specification.
 
N

Nomen Nescio

jean francois said:
Hi All,

I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
world.
Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
any suggestions :)

Borland has free older copies of their C/C++. Since you are targeting older
platforms you can use these and they should be ok or even good. And they are
free. Search for Borland Museum..

DigitalMars.com has C/C++ for those platforms as well, is free, and is
written by one of the top compiler writers of all time. Still maintained!

) and start learning C/C++, I need to translate a C
prog into TP.

Better than the other way around!
 
G

Guest

I'm really sorry for the dumbness of my questions but I'm from the TP/Pas
world.
Before I get enough money to buy to good C/C++ compiler (for DOS/Win/NT,
any suggestions :) ) and start learning C/C++, I need to translate a C
prog into TP. [...]
I've got p as a pointer on a CStruct. p = *CStruct. No matter was CStruct
is.
If the program does 'MyPointer = p++' what does this mean?

as a general rule when asking a question about C (or any other programming language) it is best to post small complete *correct* fragments of code.

I'm not sure if CStruct is part of your code or not in
"I've got p as a pointer on a CStruct. p = *CStruct."
is CStruct a type (ie. a struct) or a variable?

<snip>
 
J

James Kuyper

On 06/12/2012 08:16 AM, (e-mail address removed) wrote:
....
as a general rule when asking a question about C (or any other programming language) it is best to post small complete *correct* fragments of code.

Even if the question is "Is this correct?" :)

That exception doesn't apply in this case, but it's still quite clear
that the OP doesn't know C well enough to distinguish correct code and
incorrect code.
 
F

Fred K

The symbols here don't mean much so I stick with the words: p is
a pointer to an object of type CStruct.


(aside: it's sizeof in C and C is case sensitive)


MyPointer gets the prior value of p, before anything happens to it, so
that's one thing out of the way.

Both (1) and (2) have an element of truth about them, but they both
suggest a confusion that needs to be sorted out.

p++ adds one to p (i.e. writing p = p + 1 has the same effect) so (1) is
a correct statement. However, pointers in C are not simply addresses --
they have a type too -- and adding one to a pointer makes it point to
the next element of the target type in an array which is what some
people might mean when they write (2).

The key is that p will be altered so that it points to the next CStruct
in some array of CStructs.


(3). It's a syntax error. You might have meant:

if (!a == b)
if (a != b)
if (!(a == b))

and I don't really want to guess.


Why is this a syntax error? Is the following a syntax error?
int c = (a=b);
if ( !(c) ) {...}
 
J

James Kuyper

Why is this a syntax error?

Because the only things allowed between the keyword "if" and the
condition associated with the "if" are a mandatory '(' and optional
whitespace, including comments. "!" is not allowed.
Is the following a syntax error?
int c = (a=b);
if ( !(c) ) {...}

No, but

if !(c) { ... }

is a syntax error, of exactly the same type as

if !(a=b) { ... }
 
E

Eric Sosman

Why is this a syntax error? Is the following a syntax error?
int c = (a=b);
if ( !(c) ) {...}

Expanding on the explanations by James Kuyper and John Gordon:
The parentheses are part of the syntax of the `if' statement itself,
not just incidental parts of the expression being tested. Even if
the expression is just a variable used as a Boolean, you must write

if ( varname )
and not
if varname

Similarly, parentheses are part of the required syntax of `while'
and `do...while' and `for' and `switch': All of these involve an
expression (or more than one), *plus* parentheses.

This may at first seem weird, but in fact it's essential: It
removes an ambiguity. If parentheses were not required you might
write

if varname
*ptr = 42;

See the problem? No? Try putting it all on one line

if varname * ptr = 42 ;

Now, what's the expression being tested? Is it `varname', or the
multiplication `varname * ptr', or the assignment `varname * ptr = 42'?
Where does the test expression end, and where does the governed stuff
begin? (True, some of the possible parses would run afoul of other
constraints, but it makes for easier parsing if the compiler can figure
deduce the syntactic structure sooner rather than later.)

Some languages avoid this difficulty by using something else as
a separator, for example

if varname then * ptr = 42 ;
^^^^

.... and there are other ways out of the trouble, too. But C's way is
to insist that the test expression (or "expressions," for `for') be
surrounded by parentheses

if ( varname ) * ptr = 42 ;

if ( (a + b) * c > (d + e) / f ) ...
 
F

Fred K

Because the only things allowed between the keyword "if" and the
condition associated with the "if" are a mandatory '(' and optional
whitespace, including comments. "!" is not allowed.


No, but

if !(c) { ... }

is a syntax error, of exactly the same type as

if !(a=b) { ... }

Ah - that's what happens when one doesn't look closely at the original, and the original isn't actually the *code* being used.
I assumed that the OP's "if" was a part of his question, not part of his code. That is, the OP was asking:
" if this code means ..."
where "this code" was " !(a=b) "
 
J

jean francois

[...]
Second stupid question:

'if !(a = b)' means

1) if not a equal b (Hmmm I really don't believe this: equal is '=='
not '=')
2) a=b and if b=0 then....
3) something else?

(3). It's a syntax error. You might have meant:

if (!a == b)
if (a != b)
if (!(a == b))

and I don't really want to guess.


Why is this a syntax error? Is the following a syntax error?
int c = (a=b);
if ( !(c) ) {...}

Expanding on the explanations by James Kuyper and John Gordon:
The parentheses are part of the syntax of the `if' statement itself, not
just incidental parts of the expression being tested. Even if the
expression is just a variable used as a Boolean, you must write

if ( varname )
and not
if varname

Similarly, parentheses are part of the required syntax of `while' and
`do...while' and `for' and `switch': All of these involve an expression
(or more than one), *plus* parentheses.

This may at first seem weird, but in fact it's essential: It
removes an ambiguity.

I see . . . so in effect, if() is really a special kind of fonction ?

Thanks for all of the helpful responses.
 
J

James Kuyper

I see . . . so in effect, if() is really a special kind of fonction ?

No. The only thing that if(), while(), and for() have in common with
function calls is their use of parentheses; what they do with those
parentheses is pretty much completely unrelated to what function calls
do with them.
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top