Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why isn't my array working?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 tiresandplanes   Members   -  Reputation: 133

Like
0Likes
Like

Posted 11 February 2013 - 01:47 PM

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) ===|
 



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8010

Like
4Likes
Like

Posted 11 February 2013 - 01:55 PM

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.



#3 Álvaro   Crossbones+   -  Reputation: 12861

Like
4Likes
Like

Posted 11 February 2013 - 01:55 PM

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

Edited by Álvaro, 11 February 2013 - 01:56 PM.


#4 tiresandplanes   Members   -  Reputation: 133

Like
0Likes
Like

Posted 11 February 2013 - 02:00 PM

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!



#5 ultramailman   Prime Members   -  Reputation: 1563

Like
3Likes
Like

Posted 11 February 2013 - 02:01 PM

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


#6 tiresandplanes   Members   -  Reputation: 133

Like
0Likes
Like

Posted 11 February 2013 - 02:03 PM

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.



#7 Khatharr   Crossbones+   -  Reputation: 2958

Like
1Likes
Like

Posted 11 February 2013 - 03:04 PM

'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.

#8 ultramailman   Prime Members   -  Reputation: 1563

Like
0Likes
Like

Posted 11 February 2013 - 03:15 PM

'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.

Edited by ultramailman, 11 February 2013 - 05:28 PM.


#9 King Mir   Members   -  Reputation: 1945

Like
0Likes
Like

Posted 11 February 2013 - 03:21 PM

'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.


Edited by King Mir, 11 February 2013 - 03:30 PM.


#10 Álvaro   Crossbones+   -  Reputation: 12861

Like
1Likes
Like

Posted 11 February 2013 - 03:41 PM

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];
};

Edited by Álvaro, 11 February 2013 - 03:42 PM.


#11 tiresandplanes   Members   -  Reputation: 133

Like
0Likes
Like

Posted 11 February 2013 - 04:06 PM

I am using K & R to learn C and I was just copying what they did. There was a typo in the book, and I didn't know it was bad to use macros.



#12 King Mir   Members   -  Reputation: 1945

Like
0Likes
Like

Posted 11 February 2013 - 08:57 PM

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];
};

I had forgotten about this additional problem. Yeah, that's the other reason to use a macro. Constant variables are not constant expressions.


Edited by King Mir, 11 February 2013 - 09:07 PM.


#13 King Mir   Members   -  Reputation: 1945

Like
0Likes
Like

Posted 11 February 2013 - 09:05 PM

I am using K & R to learn C and I was just copying what they did. There was a typo in the book, and I didn't know it was bad to use macros.

It's not for this. Just don't do it in C++.

 

K & R is a very old book though. There should be better, more modern, books out there. In particular, it's bad practice (and may be against C99, I'm not sure) to use implicit return int like you do. You should explicitly write "int main".



#14 Aldacron   GDNet+   -  Reputation: 3130

Like
1Likes
Like

Posted 11 February 2013 - 09:33 PM

I am using K & R to learn C and I was just copying what they did. There was a typo in the book, and I didn't know it was bad to use macros.

 

It's not bad to use macros per se. But if you do then you need to understand the consequences of using them and give yourself some strict guidelines in doing so. In C programming, they can be quite useful (extremely so, in some cases). The flip side is that it is easy to create macros that do not behave the way you expect them to. In C++, there are better, less error-prone alternatives, hence macros are frowned upon in the general case. But even in C they should be used with care. For simple constant values like SIZE, they are perfectly safe. When I program C, I prefer macros for that situation unless I need several related constants, in which case I'd use an enum, You just need to keep in mind any issues that may arise from the data type being used (i.e. signed vs unsigned, integer vs floating point), but that's something you need to be aware of with any variable.



#15 Khatharr   Crossbones+   -  Reputation: 2958

Like
0Likes
Like

Posted 12 February 2013 - 05:23 AM

Álvaro, on 11 Feb 2013 - 13:49, said:
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];
};

That would definitely be a good case to use them. (I didn't say never to use them.) I think you can still use const values in stack arrays though. They cause so many problems so easily I just really think it's best to avoid them wherever possible.

King Mir, on 11 Feb 2013 - 13:29, said:
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.

Why is this a problem? I don't follow.
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.

#16 Álvaro   Crossbones+   -  Reputation: 12861

Like
1Likes
Like

Posted 12 February 2013 - 08:18 AM


Álvaro, on 11 Feb 2013 - 13:49, said:
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];
};


That would definitely be a good case to use them. (I didn't say never to use them.) I think you can still use const values in stack arrays though. [...]


Sort of. C99 has variable-length arrays, which is why it will work if you use a `const' variable as the size of an array on the stack.

Edited by Álvaro, 12 February 2013 - 08:18 AM.


#17 Khatharr   Crossbones+   -  Reputation: 2958

Like
0Likes
Like

Posted 12 February 2013 - 03:39 PM

Álvaro, on 12 Feb 2013 - 06:26, said:
Sort of. C99 has variable-length arrays, which is why it will work if you use a `const' variable as the size of an array on the stack.

!

I did not know this thing.
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.

#18 King Mir   Members   -  Reputation: 1945

Like
0Likes
Like

Posted 12 February 2013 - 06:51 PM

.
King Mir, on 11 Feb 2013 - 13:29, said:
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.

Why is this a problem? I don't follow.

Álvaro pointed out the bigger issue, which is that constant variables are not compile time constants from a language perspective, and so cannot be used in array sizes generally. I was pointing out that the compiler is not required to promote them to compile time constants.

#19 Khatharr   Crossbones+   -  Reputation: 2958

Like
0Likes
Like

Posted 12 February 2013 - 09:15 PM

King Mir, on 12 Feb 2013 - 16:59, said:
Álvaro pointed out the bigger issue, which is that constant variables are not compile time constants from a language perspective, and so cannot be used in array sizes generally. I was pointing out that the compiler is not required to promote them to compile time constants.

Ah. I get you. Are there any plans to update C any more? Seems like something that could easily be standardized.
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.

#20 King Mir   Members   -  Reputation: 1945

Like
1Likes
Like

Posted 12 February 2013 - 11:20 PM

King Mir, on 12 Feb 2013 - 16:59, said:
Álvaro pointed out the bigger issue, which is that constant variables are not compile time constants from a language perspective, and so cannot be used in array sizes generally. I was pointing out that the compiler is not required to promote them to compile time constants.

Ah. I get you. Are there any plans to update C any more? Seems like something that could easily be standardized.

The C working group is definitely alive and updating, and C11 recently came out, but, as far as I know, changing the nature of const to match C++ is not something anyone is pushing for. The latest proposals can be found here http://www.open-std.org/jtc1/sc22/wg14/www/docs/PostPortland2012.htm

 

 

But there's not as much energy for improving C as there is for C++, and even C99 adoption has been poor. Notably Visual studio does not and does not  plan to support C99.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS