Hello,

This again is probably a compiler issue. Your patients are very appreciated.

I have declared two seperate pointers to two seperate arrays of strucutures
on the heap. They are declared one after the other as shown at symbol (!!!)
in code below.

Am I not allowed to do this?
Please view the errors after code example.

Here is the code:
======================================ACM152.c
#include <LCD.c> //LCD FUNCTIONS

void main()
{LCD_PAINTSCREEN(1);}
============================================

=========================================LCD.c
#include <TCP.c>

void LCD_PAINTSCREEN ( int FLASHSOURCE)
{
//(!!!) Declaing two pointers (pMCB and pXXX) pointing to two arrays of
structures

struct MCB* pMCB;
pMCB = malloc(16*sizeof(struct MCB));

struct XXX* pXXX; //LINE 18 !!!!
pXXX = malloc(176*sizeof(struct XXX));

free(pMCB);
free(pXXX);
}
============================================

=========================================TCP.c
struct MCB{
int A0;
int A1;
int A2; };

struct XXX{
int B0;
int B1;
int B2;};
==================================================

Errors are:
LINE 18 (1,7): "A numeric expression must apper here"
LINE 19 (6,10): "Unidentified identifier" pXXXX
LINE 22 (6,10): "Unidentified identifier" pXXXX

I appreciate your much needed feedback!!!

--
Best regards
Robert

The declaration of a plain-C variable after a code statement. by Jeff_Relf

Jeff_Relf
Sun Dec 09 11:13:19 PST 2007

Plain C does not allow
the declaration of a variable after a code statement.
You can use brackets, like this:

main() {

struct MCB* pMCB;
pMCB = malloc(16*sizeof(struct MCB));

{ struct XXX* pXXX; // LINE 18 !!!!
pXXX = malloc(176*sizeof(struct XXX));

free(pMCB), free(pXXX); } }


RE: The declaration of a plain-C variable after a code statement. by Robby

Robby
Sun Dec 09 11:40:02 PST 2007

Hello Jeff!

Yes this is very true, and this escaped me!
I can also do it this way!

===========================================
main() {

struct MCB* pMCB;
struct XXX* pXXX;

pMCB = malloc(16*sizeof(struct MCB));
pXXX = malloc(176*sizeof(struct XXX));

free(pMCB), free(pXXX);
}
==========================================

Thanks Jeff, I appreciate your help!

--
Best regards
Robert


"Jeffâ? Relf" wrote:

> Plain C does not allow
> the declaration of a variable after a code statement.
> You can use brackets, like this:
>
> main() {
>
> struct MCB* pMCB;
> pMCB = malloc(16*sizeof(struct MCB));
>
> { struct XXX* pXXX; // LINE 18 !!!!
> pXXX = malloc(176*sizeof(struct XXX));
>
> free(pMCB), free(pXXX); } }
>
>

Re: 2 malloc's not allowed ???? by David

David
Sun Dec 09 14:28:08 PST 2007

Robby wrote:
> Hello,
>
> This again is probably a compiler issue. Your patients are very appreciated.
>
> I have declared two seperate pointers to two seperate arrays of strucutures
> on the heap. They are declared one after the other as shown at symbol (!!!)
> in code below.
>
> Am I not allowed to do this?
> Please view the errors after code example.
>
> Here is the code:
> ======================================ACM152.c
> #include <LCD.c> //LCD FUNCTIONS
>
> void main()
> {LCD_PAINTSCREEN(1);}
> ============================================
>
> =========================================LCD.c
> #include <TCP.c>
>
> void LCD_PAINTSCREEN ( int FLASHSOURCE)
> {
> //(!!!) Declaing two pointers (pMCB and pXXX) pointing to two arrays of
> structures
>
> struct MCB* pMCB;
> pMCB = malloc(16*sizeof(struct MCB));
>
> struct XXX* pXXX; //LINE 18 !!!!
> pXXX = malloc(176*sizeof(struct XXX));
>
> free(pMCB);
> free(pXXX);
> }
> ============================================
>
> =========================================TCP.c
> struct MCB{
> int A0;
> int A1;
> int A2; };
>
> struct XXX{
> int B0;
> int B1;
> int B2;};
> ==================================================
>
> Errors are:
> LINE 18 (1,7): "A numeric expression must apper here"
> LINE 19 (6,10): "Unidentified identifier" pXXXX
> LINE 22 (6,10): "Unidentified identifier" pXXXX
>
> I appreciate your much needed feedback!!!

Robby:

If you suspect your compiler, you might try your code on the online Comeau

http://www.comeaucomputing.com/tryitout/

in C89 mode. If you remember to include <stdlib.h>, you will get a very
clear error message as to what your problem is.

OTOH, if you forget to #include <stdlib.h> (as I did initially) you fall
prey to the dreaded "default int" (one of the most asinine features of C
ever devised), and get a considerable number of rather cryptic error
messages.

--
David Wilkinson
Visual C++ MVP

Re: 2 malloc's not allowed ???? by Stephen

Stephen
Mon Dec 10 14:41:48 PST 2007

It's probably because when you are using C (as opposed to C++), all your
declarations have to be at the start of the section; you can't intermingle
them with executable statements.Try it like this:

struct MCB* pMCB;
struct XXX* pXXX;
pMCB = malloc(16*sizeof(struct MCB));
pXXX = malloc(176*sizeof(struct XXX));

instead of this:

struct MCB* pMCB;
pMCB = malloc(16*sizeof(struct MCB));
struct XXX* pXXX;
pXXX = malloc(176*sizeof(struct XXX));