Why this is wrong?

L

linq936

Hi,
I have this code,

unsigned int input_0[8];

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;

The compiler is gcc ported to a specific embedded processor. The
compiler complains that I can not use increment operation like that.

Do you know why?
 
R

Rafael Almeida

On 13 Jun 2006 17:13:46 -0700
Hi,
I have this code,

unsigned int input_0[8];

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;

The compiler is gcc ported to a specific embedded processor. The
compiler complains that I can not use increment operation like that.

Do you know why?

Because the C standard doesn't say it should be allowed.
 
R

Richard Tobin

unsigned int input_0[8];

*input_0++ = 100;

input_0 is an array. You can't increment it.

But you can set a pointer to the start of the array and then increment
that:

unsigned int *p = &input_0[0]; /* or just p = input */
*p++ = 100;
...

-- Richard
 
J

Jack Klein

Hi,
I have this code,

unsigned int input_0[8];

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;

The compiler is gcc ported to a specific embedded processor. The
compiler complains that I can not use increment operation like that.

Do you know why?

Because input_0 is an array, not a pointer, and you cannot increment
an array.

An array is not a pointer, and a pointer is not an array. Far too
many newbies (and some oldbies) never grasp this.
 
J

jaysome

Hi,
I have this code,

unsigned int input_0[8];

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;

The compiler is gcc ported to a specific embedded processor. The
compiler complains that I can not use increment operation like that.

Do you know why?

Because input_0 is an array, not a pointer, and you cannot increment
an array.

An array is not a pointer, and a pointer is not an array. Far too
many newbies (and some oldbies) never grasp this.

As demonstrated by the following code:

void print_array_size(const int array[])
{
printf("array size = %lu\n", (unsigned long)(sizeof array));
}

int main(void)
{
int array[16] = {0};
print_array_size(array);
return 0;
}
 
C

Chris Dollin

Hi,
I have this code,

unsigned int input_0[8];

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;

The compiler is gcc ported to a specific embedded processor. The
compiler complains that I can not use increment operation like that.

Do you know why?

Yes.

You can't increment an array. Why did you think you could?
 
K

Keith Thompson

jaysome said:
[...]
An array is not a pointer, and a pointer is not an array. Far too
many newbies (and some oldbies) never grasp this.

As demonstrated by the following code:

void print_array_size(const int array[])
{
printf("array size = %lu\n", (unsigned long)(sizeof array));
}

int main(void)
{
int array[16] = {0};
print_array_size(array);
return 0;
}

The object "array" in main() is an array.

The object (parameter) "array" in print_array_size() is a pointer, not
an array. The rule that a parameter declaration such as "int foo[]"
is quietly transformed to the equivalent of "int *foo*" is, in my
humble opinion, unfortunate.
 
J

jaysome

jaysome said:
[...]
An array is not a pointer, and a pointer is not an array. Far too
many newbies (and some oldbies) never grasp this.

As demonstrated by the following code:

void print_array_size(const int array[])
{
printf("array size = %lu\n", (unsigned long)(sizeof array));
}

int main(void)
{
int array[16] = {0};
print_array_size(array);
return 0;
}

The object "array" in main() is an array.

The object (parameter) "array" in print_array_size() is a pointer, not
an array. The rule that a parameter declaration such as "int foo[]"
is quietly transformed to the equivalent of "int *foo*" is, in my
humble opinion, unfortunate.

Yes. Languages such as C and C++ suffer from this, while languages
such as Ada and C# are more fortunate.
 
R

rain.man

unsigned int input_0[8];
unsigned int *p_inp = input_0;

*p_inp_0++ = 100;

..............................
 
C

Charles Richmond

Hi,
I have this code,

unsigned int input_0[8];

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;

The compiler is gcc ported to a specific embedded processor. The
compiler complains that I can not use increment operation like that.

Do you know why?
Your variable "input_0" is an array name, and thus a constant.
It can *not* be incremented. Try this:

unsigned int *uiray[8];
unsigned int *input_0 = uiray;

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;


In this code, "input_0" is a pointer, *not* an array name.
 
S

Scott W

Charles said:
Hi,
I have this code,

unsigned int input_0[8];

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;

The compiler is gcc ported to a specific embedded processor. The
compiler complains that I can not use increment operation like that.

Do you know why?
Your variable "input_0" is an array name, and thus a constant.
It can *not* be incremented. Try this:

unsigned int *uiray[8];
unsigned int *input_0 = uiray;

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;


In this code, "input_0" is a pointer, *not* an array name.

why not just do:

int a = 0;
unsigned int input_0[8];

input_0[a++] = 100;
input_0[a++] = 200;
input_0[a++] = 300;
input_0[a++] = 400;
input_0[a++] = 1;
input_0[a++] = 2;
input_0[a++] = 3;
input_0[a] = 4;

or hell, just:

unsigned int input_0[8];

input_0[0] = 100;
etc.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Scott said:
Charles said:
Hi,
I have this code,

unsigned int input_0[8];

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;

The compiler is gcc ported to a specific embedded processor. The
compiler complains that I can not use increment operation like that.

Do you know why?
Your variable "input_0" is an array name, and thus a constant.
It can *not* be incremented. Try this:

unsigned int *uiray[8];
unsigned int *input_0 = uiray;

*input_0++ = 100;
*input_0++ = 200;
*input_0++ = 300;
*input_0++ = 400;
*input_0++ = 1;
*input_0++ = 2;
*input_0++ = 3;
*input_0 = 4;


In this code, "input_0" is a pointer, *not* an array name.

why not just do:

int a = 0;
unsigned int input_0[8];

input_0[a++] = 100;
input_0[a++] = 200;
input_0[a++] = 300;
input_0[a++] = 400;
input_0[a++] = 1;
input_0[a++] = 2;
input_0[a++] = 3;
input_0[a] = 4;

or hell, just:

unsigned int input_0[8];

input_0[0] = 100;
etc.

or even

int a = 7;
unsigned int input_0[8] = { 100, 200, 300, 400, 1, 2, 3, 4};

which would produce the same end state

- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEkEM8agVFX4UWr64RAjalAJ9T8HTTtL/H7c5icZ0EU1OKsCKhlwCfZ7yy
0aGWb5Q7RDlJYOg6b27mTew=
=LsN2
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top