Re: declaration of variable in for loop



santosh wrote, On 31/01/08 09:49:
jacob navia wrote:

<snip>

main()
{
int i,j, *pint;
for(i=0;i<10;i++){
int k = i;
printf("%d %p = *%d\n",i,&k,k);
pint=&i;
}
*pint = 789; // Accessing illegal storage
}

This would work on lcc-win since the storage is still valid.
I would not do this since it is absolutely non portable.
Other compilers could implement other strategies.

I hope as a QoI issue lcc-win issues a diagnostic for such uses?

I'm assuming you misread Jacob's code as saying "pint=&k" which is what he intended (as noted in a subsequent post). If so then I would not expect a diagnostic although I would be pleased if the compiler produced one.
--
Flash Gordon
.



Relevant Pages

  • Re: declaration of variable in for loop
    ... int i,j, *pint; ... This would work on lcc-win since the storage is still valid. ... I hope as a QoI issue lcc-win issues a diagnostic for such uses? ...
    (comp.lang.c)
  • Re: Linux 2.6.27.9
    ... static int smp_pSeries_cpu_bootable ... "HUAWEI MOBILE", ... "Mass Storage", ...
    (Linux-Kernel)
  • Re: a little mistake
    ... Also the use of "inline" for that function is redundant. ... compilers will do it anyways and it isn't portable across all compilers ... int main ... Calling function would be part of the users code. ...
    (comp.lang.c)
  • Re: It works without stdio.h !!
    ... most C compilers will assume that a function that has no prototype is an ... extern int, and since you only specify functions that live in the C library, an unresolved ... never declare a variable of type char unless you well and truly believe you need to be ... strcpy, strcat, sprintf etc. with it. ...
    (microsoft.public.vc.mfc)
  • Re: one-liner for characater replacement
    ... declaration you can omit the type and it is assumed 'int', ... a qualifier e.g. 'const'. ... unspecified but fixed default-promoted arguments returning int'. ... (Qualification only makes sense for things in storage ...
    (comp.lang.fortran)