Why isn't my array working?

Started by
18 comments, last by King Mir 11 years, 2 months ago

Hi, I'm not sure why this isn't working. I define SIZE at the beginning but it seems not to work. I replaced SIZE with a different number and it worked fine. Why isn't it recognizing what SIZE is?


#include <stdio.h>

#define SIZE 10;

    main()
    {
        int i, array[SIZE];
    for(i = 0; i < SIZE; i++)
    {
    array[i] = 0
    }
    }

Errors I get:
C:\Users\Gary\Desktop\c\firstproga.c||In function 'main':|
C:\Users\Gary\Desktop\c\firstproga.c|7|error: expected ']' before ';' token|
C:\Users\Gary\Desktop\c\firstproga.c|8|error: expected expression before ';' token|
C:\Users\Gary\Desktop\c\firstproga.c|10|error: 'array' undeclared (first use in this function)|
C:\Users\Gary\Desktop\c\firstproga.c|10|note: each undeclared identifier is reported only once for each function it appears in|
C:\Users\Gary\Desktop\c\firstproga.c|11|error: expected ';' before '}' token|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|

Advertisement

Macro's are stupid token replacement that does not respect the general language syntax. Avoid them unless you really must use them. Your code expands to this:

main()
{
    int i, array[10;];
    for(i = 0; i < 10;; i++)
    {
        array[i] = 0
    }
}

Drop the semicolon after the define.

You shouldn't use a semicolon in your macro definition. The way your code is written, every time you say `SIZE' the compiler sees `10;'.

EDIT: Ninja'd by less than a minute. smile.png

Ahh thanks! For some reason the book has a semi-colon at the end of a macro. It really threw me off. Must just be a typo in the book. Thank you guys!

also,

for(i = 0; i < SIZE; i++)
    {
    array[i] = 0 //<- missing a semicolon here!
    }

Ahh yeah in the original program I did have the semi there, I had to retype it on a different computer and forgot it. thanks.

'array' is a keyword in C++ IIRC, so it's a good habit to not use it as a variable name. Rather than using #define to define constants it's better to just make constants:
const size_t SIZE = 10;
This gives you type control and avoids macro expansion problems like the one you encountered.

Using #define is almost the same as using your a text editor's 'find-and-replace' function. There are some cases where it's useful, but usually it causes more problems than it solves.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

'array' is a keyword in C++ IIRC, so it's a good habit to not use it as a variable name. Rather than using #define to define constants it's better to just make constants:

const size_t SIZE = 10;
This gives you type control and avoids macro expansion problems like the one you encountered.

Using #define is almost the same as using your a text editor's 'find-and-replace' function. There are some cases where it's useful, but usually it causes more problems than it solves.


I am pretty sure "array" is not a keyword in c. There is the std::array type, but that's only in c++.

Also, nice analogy there with the "find and replace", I've never thought about it like that.

'array' is a keyword in C++ IIRC, so it's a good habit to not use it as a variable name. Rather than using #define to define constants it's better to just make constants:


const size_t SIZE = 10;
This gives you type control and avoids macro expansion problems like the one you encountered.

Using #define is almost the same as using your a text editor's 'find-and-replace' function. There are some cases where it's useful, but usually it causes more problems than it solves.

The problem with const size_t is that a global variable defined like that is addressable, and must be generated in the object file. #define, for it's faults, does not have this problem. Therefore I consider a #defined macro to be the preferred general purpose way to declare constants in C.

C++ has slightly different rules for global constants, so there the convention is different; in c++ global constants have internal linkage, and do not allocate storage if nothing takes their address.

Rather than using #define to define constants it's better to just make constants:

const size_t SIZE = 10;
This gives you type control and avoids macro expansion problems like the one you encountered.

Using #define is almost the same as using your a text editor's 'find-and-replace' function. There are some cases where it's useful, but usually it causes more problems than it solves.


Actually, I don't think this is true in C. I couldn't get this to work:
const int N = 10;

struct S {
  int i[N];
};

This topic is closed to new replies.

Advertisement