Subject:
|
RE: problems with creating arrays of larger sizes
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Tue, 18 May 2004 16:56:30 GMT
|
Viewed:
|
5129 times
|
| |
![Post a public reply to this message](/news/icon-reply.gif) | |
> 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: ![problems with creating arrays of larger sizes -Andreas Jurk (14-May-04 to lugnet.robotics.rcx.legos)](/news/x.gif) ![](/news/46.gif) ![Re: problems with creating arrays of larger sizes -Michael Obenland (14-May-04 to lugnet.robotics.rcx.legos)](/news/x.gif) ![](/news/46.gif) ![Re: problems with creating arrays of larger sizes -Andreas Jurk (17-May-04 to lugnet.robotics.rcx.legos)](/news/x.gif) ![](/news/246.gif) ![Re: problems with creating arrays of larger sizes -Nathan Braun (17-May-04 to lugnet.robotics.rcx.legos)](/news/x.gif)
![](/news/x.gif) ![](/news/x.gif) ![](/news/68.gif) ![Re: problems with creating arrays of larger sizes -Michael Obenland (17-May-04 to lugnet.robotics.rcx.legos)](/news/x.gif) ![](/news/46.gif) ![You are here](/news/here.gif)
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|