Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #4924 > unrolled thread

error in code

Started by"Bill Cunningham" <nospam@nspam.invalid>
First post2011-05-28 15:28 -0400
Last post2011-06-01 09:02 +0200
Articles 20 on this page of 49 — 17 participants

Back to article view | Back to comp.lang.c


Contents

  error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 15:28 -0400
    Re: error in code pozz <pozzugno@gmail.com> - 2011-05-28 21:49 +0200
      Re: error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 16:12 -0400
        Re: error in code Shao Miller <sha0.miller@gmail.com> - 2011-05-28 15:33 -0500
          Re: error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 17:12 -0400
            Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-28 14:26 -0700
            Re: error in code Shao Miller <sha0.miller@gmail.com> - 2011-05-28 16:29 -0500
            Re: error in code Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-28 17:26 -0600
        Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-28 14:20 -0700
          Re: error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-29 02:16 -0400
        Re: error in code Lew Pitcher <lpitcher@teksavvy.com> - 2011-05-28 16:35 -0400
          Re: error in code "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-29 15:50 -0400
          Re: error in code Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-29 20:39 -0600
    Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-28 13:46 -0700
    Re: error in code Nobody <nobody@nowhere.com> - 2011-05-28 23:06 +0100
      Re: error in code Ian Collins <ian-news@hotmail.com> - 2011-05-29 10:10 +1200
    Re: error in code Ian Collins <ian-news@hotmail.com> - 2011-05-29 10:09 +1200
    Re: error in code Angel <angel+news@spamcop.net> - 2011-05-28 22:28 +0000
      Re: error in code Ian Collins <ian-news@hotmail.com> - 2011-05-29 10:37 +1200
        Re: error in code Angel <angel+news@spamcop.net> - 2011-05-28 22:45 +0000
          Re: error in code Ian Collins <ian-news@hotmail.com> - 2011-05-29 10:48 +1200
            Re: error in code Angel <angel+news@spamcop.net> - 2011-05-28 22:56 +0000
              Re: error in code Seebs <usenet-nospam@seebs.net> - 2011-05-29 00:38 +0000
          Re: error in code "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-05-28 23:11 -0400
    Re: error in code Keith Thompson <kst-u@mib.org> - 2011-05-28 15:54 -0700
    Re: error in code Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-28 17:23 -0600
    Re: error in code jacob navia <jacob@spamsink.net> - 2011-05-29 19:30 +0200
      Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-29 18:14 -0700
        Re: error in code Keith Thompson <kst-u@mib.org> - 2011-05-29 18:50 -0700
          Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-29 19:10 -0700
            Re: error in code Seebs <usenet-nospam@seebs.net> - 2011-05-30 02:54 +0000
              Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-29 20:35 -0700
                Re: error in code Seebs <usenet-nospam@seebs.net> - 2011-05-30 06:15 +0000
        Re: error in code jacob navia <jacob@spamsink.net> - 2011-05-30 08:46 +0200
          Re: error in code Geoff <geoff@invalid.invalid> - 2011-05-30 18:50 -0700
            Re: error in code Keith Thompson <kst-u@mib.org> - 2011-05-31 10:49 -0700
      Re: error in code Peter Nilsson <airia@acay.com.au> - 2011-05-29 21:04 -0700
        Re: error in code jacob navia <jacob@spamsink.net> - 2011-05-30 08:44 +0200
        Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-31 01:35 -0700
          Re: error in code pacman@kosh.dhis.org (Alan Curry) - 2011-05-31 23:36 +0000
            Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-01 03:01 -0700
              Re: error in code Keith Thompson <kst-u@mib.org> - 2011-06-01 08:08 -0700
                Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-01 12:50 -0700
                  Re: error in code Keith Thompson <kst-u@mib.org> - 2011-06-01 13:26 -0700
                    Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-02 18:10 -0700
                      Re: error in code jacob navia <jacob@spamsink.net> - 2011-06-03 11:55 +0200
                        Re: error in code Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-03 19:37 -0700
                          Re: error in code jacob navia <jacob@spamsink.net> - 2011-06-04 08:13 +0200
        Re: error in code Nisse Engström <news.NOSPAM.idf0Z@luden.se> - 2011-06-01 09:02 +0200

