Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Three stupid C questions: ++,...

Reply
Thread Tools

Three stupid C questions: ++,...

 
 
jean francois
Guest
Posts: n/a
 
      06-11-2012
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.
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      06-11-2012


"jean francois" <(E-Mail Removed)> wrote in message
news:jr5lcs$4jr$(E-Mail Removed)...

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


--
Bartc

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-11-2012
jean francois <(E-Mail Removed)> writes:

> 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>
--
Ben.
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      06-11-2012
In <jr5lcs$4jr$(E-Mail Removed)> jean francois <(E-Mail Removed)> writes:

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

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-11-2012
On Mon, 11 Jun 2012 20:45:16 +0000 (UTC), jean francois
<(E-Mail Removed)> wrote:

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

--
Remove del for email
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      06-11-2012
In <jr5pn5$ebm$(E-Mail Removed)> John Gordon <(E-Mail Removed)> writes:

> > 'if !(a = b)' means


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


Ignore my answer to this one; Ben is right. It's a syntax error.

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      06-11-2012
On Mon, 11 Jun 2012 20:45:16 +0000 (UTC), jean francois
<(E-Mail Removed)> wrote:

> 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-ma...-c-manual.html

http://www.cprogramming.com/reference/
 
Reply With Quote
 
MarkBluemel
Guest
Posts: n/a
 
      06-12-2012
On 06/11/2012 09:45 PM, jean francois wrote:
> 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.
 
Reply With Quote
 
Nomen Nescio
Guest
Posts: n/a
 
      06-12-2012
jean francois <(E-Mail Removed)> wrote:

> 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!

 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      06-12-2012
On Monday, June 11, 2012 9:45:16 PM UTC+1, jean francois wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Three stupid C questions Jean-Claude ROVIER C Programming 16 09-03-2010 06:59 AM
Re: Three Mobile --> Skype on three (Non-three [Symbian - Nokia] handsets) Harry Stottle UK VOIP 0 01-05-2010 08:59 AM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
Stupid is as Stupid Does! Michael P Gabriel Digital Photography 3 06-26-2004 12:49 PM



Advertisments