# Help a beginner - function with pointer ...

Discussion in 'C Programming' started by bpascal123@googlemail.com, Jul 13, 2009.

1. ### Guest

Hi,

I'm learning how to use functions.
Could someone take a quick look at this code below and help me
understand this line :

Sum((float*)T, 3, 4) );

I can't figure out (float*) ... is it float *T ? I understand float is
telling the type of T which is an array but why "*" at the end ? When
it's declared it says *A, there I understand but on calling the
function Sum I then get confuse.

Thanks if you can bring me some light,

Pascal

#include <stdio.h>
main()
{

float Sum(float *A, int N, int M);

float T[3][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12}};

printf("Sum of : %f \n",
Sum((float*)T, 3, 4) );
return 0;
}

, Jul 13, 2009

2. ### Eric SosmanGuest

wrote:
> Hi,
>
> I'm learning how to use functions.
> Could someone take a quick look at this code below and help me
> understand this line :
>
> Sum((float*)T, 3, 4) );
> [...]

This is Question 6.18 in the comp.lang.c Frequently Asked
Questions (FAQ) list <http://www.c-faq.com/aryptr/index.html>,
It may or may not answer all the questions you have, but read
and ponder it first, and then come back with any puzzlements
that remain.

--
Eric Sosman
lid

Eric Sosman, Jul 13, 2009

3. ### Nick KeighleyGuest

On 13 July, 01:04, ""
<> wrote:

> I'm learning how to use functions.
> Could someone take a quick look at this code below and help me
> understand this line :
>
> Sum((float*)T, 3, 4) );

that's a syntax error

> I can't figure out (float*) ... is it float *T ?

I've no idea what "is ir float *T ?" means.
It's a cast. This means (loosly) that whatever type T was
it has been converted to type float* (pter-to-float).
This is almost always bad practice. The code you are
posting is riddled with errors and poor practice.
Get a better text to learn C from. For intance
"The C Programming Language" by Kernighan and Richie
2nd edition. This won't surprise you by using features
you haven't yet been taught.

> I understand float is
> telling the type of T which is an array but why "*" at the end ?

* means pointer. To an extent array notation can be replaced by
pointer
notaion. But be wary many texts tell you "arrays are pointers", which
is wrong.

> When
> it's declared it says *A, there I understand but on calling the
> function Sum I then get confuse.

you need to review basic C declarations.

> Thanks if you can bring me some light,

> #include <stdio.h>
> main()

int main (void)

I'm pretty sure you've been told this before

> {
>
>  float Sum(float *A, int N, int M);

why float. Why not double? (float is hardly ever necessary).
Why is the function name capitalised? Why are the arguments
capitalised?

>  float T[3][4] = {{1, 2, 3, 4},
>                   {5, 6, 7, 8},
>                   {9,10,11,12}};

why a capital T?

>  printf("Sum of : %f \n",
>                     Sum((float*)T, 3, 4) );

why the cast? It's a bad idea to force a 2D array into
a scalar variable

>  return 0;
> }

get a better text book

Nick Keighley, Jul 13, 2009
4. ### Keith ThompsonGuest

Nick Keighley <> writes:
> On 13 July, 01:04, ""
> <> wrote:
>
>> I'm learning how to use functions.
>> Could someone take a quick look at this code below and help me
>> understand this line :
>>
>> Sum((float*)T, 3, 4) );

>
> that's a syntax error

No, it isn't. T is an expression, (float*)T is a cast expression, the
whole thing is a function call.

>> I can't figure out (float*) ... is it float *T ?

>
> I've no idea what "is ir float *T ?" means.
> It's a cast. This means (loosly) that whatever type T was
> it has been converted to type float* (pter-to-float).
> This is almost always bad practice. The code you are
> posting is riddled with errors and poor practice.
> Get a better text to learn C from. For intance
> "The C Programming Language" by Kernighan and Richie
> 2nd edition. This won't surprise you by using features
> you haven't yet been taught.

Here's the original program:

#include <stdio.h>
main()
{

float Sum(float *A, int N, int M);

float T[3][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12}};