Page 1 of 3  [1] 2 3  Next page →


#4924 — error in code

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-28 15:28 -0400
Subjecterror in code
Message-ID<4de14d0c$0$1943$bbae4d71@news.suddenlink.net>
    I am trying here to calculate a mean of 3 values. I do not understand 
the errors the compiler returned I've never seen them.

#include <stdio.h>

void mean(double *avg, int periods)
{
    double val;
    val = *avg / periods;
    printf("%.2f\n", val);
}

int main(void)
{
    double a[] = 2.2, 4.2, 5.00;
    mean(a, 3);
}
p.c: In function `main':
p.c:12: invalid initializer
p.c:12: parse error before numeric constant

    What's it trying to tell me?

Bill


[toc] | [next] | [standalone]


#4929

Frompozz <pozzugno@gmail.com>
Date2011-05-28 21:49 +0200
Message-ID<irrjl0$iuv$1@nnrp.ngi.it>
In reply to#4924
Il 28/05/2011 21:28, Bill Cunningham ha scritto:
> void mean(double *avg, int periods)
> {
>      double val;
>      val = *avg / periods;
>      printf("%.2f\n", val);
> }

avg is a pointer to double. *avg is a double. If you write

   *avg / periods

your dividing one double value by periods.

In your case, avg points to an array of double values. The number of 
elements in the array is periods. So you have to write a cycle to sum 
all the periods elements together. At last, you can divide by periods.



> int main(void)
> {
>      double a[] = 2.2, 4.2, 5.00;
                     ^

You are initializing an array. In C you can do that with curly backets {}:

   double a[] = { 2.2, 4.2, 5.00 };

>      mean(a, 3);
                ^

In this case you are passing the number of elements of a[] array. I 
suggest to use a syntax like the following:

   mean(a, sizeof(a) / sizeof(a[0]));

or define a generic macro if you're going to use this syntax in several 
points:

   #define SIZEARRAY(a)  (sizeof(a) / sizeof(a[0]))

In this way, you can modify the number of elements in the 
initialization, but you haven't to modify the number of elements that 
will be automatically calculated by the preprocessor.

Of course, this is valid only for static allocated array. You you 
dinamically allocate an array, you are responsible to take its lenght in 
some variable.

> }
> p.c: In function `main':
> p.c:12: invalid initializer
> p.c:12: parse error before numeric constant
>
>      What's it trying to tell me?

See above.

[toc] | [prev] | [next] | [standalone]


#4933

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-28 16:12 -0400
Message-ID<4de15747$0$1954$bbae4d71@news.suddenlink.net>
In reply to#4929
pozz wrote:
> Il 28/05/2011 21:28, Bill Cunningham ha scritto:
>> void mean(double *avg, int periods)
>> {
>>      double val;
>>      val = *avg / periods;
>>      printf("%.2f\n", val);
>> }
>
> avg is a pointer to double. *avg is a double. If you write
>
>   *avg / periods
>
> your dividing one double value by periods.
>
> In your case, avg points to an array of double values. The number of
> elements in the array is periods. So you have to write a cycle to sum
> all the periods elements together. At last, you can divide by periods.

That's what I don't know how to do. Write a cycle. It's these little caveats 
in C that confuse me most. Functions I am grasping. Writing cycles not so 
much.

