#define array[]

This is a discussion on #define array[] within the C Programming forums, part of the General Programming Boards category; Hello. I have a problem. I want to define a constant array. Code: #include <stdio.h> #include <stdlib.h> #define array[] {'a', ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32

    #define array[]

    Hello. I have a problem. I want to define a constant array.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define array[] {'a', 'b', 'c'}
    
    
    int main()
    {
      system("clear");
    
      printf("\n\n%c\n\n", array[0]);
    
      return 0;
    }
    when I try to compile with gcc I get that error

    p.c:4:13: warning: missing whitespace after the macro name
    p.c: In function 'main':
    p.c:11: error: syntax error before '[' token


    Any one who knows how to define a constant array? Pleace help? Thank you.

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    does const not work?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This expands to

    Code:
    printf("\n\n%c\n\n", [] {'a', 'b', 'c'}[0]);
    What did you expect?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anon View Post
    This expands to
    No, it would only expand to that if there was a space after the word array. As-is, it just fails to compile.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Interestingly with GCC it only deserves a warning, with Comeau Online it is an error in C99 mode and a warning in C90 mode.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by cs05pp2 View Post
    Any one who knows how to define a constant array? Pleace help? Thank you.
    Code:
    const char array[] = "abc";
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32
    If I use that code is working and compile:

    Code:
    int main()
    {
    int array[] = {1,2,3,4,5};
    
    printf("%d", array[0]);
    
    return 0;
    }
    but if I try to define it as constant I get an error from the gcc compiler.

    Code:
    #define array[] {1,2,3,4,5};
    int main()
    {
    
    
    printf("%d", array[0]);
    
    return 0;
    How can I define a constant array ? Thank you

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    An array requires a physical location in memory. It cannot be implemented as a simple macro. cpjust's code is the correct way to do this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32
    If I define

    Code:
    #define array "abcdef"
    
    int main()
    {
    printf("%c", array[0]);
    
    return 0;
    }
    It will work, and the string is an array of characters. So I still believe that I can define a constant array. But I don't know how.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Quote Originally Posted by cs05pp2
    It will work, and the string is an array of characters. So I still believe that I can define a constant array. But I don't know how.
    Right, but then "abcdef"[0] is valid syntax. Suppose that you did not define a macro. How would you do what you are trying to do? If you cannot do it, then you cannot do it by defining a macro, since preprocessing would result in textual substitution.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    #define array "abcdef"

    it only works because "abcdef" is a string literal. There are no other "literal" arrays in C, they should have a proper identifier. If you think that #define makes an identifier out of some phrase, then please review what your text says about the prepocessor: you are wrong.

  12. #12
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    It can be done in C99 (look up compound literals); but I can't see much of a reason to do so when you can just use const, which is portable to non-C99 compilers.

    The real question is, why are you hell-bent on avoiding the creation of an array and tagging it with const?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 11:52 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21