printf("Sum of : %f \n",
Sum((float*)T, 3, 4) );
return 0;
}

The expression T is of type float (*)[4] (pointer to array of
4 float). It points to the first element of a 2-dimensional array
of float. The expression (float*)T converts it so that it appears
to point to the first element of an array of float. Presumably
the function Sum (whose definition we haven't seen) treats its
parameter as a pointer to the first element of a one-dimensional
array of float.

a definition for Sum) is that it uses type-punning to treat a
two-dimensional array as a one-dimensional array. The language
guarantees that the representations will be consistent, but strictly
speaking the behavior is undefined. But it's a fairly common
technique, it probably will work with most or all implementations,
and it's the most nearly portable way I can think of to write a
Sum function that works on arbitrary 2-dimensional arrays.

Here's a complete program, based on the above, that actually works
on at least one implementation, and I'd be surprised to see an
implementation where it doesn't. The only changes I've made are
to fix the declaration of main, increase the indentation, and add
a definition for Sum. (There are several other changes I'd make.)

#include <stdio.h>
int main(void)
{

float Sum(float *A, int N, int M);

float T[3][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12}};

printf("Sum of : %f \n",
Sum((float*)T, 3, 4) );
return 0;
}

float Sum(float *A, int N, int M) {
float result = 0;
int i;
for (i = 0; i < N*M; i ++) {
result += A;
}
return result;
}

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jul 13, 2009
5. ### Keith ThompsonGuest

Eric Sosman <> writes:
> Keith Thompson wrote:
>> Nick Keighley <> writes:
>>> On 13 July, 01:04, ""
>>> <> wrote:
>>>
>>>> I'm learning how to use functions.
>>>> Could someone take a quick look at this code below and help me
>>>> understand this line :
>>>>
>>>> Sum((float*)T, 3, 4) );
>>> that's a syntax error

>>
>> No, it isn't. T is an expression, (float*)T is a cast expression, the
>> whole thing is a function call.

>
> When you take another look, I think your first words
> will probably be "Oh, (expletive deleted))!"

Yes those were my first words. My second words were "Nope, still not
a syntax error.

Here's the context from the original program:

printf("Sum of : %f \n",
Sum((float*)T, 3, 4) );

The quoted line would be a syntax error if it were a statement by
itself, but it's not. It's the second line of a two-line statement,
and the statement as a whole is valid.

The OP asked about "this line". The line, in context, is valid.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jul 13, 2009
6. ### Guest

Hi all,

Sorry for not following this post i have initiated. Here is the full
code so you can see where I was confused.

When i sent this post, i thought (float *) T would be appealing to
some C expert programmer like some inside c convention such as a
+=1 ... i didn't even try to compil the code before sending it to you
let say for validation ; my fault

#include <stdio.h>
main()
{

float Sum(float *A, int N, int M);

float T[3][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12}};

printf("Sum des éléments : %f \n",
Sum((float*)T, 3, 4) ); // Is this (float*) a type
casting for T ???
return 0;

}

float Sum(float *A, int N, int M)
{
int I;
float S;

for (I=0; I<N*M; I++)
S += A;
return S;
}

, Jul 14, 2009
7. ### Nick KeighleyGuest

