Converting #define value to a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • maker.rain1@gmail.com

    #1

    Converting #define value to a variable

    Hello All,

    I have come across a problem as explained below in a sample. Please
    help me if anyone has any ideas to solve this.

    I have a #define as defined below.

    #define MAX 200

    int _tmain(int argc, _TCHAR* argv[])
    {
    int arr[MAX];
    return 0;
    }

    Here we do not have any problem. I have a situation and I want to make
    this MAX value as a variable. As per C rules we can not do that and
    compiler gives error. In my actual problematic code this kind of code
    is at lots of places. And also at certain places we have code like
    below.

    typedef struct _xxxxx
    {
    ....
    byte val;
    uint array[MAX];
    }xxxxx;

    So what would be the best solution with minimal effort to replace
    #define value MAX with a variable which I can change anytime.

    Thanks in advance and awaiting for some response soon.
  • rahul

    #2
    Re: Converting #define value to a variable

    On Jun 6, 3:28 pm, maker.ra...@gma il.com wrote:
    Hello All,
    >
    I have come across a problem as explained below in a sample. Please
    help me if anyone has any ideas to solve this.
    >
    I have a #define as defined below.
    >
    #define MAX 200
    >
    int _tmain(int argc, _TCHAR* argv[])
    {
    int arr[MAX];
    return 0;
    >
    }
    >
    Here we do not have any problem. I have a situation and I want to make
    this MAX value as a variable. As per C rules we can not do that and
    compiler gives error.
    What's with _tmain and _TCHAR*? It's not standard C.
    C99 allows variable length arrays. If you have a C99 complying
    compiler, that can
    do the trick for you.
    >typedef struct _xxxxx
    >{
    ....
    byte val;
    uint array[MAX];
    }xxxxx;
    >So what would be the best solution with minimal effort to replace
    >#define value MAX with a variable which I can change anytime.
    since when we have a byte data type in C?
    You can declare the array as a pointer and allocate it memory based on
    your variable in your code.

    typedef struct _foo {
    short num;
    unsigned int* array;
    }foo;
    struct foo bar;

    /* Later in your code */
    bar.array = malloc (num * sizeof *bar.array);


    Comment

    • Ben Bacarisse

      #3
      Re: Converting #define value to a variable

      maker.rain1@gma il.com writes:
      I have a #define as defined below.
      >
      #define MAX 200
      >
      int _tmain(int argc, _TCHAR* argv[])
      {
      int arr[MAX];
      return 0;
      }
      <snip>
      typedef struct _xxxxx
      {
      ....
      byte val;
      uint array[MAX];
      }xxxxx;
      >
      So what would be the best solution with minimal effort to replace
      #define value MAX with a variable which I can change anytime.
      There is no one solution to both problems. If you can use a C99
      compiler (or an almost C99 compiler like gcc) you can get round both
      of these but in different ways.

      The first "just works". For the second, you leave out the size. When
      the struct is allocated you must add in some extra space for the final
      array. This means you can't usually declare variables of such a
      struct type -- you must be using malloc.

      If you don't have C99, the first problem has to be solved with:

      int *arr = malloc(MAX * sizeof *arr);

      and now MAX need not be a constant expression. Remember to free the
      space.

      For the struct problem you have two choices. The most portable is to
      change the array to a pointer. Allocating one of these structs then
      involves two mallocs -- one for the struct and one for the array.

      If you don't mind sailing close to the rocks, the alternative is to
      replace the array size by 1 and allocate the extra space (minus one
      array element) as if you were using the C99 solution. This is not
      guaranteed to work by the C standard, but it work on a very wide range
      of systems. For reference, it is called "the struct hack".

      --
      Ben.

      Comment

      • CBFalconer

        #4
        Re: Converting #define value to a variable

        maker.rain1@gma il.com wrote:
        >
        I have come across a problem as explained below in a sample.
        Please help me if anyone has any ideas to solve this.
        >
        I have a #define as defined below.
        >
        #define MAX 200
        int _tmain(int argc, _TCHAR* argv[]) {
        int arr[MAX];
        return 0;
        }
        >
        Here we do not have any problem. ...
        Yes you do. The identifiers _tmain and _TCHAR are reserved for the
        implementation. You are not allowed to use them. Avoid all
        leading underscores.

        --
        [mail]: Chuck F (cbfalconer at maineline dot net)
        [page]: <http://cbfalconer.home .att.net>
        Try the download section.


        ** Posted from http://www.teranews.com **

        Comment

        • Serve Lau

          #5
          Re: Converting #define value to a variable


          "CBFalconer " <cbfalconer@yah oo.comschreef in bericht
          news:484946E1.8 5EBCA26@yahoo.c om...
          maker.rain1@gma il.com wrote:
          >>
          >I have come across a problem as explained below in a sample.
          >Please help me if anyone has any ideas to solve this.
          >>
          >I have a #define as defined below.
          >>
          >#define MAX 200
          >int _tmain(int argc, _TCHAR* argv[]) {
          > int arr[MAX];
          > return 0;
          >}
          >>
          >Here we do not have any problem. ...
          >
          Yes you do. The identifiers _tmain and _TCHAR are reserved for the
          implementation. You are not allowed to use them. Avoid all
          leading underscores.
          they are names supplied by the implementation he just uses them. So nope, in
          that sense there's no problem here

          Comment

          • Barry Schwarz

            #6
            Re: Converting #define value to a variable

            On Fri, 6 Jun 2008 03:28:35 -0700 (PDT), maker.rain1@gma il.com wrote:
            >Hello All,
            >
            >I have come across a problem as explained below in a sample. Please
            >help me if anyone has any ideas to solve this.
            >
            >I have a #define as defined below.
            >
            >#define MAX 200
            >
            >int _tmain(int argc, _TCHAR* argv[])
            >{
            int arr[MAX];
            return 0;
            >}
            >
            >Here we do not have any problem. I have a situation and I want to make
            >this MAX value as a variable. As per C rules we can not do that and
            >compiler gives error. In my actual problematic code this kind of code
            >is at lots of places. And also at certain places we have code like
            >below.
            >
            >typedef struct _xxxxx
            >{
            ....
            byte val;
            uint array[MAX];
            }xxxxx;
            >
            >So what would be the best solution with minimal effort to replace
            >#define value MAX with a variable which I can change anytime.
            >
            If you don't know the size of the array at compile time but you will
            know at run time, replace the array with a pointer and use malloc to
            allocate the space. Your code can reference elements of the "dynamic
            array" with the same syntax as if you had defined the array.


            Remove del for email

            Comment

            Working...