Author: Angrim
Date: 15:01:13 01/19/02
Go up one level in this thread
On January 19, 2002 at 08:50:01, Sune Fischer wrote: >On January 18, 2002 at 21:04:24, David Rasmussen wrote: >>But if I have declared an array statically in C (in my case it is C++), like >>this: >> >>int a[10]; >> >>int i = ...; >> >>a[i] = 42; >> >>or >> >>struct S >>{ >> int a[10]; >>}; >> >>S s; >> >>s.a[i] = 42; >> >>then a is a constant pointer, not just a pointer. And the compiler could very >>well assume (at least if I told it to), that array accesses via a cannot be >>outside bounds. You can write all sorts of code that would make this not work, >>but using such a feature would be the programmers responsibility. >> >>/David > >I was told once by some linux-nerds that 'a' is not a pointer in this case. >http://www.eskimo.com/~scs/C-faq/q6.2.html > >I said, "why not, you can use it just like a pointer?" >And they went berserk;) > This has nothing to do with linux, most experienced C programmers will be annoyed by this. The main danger in confusing arrays and pointer is when you try to do things to a pointer that would work with an array. Examples from a programer that I used to have to work with: char *s; s=malloc(100); memset(s, 0, sizeof(s)); or: char *s; s=malloc(100); memset(&s, 0, 100); in both of which cases, if it were char s[100]; the code would work. And a contrived example of useing an array like a pointer breaking: { char **s; char *t="right"; char a[6]="wrong"; s=&a; *s=t; printf("%s\n",a); } This should give a compiler warning. if you ignore it the results will be wrong(gibberish even) if instead this had been char *a="wrong"; the results would have been right and you would get no compiler warning. So in summary, arrays are not pointers, and it is possible to mess up your programs by confusing the two. Angrim
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.