On 14 July, 13:27, ""
<> wrote:
> Hi all,
>
> Sorry for not following this post i have initiated. Here is the full
> code so you can see where I was confused.
>
> When i sent this post, i thought (float *) T would be appealing to
> some C expert programmer like some inside c convention such as a
> +=1 ... i didn't even try to compil the code before sending it to you
> let say for validation ; my fault
>
> #include <stdio.h>
> main()
> {
>
>  float Sum(float *A, int N, int M);
>
>  float T[3][4] = {{1, 2, 3, 4},
>                   {5, 6, 7, 8},
>                   {9,10,11,12}};
>
>  printf("Sum des éléments : %f \n",
>                     Sum((float*)T, 3, 4) ); // Is this (float*) a type
> casting for T ???

what did I say in my previous post?

>  return 0;
>
> }
>
> float Sum(float *A, int N, int M)
> {
>  int I;
>  float S;
>
>  for (I=0; I<N*M; I++)
>       S += A;
>  return S;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

Nick Keighley, Jul 14, 2009
8. ### Eric SosmanGuest

wrote:
> Hi all,
>
> Sorry for not following this post i have initiated. Here is the full
> code so you can see where I was confused.
>
> When i sent this post, i thought (float *) T would be appealing to
> some C expert programmer like some inside c convention such as a
> +=1 ... i didn't even try to compil the code before sending it to you
> let say for validation ; my fault
>
> #include <stdio.h>
> main()
> {
>
> float Sum(float *A, int N, int M);
>
> float T[3][4] = {{1, 2, 3, 4},
> {5, 6, 7, 8},
> {9,10,11,12}};
>
> printf("Sum des éléments : %f \n",
> Sum((float*)T, 3, 4) ); // Is this (float*) a type
> casting for T ???
> return 0;
>
> }
>
> float Sum(float *A, int N, int M)
> {
> int I;
> float S;
>
> for (I=0; I<N*M; I++)
> S += A;
> return S;
> }

It looks like there are two substantive questions here:
(1) What does the cast mean? and (2) Why is it used?

The cast is an operator taking one operand and producing
a value. The result is the value obtained by converting the
operand value to a different (usually) type. For example,
`(int) 3.14159' takes a `double' value approximately equal
to pi and converts it to an `int'; the result is the `int'
value three. `(float) 3.14159' takes the same `double' value
and converts it to a `float'; the result is again approximately
equal to pi but probably slightly different from the original.

The cast `(float*) T' is slightly subtler, mostly because
the values of pointers are harder to "see" -- It's easy to
notice that converting a `double' to an `int' discards the
fractional part, and it's not much harder to see that
converting a `double' to a `float' may yield a slightly
coarser approximation, but pointer values are a little harder
to observe because they are usually described not in their
own terms but in terms of the things they point at. Still,
`(float*) T' operates just like the other casts: It takes the
value of `T' as an operand, converts the value to a `float*',
and yields the converted value as a result.

... which leads to two subordinate questions: (1a) What
is the value of `T'? and (1b) What value results from converting
it to a `float*'?

The identifier `T' refers to a two-dimensional array whose
elements are `float' values. Well, sort of: To be more exact,
`T' refers to a one-dimensional array whose elements are also
one-dimensional arrays, and those "inner" arrays have elements
that are `float' values. So `T' refers to an array of arrays
of `float'.

Now we encounter "The Rule:" In all but a few contexts,
using the name of an array in an expression is the same as
using a pointer to the array's first element. That is, when
you write `T' as the operand of the cast, it is just as if you
had written `&T[0]'. What is `T[0]'? It is the first element
of the array `T', namely, an "inner" array of four `float'.
So, what is `&T[0]'? A pointer to that four-element array.
What is the type of the implied expression `&T[0]'? Is it
`float*'? No, because `T[0]' is an array, not a `float'.
So the type of `&T[0]' is "pointer to array of four `float'
values."

So, writing `T' is equivalent to writing `&T[0]', and
that in turn is a pointer value that refers to four values
in adjacent memory locations. The `(float*)' cast converts
this value to a pointer of a different type: A pointer that
refers to just one `float' value, the first of the four in
the array `T[0]'. The converted value points to the same
memory location as the original (which is another reason the
change is hard to discern), but the extent of the thing it
points at is smaller: One value instead of four. As the
programmer you happen to know that three more values follow
this one, but the converted pointer addresses them one at a

(All that writing, and we're only now getting around to
Question 2! It's said that brevity is the soul of wit, so
I must be either witless or soulless ...)

Why convert the pointer value from one type to another?
Look at the Sum() function: It's first parameter is a `float*',
a pointer to a single `float' value. If you had tried to use
plain `T' as the corresponding argument, the compiler would
have protested: Writing `T' is the same as writing `&T[0]',
the type of that expression is "pointer to array of four
`float' values," and that is not the same as "pointer to a
single `float'." Since the argument type does not match the
parameter type and can't be converted automatically, the
compiler complains that you have provided an unusable argument.
This is usually a Good Thing: Try `fprintf("Hello, world!\n")'
and see how helpful it is when the compiler spots the error
for you.

But in this case, Sum() wants a pointer to the first
`float' in an array of `M*N' values. Writing `(float*)T'
supplies the needed conversion from "pointer to array" to
"pointer to `float'," and all is well. The Sum() function
treats its parameter as a pointer to the start of an array
of `M*N' values, adds them, and returns their sum. (In truth,
there's an error: The variable `S' is never initialized, so
it "holds garbage," and there's no telling what you'll get
when you start adding the `float' values to that garbage.
Inserting `S = 0;' will fix it.)

Finally, the un-asked question: Is this pointer-punning
a good idea? Technically, no: When Sum() steps smoothly from
the last element of `T[0]' to the first of `T[1]', it invokes
undefined behavior. It was given a pointer to the first value
in `T[0]', and it is free to use that pointer to access the
elements of `T[0]', but as soon as it attempts to access the
non-existent element `T[0][4]' it is using an index value that
is outside the given array. In principle, anything can happen.

... except that nothing (bad) ever does. There is no
`T[0][4]', but the memory slot it would occupy if it existed
is in fact present, and occupied by `T[1][0]', the first
`float' in `T[1]'. Technically, Sum() has walked off the
end of `T[0]' and fallen into uncharted waters, but in actual
practice it glides smoothly through the elements of `T[1]'
and `T[2]' without even a ripple. Treating an array that has
"internal structure" as if it were just a big undifferentiated
one-dimensional array is Technically Wrong, but is always done
and always works. Philosophers of programming languages might
trace the necessity for this sort of subterfuge to C's weak
support for arrays; I'm not that much of a philosopher. So go
ahead and use the suspect technique, remembering always to say
"Nudge, nudge, wink, wink" when you do.

--
Eric Sosman
lid

Eric Sosman, Jul 14, 2009
9. ### Ben BacarisseGuest

"" <> writes:

> Sorry for not following this post i have initiated. Here is the full
> code so you can see where I was confused.
>
> When i sent this post, i thought (float *) T would be appealing to
> some C expert programmer like some inside c convention such as a
> +=1 ... i didn't even try to compil the code before sending it to you
> let say for validation ; my fault
>
> #include <stdio.h>
> main()

int main(void) is better. Leaving off the return type is not valid in
C99 (the latest standard) but // are a C99 feature so can't use // and
leave off the return type. Anyway, it is always better to be explicit
even if you don't have to be.

> {
>
> float Sum(float *A, int N, int M);
>
> float T[3][4] = {{1, 2, 3, 4},
> {5, 6, 7, 8},
> {9,10,11,12}};
>
> Sum((float*)T, 3, 4) ); // Is this (float*) a type
> casting for T ???

Yes. If you don't want a cast (and I would not) you can write either:

Sum(&T[0][0], 3, 4)

or

Sum(T[0], 3, 4)

That second one will take you a while to work out!

> return 0;
>
> }
>
> float Sum(float *A, int N, int M)
> {
> int I;
> float S;

No initial value.

> for (I=0; I<N*M; I++)
> S += A;
> return S;

You can get anything at all now. Set S = 0 first;

> }

BTW, it is odd to pass two numbers to Sum when you have had to pretend
it a 1-dimensional array of floats. I'd pass one number -- N*M.

If you are happy to use C99, you can use it's variable length array
mechanism and write:

#include <stdio.h>

float Sum(int N, int M, float A[N][M])
{
float S = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
S += A[j];
return S;
}

int main(void)
{
float T[3][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12}};
return 0;
}

If you do a lot of array programming, the features of C99 may well be
worth the loss of portability. There are not many C99 compilers for
Windows, for example.

--
Ben.

Ben Bacarisse, Jul 14, 2009
10. ### Keith ThompsonGuest

Ben Bacarisse <> writes:
> "" <> writes:
>
>> Sorry for not following this post i have initiated. Here is the full
>> code so you can see where I was confused.
>>
>> When i sent this post, i thought (float *) T would be appealing to
>> some C expert programmer like some inside c convention such as a
>> +=1 ... i didn't even try to compil the code before sending it to you
>> let say for validation ; my fault
>>
>> #include <stdio.h>
>> main()

>
> int main(void) is better. Leaving off the return type is not valid in
> C99 (the latest standard) but // are a C99 feature so can't use // and
> leave off the return type.

Well, you can, but you probably shouldn't.

You can't use // comments (a C99-only feature) and implicit int (a
C90-only feature) in the same program *if* you want the program to
conform to some C standard. But most C compilers, in their default
mode, accept both implicit int (to avoid breaking old code) and //
comments (either as an extension to C90 or as a feature of partially
implemented C99 support), so you can probably get away with it.
But that doesn't mean you should. By using a compiler in some
partially conforming mode, where it supports some arbitrary set of
extensions and some arbitrary set of C99 features, you can make it
more difficult to port the code to some other implementation with
a subtly different set of extensions and features, and you can lose
out on diagnostics that can tell you where your code is non-portable.

> Anyway, it is always better to be explicit
> even if you don't have to be.

Agreed.

Well, not always. For example, a cast is more explicit than an
implicit conversion, but it's usually better to depend on the
implicit conversion when it's available.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jul 14, 2009
11. ### Guest

> If you do a lot of array programming, the features of C99 may well be
> worth the loss of portability.  There are not many C99 compilers for
> Windows, for example.
>
> --
> Ben.

Hi Ben,

I find your help very interesting. I went on to test your code as i
find it more coherent as the code i'm learning from (the one with
(float *)T ... ) .

You're saying something about C99 and portability... here is what gcc
(the compiler i use on linux ubuntu - i just switch from gcc dolorie
windows compiling towards linux ubuntu inner gcc compiler because i
think i'm able to replace in my learning process...) so, here is what
the compiler tells :

test12.c: In function ‘Sum’:
test12.c:6: error: ‘for’ loop initial declaration used outside C99
mode
test12.c:7: error: ‘for’ loop initial declaration used outside C99
mode

I didn't try to compile on gcc Windows, maybe it would have worked...

About portability and c99, i'm not sure to understand the relation
between the two. Is C99 the newest c programming trend or norm that
doesn't fully take in consideration portability ?

For now, i'm doing array programming because that's the current
chapters in the books and websites i'm learning from. I hope
programming is not about arrays only.
But as an accountant, arrays are very common in my jobs even if i'm
start to like programming, accounting is still what i have studied.
That's why i concentrate on arrays. But i like to understand anything
i'm learning it helps to make progress.

I've started learning c about 3-4 months ago. I like this language
and and find it quite "versatile" i think. I understand C is playing a
role in embedded systems. I'm not sure what 's the purpose of
learning C ... as an accountant.

I think it will help later to learn javascript if i want to get into
web development or python if i want some to write simple applications
with a starting level in programming. There are many options left,
such as java, ruby... For now, i'm just learning and I hope to be able
to cover the basics.

Thanks,

Pascal

, Jul 14, 2009
12. ### Guest

> If you do a lot of array programming, the features of C99 may well be
> worth the loss of portability.  There are not many C99 compilers for
> Windows, for example.
>
> --
> Ben.

Hi Ben,

I have tried to compile your code which i find more coherent than the
one with (char *) ... but the compiler returns :

test12.c: In function ‘Sum’:
test12.c:6: error: ‘for’ loop initial declaration used outside C99
mode
test12.c:7: error: ‘for’ loop initial declaration used outside C99
mode

It says something about C99... And you're talking about C99 as well. I
'm not sure i understand what C99 has to do. I first have to tell that
i switch from Windows gcc dolorie to Linux Ubuntu inner gcc compiler.
I find more comfortable in my learning process when I have to deal
with conio.h ... and replace it with proper function so it works on
Linux.

From what i understand, C99 is not fully compatible with
portability ?

Portability between Linux, Mac OS and Windows ? But on the other hand,
its features make things easier when dealing with arrays ?

I'm doing only array programming, it happens to be the case because
that's the current chapter i'm learning from. I hope programming is
not about arrays only. However as an accountant, i think i need to
focus on arrays because accounting is a lot about financial reports. I
don't know if C is well suited then.

I'm not sure what i'll be able to do if i'm learning C. I understand C
is a lot about embedded systems.

Learning C might help me learn Javascript if I want to get into Web
development or Ruby or Java or whatever is hot then. Or if I want to
write simple applications with C++ or Python with a entry level in
programming.

Thanks,

Pascal

, Jul 15, 2009
13. ### Flash GordonGuest

wrote:
>> If you do a lot of array programming, the features of C99 may well be
>> worth the loss of portability. There are not many C99 compilers for
>> Windows, for example.
>>
>> --
>> Ben.

>
> Hi Ben,
>
> I find your help very interesting. I went on to test your code as i
> find it more coherent as the code i'm learning from (the one with
> (float *)T ... ) .
>
> You're saying something about C99 and portability... here is what gcc
> (the compiler i use on linux ubuntu - i just switch from gcc dolorie
> windows compiling towards linux ubuntu inner gcc compiler because i
> think i'm able to replace in my learning process...) so, here is what
> the compiler tells :
>
> test12.c: In function ‘Sum’:
> test12.c:6: error: ‘for’ loop initial declaration used outside C99
> mode
> test12.c:7: error: ‘for’ loop initial declaration used outside C99
> mode
>
> I didn't try to compile on gcc Windows, maybe it would have worked...

What you need to do is tell gcc to come as close as it gets to the C99
standard, which the GCC maintainers admit is not all the way. <OT> This
is done with -stc=c99. It's currently telling you that you have not done
this but you are using features from that standard.

> About portability and c99, i'm not sure to understand the relation
> between the two. Is C99 the newest c programming trend or norm that
> doesn't fully take in consideration portability ?

C99 is the current official standard.
<mode=sarcastic>
However, there are a few compilers people occasionally use that have not
yet finished implementing it, like the gcc development team.
There are also a few small software companies that have not even made
much of a start, such as Microsoft.
</mode>

Some people consider that if Microsoft Visual Studio is not even close,
and gcc has not reached full conformance, then using features specific
to C99 is limiting portability.

> For now, i'm doing array programming because that's the current
> chapters in the books and websites i'm learning from. I hope
> programming is not about arrays only.

It is one among very many. An important one, but by no means the only one.

> But as an accountant, arrays are very common in my jobs even if i'm
> start to like programming, accounting is still what i have studied.
> That's why i concentrate on arrays. But i like to understand anything
> i'm learning it helps to make progress.
>
> I've started learning c about 3-4 months ago. I like this language
> and and find it quite "versatile" i think. I understand C is playing a
> role in embedded systems. I'm not sure what 's the purpose of
> learning C ... as an accountant.

As an accountant I can't see much need to know any programming language,
buy a pre-built and configured accounting package. As someone learning
to program, on the other hand, there is a lot that can be learned by
learning to program in C.

> I think it will help later to learn javascript if i want to get into
> web development or python if i want some to write simple applications
> with a starting level in programming. There are many options left,
> such as java, ruby... For now, i'm just learning and I hope to be able
> to cover the basics.

Some languages are based on fundamentally different concepts, and a lot
of languages, including most used for web development, are higher level.
--
Flash Gordon

Flash Gordon, Jul 15, 2009
14. ### Ben BacarisseGuest

"" <> writes:

>> If you do a lot of array programming, the features of C99 may well be
>> worth the loss of portability. Â There are not many C99 compilers for
>> Windows, for example.
>>
>> --
>> Ben.

Snip sig blocks when you reply -- even short one.

<snip>
> You're saying something about C99 and portability... here is what gcc
> (the compiler i use on linux ubuntu - i just switch from gcc dolorie
> windows compiling towards linux ubuntu inner gcc compiler because i
> think i'm able to replace in my learning process...) so, here is what
> the compiler tells :
>
> test12.c: In function â€˜Sumâ€™:
> test12.c:6: error: â€˜forâ€™ loop initial declaration used outside C99
> mode
> test12.c:7: error: â€˜forâ€™ loop initial declaration used outside C99
> mode

This is saying that the code uses C99 features (specifically the
permission to declare a variable in the first clause of a for
statement) but that the compiler was not called in C99 mode (add
-std=c99 to the compile command).

> I didn't try to compile on gcc Windows, maybe it would have
> worked...

As far as this goes, gcc is much the same on all platforms. I.e. it
will work when gcc is invoked in C99 mode on Windows and Linux and you
will get a similar diagnostic in its default mode on both.

> About portability and c99, i'm not sure to understand the relation
> between the two. Is C99 the newest c programming trend or norm that
> doesn't fully take in consideration portability ?

C99 is newest standard. Its lack of portability comes only from the
fact that there are only a few C99 compilers, not from any features of
the language itself. If I were starting out now, I'd learn C99 and
write in the common intersection of C90 and C99 if I need maximum
portability.

<snip>
> I've started learning c about 3-4 months ago. I like this language
> and and find it quite "versatile" i think. I understand C is playing a
> role in embedded systems. I'm not sure what 's the purpose of
> learning C ... as an accountant.

I'd have thought you'd start by writing spreadsheet programs. It's a
remarkably good way to learn the basics of programming, I think.

<snip>
--
Ben.

Ben Bacarisse, Jul 15, 2009
15. ### jameskuyperGuest

wrote:
....
> About portability and c99, i'm not sure to understand the relation
> between the two. Is C99 the newest c programming trend or norm that
> doesn't fully take in consideration portability ?

C99 is a decade-old update to the C standard. While that is the
"newest" version of the standard, there are only a few implementations
that fully conform to C99, so it doesn't qualify as trendy.
Implementations that incompletely conform to C99 are widely available,
but they differ with each other in regard to what subset of the new
C99 features they implement.

So how does this connect to portability? Compilers that can be put
into a mode (usually NOT their default mode) which fully conforms to
C90 are commonplace on virtually every kind of computer platform.
Therefore, it's quite feasible to write code which can be compiled for
virtually any platform, so long as you restrict yourself to features
that are supported in C90. Virtually any program will have portions
that cannot be made portable, but if the program is well designed,
those parts are kept distinct from the rest of the program, minimizing
the amount of re-writing needed when porting to a new platform.

In contrast, code that uses C99 features can only be used on platforms
for which there are compilers that implement those features; the more
C99 features you use, the smaller the number of compilers that
qualify, and therefore the smaller the number of platforms it can be
ported to. Therefore, if portability is important to you, you
shouldn't make use of features that are specific to C99. If it's
REALLY important to you, you shouldn't use any features that are
specific to C90, either - that way your code can be compiled by C99
compilers as well as C90 compilers.

While I understand this point of view, and can explain it, I don't
share it. When given the option to do so (which is not the case where
I work), I will write code that uses C99 features freely, and never
use any C90 feature that is incompatible with C99, and I complain to
the vendor if any of those features don't work on a given compiler.
However, I'm not trying to sell a product to people working on a lot
of different platforms. I can understand the more conservative
approach favored by some of those who are.

jameskuyper, Jul 15, 2009
16. ### jameskuyperGuest

wrote:
> > If you do a lot of array programming, the features of C99 may well be
> > worth the loss of portability. There are not many C99 compilers for
> > Windows, for example.
> >
> > --
> > Ben.

>
> Hi Ben,
>
> I have tried to compile your code which i find more coherent than the
> one with (char *) ... but the compiler returns :
>
> test12.c: In function ‘Sum’:
> test12.c:6: error: ‘for’ loop initial declaration used outside C99
> mode
> test12.c:7: error: ‘for’ loop initial declaration used outside C99
> mode
>
> It says something about C99... And you're talking about C99 as well. I
> 'm not sure i understand what C99 has to do.

The declaration of 'i' inside the for loop:

for (int i = 0; i < N; i++)

is a new feature in C99. In C90, you would have to write something
like:

int i;
for(i=0; i<N; i++)

jameskuyper, Jul 15, 2009
17. ### Ben BacarisseGuest

"" <> writes:
<snip>
> I have tried to compile your code which i find more coherent than the
> one with (char *) ... but the compiler returns :

what this is even though you may be viewing it though Google's
goggles) is not synchronous -- give enough time for people's replies
to filter through.

Anyway, see my earlier reply with may now be visible to you.

<snip>
--
Ben.

Ben Bacarisse, Jul 15, 2009
18. ### Phil CarmodyGuest

Tim Harig <> writes:
> On 2009-07-14, jameskuyper <> wrote:
>> wrote:
>>> About portability and c99, i'm not sure to understand the relation
>>> between the two. Is C99 the newest c programming trend or norm that
>>> doesn't fully take in consideration portability ?

>> C99 is a decade-old update to the C standard. While that is the
>> "newest" version of the standard, there are only a few implementations
>> that fully conform to C99, so it doesn't qualify as trendy.

>
> Which rather brings up the point of penetration. I wonder how many
> people are really taking advantage of C99

Ambiguous predicate.

Anyone who uses // comments is really taking advantage of C99. That's
a lot of people. Probably people who don't even know it's C99, probably
people who don't even know the difference between C and C++, but still,
they're, perhaps by happenstance, taking advantage of some part of C99.

However, if by 'really taking advantage of C99' you mean 'deliberately
taking advantage of large swathes of new features from C99', then the
conclusion would be very different.

Phil
--
If GML was an infant, SGML is the bright youngster far exceeds
expectations and made its parents too proud, but XML is the
before he had sex, which was rape. -- Erik Naggum (1965-2009)

Phil Carmody, Jul 16, 2009
19. ### Richard BosGuest

Phil Carmody <> wrote:

> Tim Harig <> writes:
> > On 2009-07-14, jameskuyper <> wrote:
> >> wrote:
> >>> About portability and c99, i'm not sure to understand the relation
> >>> between the two. Is C99 the newest c programming trend or norm that
> >>> doesn't fully take in consideration portability ?
> >> C99 is a decade-old update to the C standard. While that is the
> >> "newest" version of the standard, there are only a few implementations
> >> that fully conform to C99, so it doesn't qualify as trendy.

> >
> > Which rather brings up the point of penetration. I wonder how many
> > people are really taking advantage of C99

>
> Ambiguous predicate.
>
> Anyone who uses // comments is really taking advantage of C99.

Not true. Most compilers which support // comments did so well before
C99. That's why they're in C99 in the first place.

Richard

Richard Bos, Jul 17, 2009
20. ### Keith ThompsonGuest

(Richard Bos) writes:
> Phil Carmody <> wrote:
>> Tim Harig <> writes:
>> > On 2009-07-14, jameskuyper <> wrote:
>> >> wrote:
>> >>> About portability and c99, i'm not sure to understand the relation
>> >>> between the two. Is C99 the newest c programming trend or norm that
>> >>> doesn't fully take in consideration portability ?
>> >> C99 is a decade-old update to the C standard. While that is the
>> >> "newest" version of the standard, there are only a few implementations
>> >> that fully conform to C99, so it doesn't qualify as trendy.
>> >
>> > Which rather brings up the point of penetration. I wonder how many
>> > people are really taking advantage of C99

>>
>> Ambiguous predicate.
>>
>> Anyone who uses // comments is really taking advantage of C99.

>
> Not true. Most compilers which support // comments did so well before
> C99. That's why they're in C99 in the first place.

In fact // comments were introduced in BCPL, and dropped by K&R when
they derived C from it. They were re-introduced in C++ (Stroustrup
has explicitly stated that he got them from BCPL). It was probably
the C++ influence that led pre-C99 compiler implementers to support
them as an extension. (Note that they can cause incompatibilities in
some contrived cases.)

<OT>My personal preference, ignoring compatiblity issues, is
for all comments to be terminated by the end of the line, like
"//" comments in BCPL, C++, and C99, "#" comments in a number of
comments will never be eliminated in any language calling itself
either C or C++.</OT>

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jul 17, 2009