>> int main(void)
>> {
>>      double a[] = 2.2, 4.2, 5.00;
>                     ^
>
> You are initializing an array. In C you can do that with curly
> backets {}:
>   double a[] = { 2.2, 4.2, 5.00 };
>
>>      mean(a, 3);
>                ^
>
> In this case you are passing the number of elements of a[] array. I
> suggest to use a syntax like the following:
>
>   mean(a, sizeof(a) / sizeof(a[0]));

What's the sizeof(a[0]) mean?

> or define a generic macro if you're going to use this syntax in
> several points:
>
>   #define SIZEARRAY(a)  (sizeof(a) / sizeof(a[0]))
>
> In this way, you can modify the number of elements in the
> initialization, but you haven't to modify the number of elements that
> will be automatically calculated by the preprocessor.
>
> Of course, this is valid only for static allocated array. You you
> dinamically allocate an array, you are responsible to take its lenght
> in some variable.
>
>> }
>> p.c: In function `main':
>> p.c:12: invalid initializer
>> p.c:12: parse error before numeric constant
>>
>>      What's it trying to tell me?
>
> See above. 

[toc] | [prev] | [next] | [standalone]


#4938

FromShao Miller <sha0.miller@gmail.com>
Date2011-05-28 15:33 -0500
Message-ID<irrm6t$17v$1@dont-email.me>
In reply to#4933
On 5/28/2011 3:12 PM, Bill Cunningham wrote:
> pozz wrote:
>> Il 28/05/2011 21:28, Bill Cunningham ha scritto:
>>> void mean(double *avg, int periods)
>>> {
>>>       double val;
>>>       val = *avg / periods;
>>>       printf("%.2f\n", val);
>>> }
>>
>> avg is a pointer to double. *avg is a double. If you write
>>
>>    *avg / periods
>>
>> your dividing one double value by periods.
>>
>> In your case, avg points to an array of double values. The number of
>> elements in the array is periods. So you have to write a cycle to sum
>> all the periods elements together. At last, you can divide by periods.
>
> That's what I don't know how to do. Write a cycle. It's these little caveats
> in C that confuse me most. Functions I am grasping. Writing cycles not so
> much.
>

"A cycle" might also be called "a loop".

   double mean(double * values, int periods) {
       double accumulator;
       int i;
       double average;

       accumulator = 0;
       for (i = 0; i < periods; ++i)
         accumulator += values[i];
       average = accumulator / periods;
       return average;
     }

>>> int main(void)
>>> {
>>>       double a[] = 2.2, 4.2, 5.00;
>>                      ^
>>
>> You are initializing an array. In C you can do that with curly
>> backets {}:
>>    double a[] = { 2.2, 4.2, 5.00 };
>>
>>>       mean(a, 3);
>>                 ^
>>
>> In this case you are passing the number of elements of a[] array. I
>> suggest to use a syntax like the following:
>>
>>    mean(a, sizeof(a) / sizeof(a[0]));
>
> What's the sizeof(a[0]) mean?

Given:

   double a[] = { 2.2, 4.2, 5.00 };

'sizeof a' yields the size of the 'a' array, in bytes.  'sizeof a[0]' 
(or 'sizeof *a') gives the size of a single element of the 'a' array, in 
bytes.  Since a single element is a 'double' in this case, 'sizeof a[0]' 
gives the same value as 'sizeof (double)'.

If you divide the size of the array by the size of one of its elements, 
you get the number of elements in the array.

>
>> or define a generic macro if you're going to use this syntax in
>> several points:
>>
>>    #define SIZEARRAY(a)  (sizeof(a) / sizeof(a[0]))
>>

Thus, the above macro gives the number of elements in an array.  Same 
idea as:

   #define COUNTOF(array) (sizeof (array) / sizeof *(array))

[toc] | [prev] | [next] | [standalone]


#4941

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-28 17:12 -0400
Message-ID<4de16581$0$1969$bbae4d71@news.suddenlink.net>
In reply to#4938
Shao Miller wrote:
> On 5/28/2011 3:12 PM, Bill Cunningham wrote:
>> pozz wrote:
>>> Il 28/05/2011 21:28, Bill Cunningham ha scritto:
>>>> void mean(double *avg, int periods)
>>>> {
>>>>       double val;
>>>>       val = *avg / periods;
>>>>       printf("%.2f\n", val);
>>>> }
>>>
>>> avg is a pointer to double. *avg is a double. If you write
>>>
>>>    *avg / periods
>>>
>>> your dividing one double value by periods.
>>>
>>> In your case, avg points to an array of double values. The number of
>>> elements in the array is periods. So you have to write a cycle to
>>> sum all the periods elements together. At last, you can divide by
>>> periods.
>>
>> That's what I don't know how to do. Write a cycle. It's these little
>> caveats in C that confuse me most. Functions I am grasping. Writing
>> cycles not so much.
>>
>
> "A cycle" might also be called "a loop".
>
>   double mean(double * values, int periods) {
>       double accumulator;
>       int i;
>       double average;
>
>       accumulator = 0;
>       for (i = 0; i < periods; ++i)
>         accumulator += values[i];    <-
>       average = accumulator / periods;
>       return average;
>     }

    I'm a little confused here. accumulator+accumulator=values[i];
The i is being iterated. The accumulator is where the value is being stored 
right?

Bill

[toc] | [prev] | [next] | [standalone]


#4943

FromGeoff <geoff@invalid.invalid>
Date2011-05-28 14:26 -0700
Message-ID<d1q2u6duggde34n7chcqdukjj5havab3m4@4ax.com>
In reply to#4941
On Sat, 28 May 2011 17:12:45 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>Shao Miller wrote:
>> On 5/28/2011 3:12 PM, Bill Cunningham wrote:
>>> pozz wrote:
>>>> Il 28/05/2011 21:28, Bill Cunningham ha scritto:
>>>>> void mean(double *avg, int periods)
>>>>> {
>>>>>       double val;
>>>>>       val = *avg / periods;
>>>>>       printf("%.2f\n", val);
>>>>> }
>>>>
>>>> avg is a pointer to double. *avg is a double. If you write
>>>>
>>>>    *avg / periods
>>>>
>>>> your dividing one double value by periods.
>>>>
>>>> In your case, avg points to an array of double values. The number of
>>>> elements in the array is periods. So you have to write a cycle to
>>>> sum all the periods elements together. At last, you can divide by
>>>> periods.
>>>
>>> That's what I don't know how to do. Write a cycle. It's these little
>>> caveats in C that confuse me most. Functions I am grasping. Writing
>>> cycles not so much.
>>>
>>
>> "A cycle" might also be called "a loop".
>>
>>   double mean(double * values, int periods) {
>>       double accumulator;
>>       int i;
>>       double average;
>>
>>       accumulator = 0;
>>       for (i = 0; i < periods; ++i)
>>         accumulator += values[i];    <-
>>       average = accumulator / periods;
>>       return average;
>>     }
>
>    I'm a little confused here. accumulator+accumulator=values[i];
>The i is being iterated. The accumulator is where the value is being stored 
>right?
>
>Bill
>

It would be better written as:

void mean(double *array, int periods)
{
	int i;
	double mean;
	double sum;
	
	sum = 0;
	for (i=0; i < periods; i++)
		sum = sum + array[i];

	mean = sum / periods;

	printf("%.2f\n", mean);
}

[toc] | [prev] | [next] | [standalone]


#4944

FromShao Miller <sha0.miller@gmail.com>
Date2011-05-28 16:29 -0500
Message-ID<irrpfj$lhi$1@dont-email.me>
In reply to#4941
On 5/28/2011 4:12 PM, Bill Cunningham wrote:
> Shao Miller wrote:
>> On 5/28/2011 3:12 PM, Bill Cunningham wrote:
>>> pozz wrote:
>>>> Il 28/05/2011 21:28, Bill Cunningham ha scritto:
>>>>> void mean(double *avg, int periods)
>>>>> {
>>>>>        double val;
>>>>>        val = *avg / periods;
>>>>>        printf("%.2f\n", val);
>>>>> }
>>>>
>>>> avg is a pointer to double. *avg is a double. If you write
>>>>
>>>>     *avg / periods
>>>>
>>>> your dividing one double value by periods.
>>>>
>>>> In your case, avg points to an array of double values. The number of
>>>> elements in the array is periods. So you have to write a cycle to
>>>> sum all the periods elements together. At last, you can divide by
>>>> periods.
>>>
>>> That's what I don't know how to do. Write a cycle. It's these little
>>> caveats in C that confuse me most. Functions I am grasping. Writing
>>> cycles not so much.
>>>
>>
>> "A cycle" might also be called "a loop".
>>
>>    double mean(double * values, int periods) {
>>        double accumulator;
>>        int i;
>>        double average;
>>
>>        accumulator = 0;
>>        for (i = 0; i<  periods; ++i)
>>          accumulator += values[i];<-
>>        average = accumulator / periods;
>>        return average;
>>      }
>
>      I'm a little confused here. accumulator+accumulator=values[i];
> The i is being iterated. The accumulator is where the value is being stored
> right?

The line:

   accumulator += values[i];

is similar to:

   accumulator = accumulator + values[i];

and not to:

   accumulator + accumulator = values[i];

Yes.  As its name suggests, the 'accumulator' is accumulating every 
value in the array; it's the sum.

[toc] | [prev] | [next] | [standalone]


#4961

FromJoe Pfeiffer <pfeiffer@cs.nmsu.edu>
Date2011-05-28 17:26 -0600
Message-ID<1bvcwufluj.fsf@snowball.wb.pfeifferfamily.net>
In reply to#4941
"Bill Cunningham" <nospam@nspam.invalid> writes:

> Shao Miller wrote:

>>         accumulator += values[i];    <-
>
>     I'm a little confused here. accumulator+accumulator=values[i];
> The i is being iterated. The accumulator is where the value is being stored 
> right?

No, it means

           accumulator = accumulator + values[i];
-- 
"Erwin, do you know what happened to the cat?" -- Mrs. Shroedinger

[toc] | [prev] | [next] | [standalone]


#4942

FromGeoff <geoff@invalid.invalid>
Date2011-05-28 14:20 -0700
Message-ID<rdp2u6pop1qrg1spa2pb5rltnurclcbqlb@4ax.com>
In reply to#4933
On Sat, 28 May 2011 16:12:03 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>>   mean(a, sizeof(a) / sizeof(a[0]));
>
>What's the sizeof(a[0]) mean?

It's the size, in bytes, (whatever they are), of the first element of
the array "a". It's better than writing sizeof(double) because if you
change the declaration of "a" from double a[] to float a[] you don't
have to change the calculation of the size.

sizeof(a) / sizeof(a[0]) is "the total length of the array divided by
the length of any member of the array" (i.e., the count of the members
of the array).

[toc] | [prev] | [next] | [standalone]


#4976

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-29 02:16 -0400
Message-ID<4de1e503$0$1976$bbae4d71@news.suddenlink.net>
In reply to#4942
Geoff wrote:
> On Sat, 28 May 2011 16:12:03 -0400, "Bill Cunningham"
> <nospam@nspam.invalid> wrote:
>
>>>   mean(a, sizeof(a) / sizeof(a[0]));
>>
>> What's the sizeof(a[0]) mean?
>
> It's the size, in bytes, (whatever they are), of the first element of
> the array "a". It's better than writing sizeof(double) because if you
> change the declaration of "a" from double a[] to float a[] you don't
> have to change the calculation of the size.
>
> sizeof(a) / sizeof(a[0]) is "the total length of the array divided by
> the length of any member of the array" (i.e., the count of the members
> of the array).

    Oh Ok I see. 

[toc] | [prev] | [next] | [standalone]


#5016

FromLew Pitcher <lpitcher@teksavvy.com>
Date2011-05-28 16:35 -0400
Message-ID<tEwEp.6367$4h1.987@newsfe06.iad>
In reply to#4933
On May 28, 2011 16:12, in comp.lang.c, nospam@nspam.invalid wrote:

> pozz wrote:
>> Il 28/05/2011 21:28, Bill Cunningham ha scritto:
>>> void mean(double *avg, int periods)
>>> {
>>>      double val;
>>>      val = *avg / periods;
>>>      printf("%.2f\n", val);
>>> }
>>
>> avg is a pointer to double. *avg is a double. If you write
>>
>>   *avg / periods
>>
>> your dividing one double value by periods.
>>
>> In your case, avg points to an array of double values. The number of
>> elements in the array is periods. So you have to write a cycle to sum
>> all the periods elements together. At last, you can divide by periods.
> 
> That's what I don't know how to do. Write a cycle.

Bill, you've done it before. Why can't you write a "cycle" (aka a "loop")
this time?

Think
  for (....) {/*something*/}
or
  while (....) {/*something*/}
or
  do {/*something*/}  while (....);


> It's these little 
> caveats in C that confuse me most. Functions I am grasping. Writing cycles
> not so much.
> 
>>> int main(void)
>>> {
>>>      double a[] = 2.2, 4.2, 5.00;
>>                     ^
>>
>> You are initializing an array. In C you can do that with curly
>> backets {}:
>>   double a[] = { 2.2, 4.2, 5.00 };
>>
>>>      mean(a, 3);
>>                ^
>>
>> In this case you are passing the number of elements of a[] array. I
>> suggest to use a syntax like the following:
>>
>>   mean(a, sizeof(a) / sizeof(a[0]));
> 
> What's the sizeof(a[0]) mean?

Again, you know this.

What does sizeof(int) mean?
What does sizeof(double) mean?
What does sizeof("string") mean?

>> or define a generic macro if you're going to use this syntax in
>> several points:
>>
>>   #define SIZEARRAY(a)  (sizeof(a) / sizeof(a[0]))
>>
>> In this way, you can modify the number of elements in the
>> initialization, but you haven't to modify the number of elements that
>> will be automatically calculated by the preprocessor.
>>
>> Of course, this is valid only for static allocated array. You you
>> dinamically allocate an array, you are responsible to take its lenght
>> in some variable.
>>
>>> }
>>> p.c: In function `main':
>>> p.c:12: invalid initializer
>>> p.c:12: parse error before numeric constant
>>>
>>>      What's it trying to tell me?
>>
>> See above.
> 
> 

-- 
Lew Pitcher
Master Codewright & JOAT-in-training   | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
----------      Slackware - Because I know what I'm doing.         ------

[toc] | [prev] | [next] | [standalone]


#5020

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-29 15:50 -0400
Message-ID<4de2a3da$0$1966$bbae4d71@news.suddenlink.net>
In reply to#5016
Lew Pitcher wrote:
> On May 28, 2011 16:12, in comp.lang.c, nospam@nspam.invalid wrote:
>
>> pozz wrote:
>>> Il 28/05/2011 21:28, Bill Cunningham ha scritto:
>>>> void mean(double *avg, int periods)
>>>> {
>>>>      double val;
>>>>      val = *avg / periods;
>>>>      printf("%.2f\n", val);
>>>> }
>>>
>>> avg is a pointer to double. *avg is a double. If you write
>>>
>>>   *avg / periods
>>>
>>> your dividing one double value by periods.
>>>
>>> In your case, avg points to an array of double values. The number of
>>> elements in the array is periods. So you have to write a cycle to
>>> sum all the periods elements together. At last, you can divide by
>>> periods.
>>
>> That's what I don't know how to do. Write a cycle.
>
> Bill, you've done it before. Why can't you write a "cycle" (aka a
> "loop") this time?

    I know but its been a long long time. I got away from C for a long time 
awhile back. Burned out or something. And remember I am a hobyist. I have 
other things in my life besides C.

> Think
>  for (....) {/*something*/}
> or
>  while (....) {/*something*/}
> or
>  do {/*something*/}  while (....);
>
>
>> It's these little
>> caveats in C that confuse me most. Functions I am grasping. Writing
>> cycles not so much.
>>
>>>> int main(void)
>>>> {
>>>>      double a[] = 2.2, 4.2, 5.00;
>>>                     ^
>>>
>>> You are initializing an array. In C you can do that with curly
>>> backets {}:
>>>   double a[] = { 2.2, 4.2, 5.00 };
>>>
>>>>      mean(a, 3);
>>>                ^
>>>
>>> In this case you are passing the number of elements of a[] array. I
>>> suggest to use a syntax like the following:
>>>
>>>   mean(a, sizeof(a) / sizeof(a[0]));
>>
>> What's the sizeof(a[0]) mean?
>
> Again, you know this.
>
> What does sizeof(int) mean?
> What does sizeof(double) mean?
> What does sizeof("string") mean?
>
>>> or define a generic macro if you're going to use this syntax in
>>> several points:
>>>
>>>   #define SIZEARRAY(a)  (sizeof(a) / sizeof(a[0]))
>>>
>>> In this way, you can modify the number of elements in the
>>> initialization, but you haven't to modify the number of elements
>>> that will be automatically calculated by the preprocessor.
>>>
>>> Of course, this is valid only for static allocated array. You you
>>> dinamically allocate an array, you are responsible to take its
>>> lenght in some variable.
>>>
>>>> }
>>>> p.c: In function `main':
>>>> p.c:12: invalid initializer
>>>> p.c:12: parse error before numeric constant
>>>>
>>>>      What's it trying to tell me?
>>>
>>> See above. 

[toc] | [prev] | [next] | [standalone]


#5029

FromJoe Pfeiffer <pfeiffer@cs.nmsu.edu>
Date2011-05-29 20:39 -0600
Message-ID<1bk4d852tt.fsf@snowball.wb.pfeifferfamily.net>
In reply to#5016
Lew Pitcher <lpitcher@teksavvy.com> writes:
>
> Bill, you've done it before. Why can't you write a "cycle" (aka a "loop")
> this time?

I'm not Bill, but calling it a "cycle" threw me completely.  I had no
idea what he was talking about.
-- 
"Erwin, do you know what happened to the cat?" -- Mrs. Shroedinger

[toc] | [prev] | [next] | [standalone]


#4939

FromGeoff <geoff@invalid.invalid>
Date2011-05-28 13:46 -0700
Message-ID<sin2u6lpn35s851tau49f4vaak2351tfal@4ax.com>
In reply to#4924
On Sat, 28 May 2011 15:28:24 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>    I am trying here to calculate a mean of 3 values. I do not understand 
>the errors the compiler returned I've never seen them.
>
>#include <stdio.h>
>
>void mean(double *avg, int periods)
>{
>    double val;
>    val = *avg / periods;
>    printf("%.2f\n", val);
>}
>
>int main(void)
>{
>    double a[] = 2.2, 4.2, 5.00;
>    mean(a, 3);
>}
>p.c: In function `main':
>p.c:12: invalid initializer
>p.c:12: parse error before numeric constant
>
>    What's it trying to tell me?
>

It's telling you the array of doubles, a[] was initialized
incorrectly. You must use braces to initialize an array.
The parse error is a secondary effect of the bad syntax of your array
initialization.

[toc] | [prev] | [next] | [standalone]


#4945

FromNobody <nobody@nowhere.com>
Date2011-05-28 23:06 +0100
Message-ID<pan.2011.05.28.22.05.43.62000@nowhere.com>
In reply to#4924
On Sat, 28 May 2011 15:28:24 -0400, Bill Cunningham wrote:

> void mean(double *avg, int periods)
> {
>     double val;
>     val = *avg / periods;
>     printf("%.2f\n", val);
> }
> }

Note the excess closing brace.

[toc] | [prev] | [next] | [standalone]


#4947

FromIan Collins <ian-news@hotmail.com>
Date2011-05-29 10:10 +1200
Message-ID<94da7jFr2iU5@mid.individual.net>
In reply to#4945
On 05/29/11 10:06 AM, Nobody wrote:
> On Sat, 28 May 2011 15:28:24 -0400, Bill Cunningham wrote:
>
>> void mean(double *avg, int periods)
>> {
>>      double val;
>>      val = *avg / periods;
>>      printf("%.2f\n", val);
>> }
>> }
>
> Note the excess closing brace.
>
There wasn't one to note in the OP.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#4946

