Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Compiler Question

Author: Sune Fischer

Date: 16:19:20 01/19/02

Go up one level in this thread


On January 19, 2002 at 18:01:13, Angrim wrote:
>>
>>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.

Well it was a linux forum ;)

>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));

yes sizeof() only works with arrays, but is that really the reason arrays are
not pointers?

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

It would?
I believe you should do:
memset(s, 0, 100);
in both cases, s being a pointer _or_ array.


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

char a[]="wrong";
should work, but is 'a' then a pointer or array (im guessing array but...)


>So in summary, arrays are not pointers, and it is possible to
>mess up your programs by confusing the two.
>
>Angrim

Other than the sizeof operator and the fact that

char a[5];
char *p;

a=p;

is illegal, I don't see the differences.
The syntax is, from my point of view, identical.

-S.



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.