Subject:
|
RE: problems with creating arrays of larger sizes
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Tue, 18 May 2004 16:56:30 GMT
|
Viewed:
|
5433 times
|
| |
| |
> Andreas Jurk wrote:
> > OK, here is my code.
>
> > int main()
> > {
> > int i;
> > char c[n];
> >
> > for(i=0;i<n;i++)
> > {
> > *(c+i)=0;
> > }
> > return 0;
> > }
>
> This won't work for sure.
Need to understand the difference how array and pointers are being allocated
... And char is usually one or two bytes, depending on type of procesor;
and pointer is usually 4 bytes, unless you go to 64-bit processor ... If
you wish to utilize the convenicence of using array... Try this:
E.g.
char *c2=(char*)NULL;
char c1[10];
memset(c1, 0, 10);
OR
char *c2=(char*)NULL;
char c1[10];
c2 = c1;
// so whatever c1 points to , c2 will have the content; and vice versa
memset(c1,0, 10);
// this will do the initialization that you wish to do ; for both c1 and
c2.
//e.g.
// the following will print A A A A
memcpy(c1, 65, 10);
printf("%c %c %c %c\n", *(c2+0),*(c2+2), *(c2+7), *(c2+8));
// the following will print B B B B
memcpy(c2,'B' , 10);
printf("%c %c %c %c\n", *(c1+0),*(c1+2), *(c1+7), *(c1+8));
// but beware, if you are trying deallocate c2, not only c1 is corrupted, it
may very well crash in most dll.
--
E
|
|
Message is in Reply To:
6 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|