FromIan Collins <ian-news@hotmail.com>
Date2011-05-29 10:09 +1200
Message-ID<94da4tFr2iU4@mid.individual.net>
In reply to#4924
On 05/29/11 07:28 AM, Bill Cunningham wrote:
>      I am trying here to calculate a mean of 3 values. I do not understand
> the errors the compiler returned I've never seen them.
>
> #include<stdio.h>
>
> void mean(double *avg, int periods)
> {
>      double val;
>      val = *avg / periods;
>      printf("%.2f\n", val);
> }
>
> int main(void)
> {
>      double a[] = 2.2, 4.2, 5.00;
>      mean(a, 3);
> }
> p.c: In function `main':
> p.c:12: invalid initializer
> p.c:12: parse error before numeric constant
>
>      What's it trying to tell me?

To get a C programming bog and read it.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#4950

FromAngel <angel+news@spamcop.net>
Date2011-05-28 22:28 +0000
Message-ID<slrniu2toc.6e6.angel+news@pearlgates.net>
In reply to#4924
On 2011-05-28, Bill Cunningham <nospam@nspam.invalid> wrote:
>
> int main(void)
> {
>     double a[] = 2.2, 4.2, 5.00;
>     mean(a, 3);
> }
> p.c: In function `main':
> p.c:12: invalid initializer
> p.c:12: parse error before numeric constant
>
>     What's it trying to tell me?

