By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,795 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,795 IT Pros & Developers. It's quick & easy.

Why this is wrong?

P: n/a
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?

Jun 14 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On 13 Jun 2006 17:13:46 -0700
li*****@hotmail.com wrote:
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.
Jun 14 '06 #2

P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
<li*****@hotmail.com> wrote:
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
Jun 14 '06 #3

P: n/a
On 13 Jun 2006 17:13:46 -0700, li*****@hotmail.com wrote in
comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 14 '06 #4

P: n/a
On Wed, 14 Jun 2006 01:34:03 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
On 13 Jun 2006 17:13:46 -0700, li*****@hotmail.com wrote in
comp.lang.c:
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;
}

--
jay
Jun 14 '06 #5

P: n/a
li*****@hotmail.com wrote:
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?

--
Chris "seeker" Dollin
"We did not have time to find out everything we wanted to know." /A Clash of Cymbals/

Jun 14 '06 #6

P: n/a
jaysome <ja*****@spamcop.net> writes:
On Wed, 14 Jun 2006 01:34:03 -0500, Jack Klein <ja*******@spamcop.net>
wrote:

[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 14 '06 #7

P: n/a
On Wed, 14 Jun 2006 08:37:37 GMT, Keith Thompson <ks***@mib.org>
wrote:
jaysome <ja*****@spamcop.net> writes:
On Wed, 14 Jun 2006 01:34:03 -0500, Jack Klein <ja*******@spamcop.net>
wrote:

[...]
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.

--
jay
Jun 14 '06 #8

P: n/a
unsigned int input_0[8];
unsigned int *p_inp = input_0;

*p_inp_0++ = 100;

..............................
li*****@hotmail.com wrote:
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?


Jun 14 '06 #9

P: n/a
li*****@hotmail.com wrote:

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.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Jun 14 '06 #10

P: n/a
Charles Richmond wrote:
li*****@hotmail.com wrote:
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.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+


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.
Jun 14 '06 #11

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Scott W wrote:
Charles Richmond wrote:
li*****@hotmail.com wrote:
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.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+


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-----
Jun 14 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.