how to do this in C:
if got:

myheader.h:

#define sz 5

in program if option = 2, sz for marray change to 10.

extern unsigned char marray[arrsz];

also, in C file, locations[] be the first one if option = 1 or be locations[] be the
second one if the option = 2.

   struct pt
    {
        int x,y;
    };

    static pt locations[] =
    {
        {100,200},
        {210,320}
    };

    static pt locations[] =
    {
        {100,200},
        {210,320},
        {410,120}
    };

Recommended Answers

All 5 Replies

Your questions are not really clear ...

If you need different sized arrays ... you could use dynamic allocation of memory

or just use two defines

#define SIZE_10 10
#define SIZE_20 20
// etc

re. the 2nd question ...
you can use

if( condition1IsTrue ) { /* */; )
else if( condition2IsTrue ) { /* */; )
// ...
else { /* */; )

or ...

switch( intVal )
{
case val1: /* */; break;
case val2:  /* */; break;
// 
default:  /* */;
}

using no dynamic?.

#define SIZE_10 10
#define SIZE_20 20
// etc

in program if option = 1, how to set the array size to SIZE_10 otherwise to SIZE_20?.

extern unsigned char marray[arrsz];

Why do you need to do that? Just have an array of max possible size, then have a variable, such as arrsz, that contains the size you want (either SIZE_10 or SIZE_20). It won't matter if you have extra elements in the array that aren't used.

extern unsigned char marray[arrsz];

You can't declare an array until you know how big it is. The value of arrsz has to be known before that array can be declared, and using a c++11 compliant compiler. extern assumed a global variable, so that won't work.

can i do something like this?.

static pt locations[] =
{
{100,200},
{210,320}
};
static pt locations1[] =
{
{100,200},
{210,320},
{410,120}
};

pt *p;

if( condition1IsTrue ) { /* */; )
p = locations;
else if( condition2IsTrue ) { /* */; )
    p = locations1;
else { /* */; )

This may give you some ideas ...

/*  structPoint.c */

#include <stdio.h>
#include <ctype.h> /* re. tolower */

typedef struct
{
    int x, y;
} Point ;

void printPoint( const Point* p )
{
    printf( "(%d, %d)  ", p->x, p->y ) ;
}
void printPoints( const Point ary[], int size )
{
    int i;
    for( i = 0; i < size; ++ i )
    {
        printPoint( &ary[i] ) ;
        putchar( '\n' );
    }
}

char takeInChar( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChar( "More (y/n) ? " )) == 'n' ) return 0;
    /* else ... */
    return 1;
}


int main()
{
    Point locations2[] =
    {
        {100,200},
        {210,320}
    } ;
    Point locations3[] =
    {
        {100,200},
        {210,320},
        {410,120}
    } ;


    do
    {
        switch( takeInChar( "Choose '2' or '3' (enter 2 or 3) : " ) )
        {
            case '2' : printPoints( locations2, 2 ); break;
            case '3' : printPoints( locations3, 3 ); break;
            default : printf( "\nNOT implemented here yet ... \n" );
        }
        putchar( '\n' ) ;
    }
    while( more() ) ;

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.