Re: Can pre-innitialized array be changed? by Robby
Robby
Tue Feb 07 16:20:29 CST 2006
Hi Victor,
I never said that I am using C, I said, that I am reviewing some C stuff,
since I am looking at some code fragments form a C book. I must of created a
CPP file when I created a new project so I can try these C samples. I think I
chose "Win32ConsoleProject" and then "CPP file". I am not sure though!
So, I don't know.... according to you, If I am to try some C stuff, where I
am sopposed to try it then?
Oh yeah! hmmmm , The string class... I would have to review that.
Victor... Tell me something. I have started all this C programming last
summer. I did read a book in C++ and it was very interesting and enjoyed it.
But, Since September I started reading Petzold's book to understand Win32. I
explained to one of my freinds whichg is a Guru in this stuff...(He was also
certified for newsgroups) that I was reviewing linked lists and string
classes and templates. He nearly yanked the book away from me! telling me
that if I am to program in VC++, that I wouldn't need all that?
I found it strange, because compared to me, he is a genius in
C/C++/VC++/Delphi, so at first I didn't know what to do... But I read up on
the stuff anyways. I don't know if I did good or not, but I did put alot of
time with classes, array of pointers to classes and so forth, (I fully
understood linked lists but didn't really practice much with them since I was
told that it wasn't neccessary in VC++)
Can you share your sincere opinion with me! Is it true that most of the
programming in VC++ doesn't really require you to know linked lists, and
string class etc.. ?
--
Best regards
Robert
"Victor Bazarov" wrote:
> Robby wrote:
> > I am reviewing some array stuff in 'C' and I would like to understand
> > something. The code fragment below displays the words innitialized in the
> > words array.
> >
> > ===============================================
> > char words[][2][10] =
> > {
> > "We", "Are",
> > "All", "Human"
> > };
> >
> > printf("All the words will be displayed. Press any number to continue! \n");
> > scanf("%d",&p);
> >
> > for(i = 0; i<2; i++)
> > for(ii=0;ii<2;ii++)
> > printf("Word > %s:\n",words[i][ii]);
> >
> > //words[1][1]="Nice";
> >
> > printf("ENTER A NUMBER[#] TO EXIT: >> ");
> > scanf("%d",&p);
> > ===============================================
> >
> > Lets say that further down this program I would like to replace the last
> > word "Human" to "Nice". Why wouldn't I be able to do it with the following
> > line:
> >
> > words[1][1]="Nice";
>
> Because arrays are _not_assignable_. You can still do it using 'memcpy'
> or 'strcpy':
>
> strcpy(words[1][1], "Nice");
>
> > Anyhow, if I try this, it gives the following error:
> >
> > c:\_DTS_PROGRAMMING\c\GREEN_OSBOURN_P177_PointersWithArrays\P177.cpp(108) :
>
> The extension of the file ('.cpp') suggest that you're using C++. Why do
> you then claim that you're using 'C'? And if you are, in fact, using C++,
> then you should not really be using arrays of char, but instead use the
> standard 'string' class...
>
> > error C2440: '=' : cannot convert from 'const char [5]' to 'char [10]'
> >
> > The error seems to not be able to convert a 'const char' (Nice) to a char
> > (as array defined).
>
> Not to a char. To an array of char.
>
> > So is it that when we declare an array this way:
> >
> > char words[][2][10] =
> > {
> > "We", "Are",
> > "All", "Human"
> > };
> >
> > then we cannot modify its contents anymore? Lets say we wnat to modify the
> > contents, what do we do?
>
> We assign them, element by element, or use other means, like standard
> library functions.
>
> > [..]
>
> V
> --
> Please remove capital As from my address when replying by mail
> Sorry, I do not respond to top-posted replies, please don't ask
>