That you should get a good tutorial on the syntax of C, and read it.

The error means literally what it says, you initialized your array
wrongly. An array initializer must be between accolades.


-- 
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

[toc] | [prev] | [next] | [standalone]


#4951

FromIan Collins <ian-news@hotmail.com>
Date2011-05-29 10:37 +1200
Message-ID<94dbp1Fr2iU6@mid.individual.net>
In reply to#4950
On 05/29/11 10:28 AM, Angel wrote:
> On 2011-05-28, Bill Cunningham<nospam@nspam.invalid>  wrote:
>>
>> int main(void)
>> {
>>      double a[] = 2.2, 4.2, 5.00;
>>      mean(a, 3);
>> }
>> p.c: In function `main':
>> p.c:12: invalid initializer
>> p.c:12: parse error before numeric constant
>>
>>      What's it trying to tell me?
>
> That you should get a good tutorial on the syntax of C, and read it.
>
> The error means literally what it says, you initialized your array
> wrongly. An array initializer must be between accolades.

accolades?

I guess you aren't a native English speaker.  In English an accolade is 
some form of award or praise!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#4953

FromAngel <angel+news@spamcop.net>
Date2011-05-28 22:45 +0000
Message-ID<slrniu2uo6.6e6.angel+news@pearlgates.net>
In reply to#4951
On 2011-05-28, Ian Collins <ian-news@hotmail.com> wrote:
> On 05/29/11 10:28 AM, Angel wrote:
>>
>> The error means literally what it says, you initialized your array
>> wrongly. An array initializer must be between accolades.
>
> accolades?
>
> I guess you aren't a native English speaker.  In English an accolade is 
> some form of award or praise!

Dutch, actually. And here an accolade is one of those curly brace
thingies you put around statement blocks and array initializers. :-)

According to Wikipedia, it has that meaning in English too, but only in
the context of muscial notation. :-)

https://secure.wikimedia.org/wikipedia/en/wiki/Accolade_%28notation%29#Lines


-- 
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c


csiph-web