Weird response from statement in c.

In summary: The enum (stands for enumeration) is a little safer - the compiler knows the value is an integer and can check where using it in the code makes sense.
  • #1
dE_logics
742
0
In the program below -
Code:
#include<stdio.h>
char places = 4;
main()
{
	char m = 3;
	short i;
	char counter[places - 2][1];
	for(i = 0;i<=places-3;i++)
	{
		counter[i][0] = m;
		m++;
		counter[i][1] = i - i;
	}
	printf("%d \n", counter[1][0]);//Value of counter[1][0] = 4
	counter[0][1] = 2;
	printf("%d", counter[1][0]);//Value of counter[1][0] switches to 2 after the above statement...WHAT?
}

The statement 'counter[0][1] = 2;' changes the value of counter[1][0] to 2...and it was 4 previously.
 
Technology news on Phys.org
  • #2
dE_logics said:
The statement 'counter[0][1] = 2;' changes the value of counter[1][0] to 2...and it was 4 previously.

The size of your array is "1" in the second dimension! You declared the array as
Code:
	char counter[places - 2][1];

Note that any index to the array must be strictly LESS than the size given in the array declaration. Thus the only valid value for the second index is zero!

Hence the expression counter[0][1] is an overflow of the second index, and in fact it is indeed the same thing as counter[1][0].

By the way; it's a really bad idea to use a variable in the size of the array. If you intend "places" to be a constant, then use one of these two methods.
Code:
// ONLY USE ONE OF THESE METHODS!
#define places 4      // old method
enum { places = 4 };  // better new method, unless you have a very old compiler

My preference would be this:
Code:
// Array size definition
enum {
    numrows = 3,
    numcols = 2
};

void main()
{
    char array[numrows][numcols];

Cheers -- sylas
 
  • #3
OH YES!...HOW STUPID!

Thanks man!
 
  • #4
places will have to vary...actually this is a section of a program.
 
  • #5
dE_logics said:
places will have to vary...actually this is a section of a program.

You can't vary the size of an array in C. You can perhaps pick a maximum possible size, define that for a constant, and then have another variable which effectively says how much of the array you will use.

Cheers -- sylas
 
  • #6
Yes, on second thought, it is a good idea.

Thanks!
 
  • #7
So there's no difference between

#define places 4
and
enum { places = 4 }?

Both will be called symbolic constants?


What will enum stand for?
 
  • #8
They do the same job
The enum (stands for enumeration) is a little safer - the compiler knows the value is an integer and can check where using it in the code makes sense.
The #define is just a search/replace - if you make a mistake such as writing #define places 4;
The compiler will simply copy this to give you char array[4;];
which is an error - but the error message will be harder to understand because it will say the error is in "char array[places];"

edit - one other thing to be carefull of, if you are using gcc (if you are on Linux) then you can write
int places=4;
char array[places];
This is an extention to c++ by the gcc compiler and doesn't work anywhere else - don't use it.
 
  • #9
edit - one other thing to be carefull of, if you are using gcc (if you are on Linux) then you can write
int places=4;
char array[places];
This is an extention to c++ by the gcc compiler and doesn't work anywhere else - don't use it.

YES, I DID THAT!

AND IT DID WORK...but it was sort of confusing, at times it works and at times it does not.

I made places and array global variables and it did not work all of a sudden...and I don't think we have absolutely no reference to this in ANY ebook...or in general book.

So, if I make place a symbolic constant, will array[places] work...in most compilers?

Another question with enum...if I make enum {places = 5.4857}...i.e float, will it get rejected?
 
  • #10
Yes a symbolic constant inside an array index will work anywhere.
No enums can only be integers.
 
  • #11
Ok...thanks a lot!
 

Related to Weird response from statement in c.

1. What does "weird response from statement in c" mean?

"Weird response from statement in c" is a common error message in the C programming language. It usually indicates that there is a logical error in the code, such as a missing semicolon or a variable being used before it is declared.

2. How can I fix a "weird response from statement in c" error?

To fix this error, you will need to carefully review your code and look for any logical errors. Check for missing semicolons, incorrect variable names, and any other mistakes that may be causing the error. You may also want to use a debugger to help identify the specific line of code that is causing the error.

3. Why am I getting a "weird response from statement in c" error?

This error can occur for a variety of reasons, but it is most commonly caused by a logical error in the code. It could also be caused by a syntax error, a missing library or header file, or a problem with your compiler. It is important to carefully review your code and check for any mistakes that may be causing the error.

4. Can I prevent "weird response from statement in c" errors from occurring?

While it is impossible to completely prevent this type of error from occurring, there are some steps you can take to minimize the chances of encountering it. These include writing clean and organized code, using a debugger to catch any errors early on, and carefully reviewing your code for any logical or syntax errors.

5. Is "weird response from statement in c" a serious error?

It depends on the context of the error and how it is affecting your program. In most cases, this error can be easily fixed by identifying and correcting the mistake in the code. However, if left unresolved, it could potentially cause your program to crash or produce unexpected results. It is important to address this error as soon as possible to ensure the proper functioning of your program.

Similar threads

  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
821
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
963
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
Back
Top