Hi,

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

Anyhow, if I try this, it gives the following error:

c:\_DTS_PROGRAMMING\c\GREEN_OSBOURN_P177_PointersWithArrays\P177.cpp(108) :
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).

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?

I am reading a C book from Osborne and it shows many examples, but this
scenario is not really shown, can someone help me resolve this issue... All
much appreciated!

--
Best regards
Robert

Re: Can pre-innitialized array be changed? by Victor

Victor
Tue Feb 07 15:27:14 CST 2006

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

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
>

Re: Can pre-innitialized array be changed? by David

David
Wed Feb 08 06:50:31 CST 2006

Robby,

You have a matrix of character arrays. I believe if you have an array of
pointers to character, you could do it.

Like this:

char * matrix[2][3]={"one","two","three","four","five","six"} ;
matrix[1][2] = "replacement value" ;

David Liebtag



Re: Can pre-innitialized array be changed? by Victor

Victor
Wed Feb 08 08:31:17 CST 2006

David Liebtag wrote:
> You have a matrix of character arrays. I believe if you have an array of
> pointers to character, you could do it.
>
> Like this:
>
> char * matrix[2][3]={"one","two","three","four","five","six"} ;
> matrix[1][2] = "replacement value" ;

Just so there is no misunderstanding, initialisation of pointers to char
with a string literal is _deprecated_ in C++. Please declare those array
elements 'pointers to const char':

const char * matrix[2] ...

otherwise somebody might think that it's OK to use those in, say, strtok,
and you get undefined behaviour.

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

Re: Can pre-innitialized array be changed? by Robby

Robby
Wed Feb 08 09:58:30 CST 2006

Thanks guys for all your posts... and David, I will go forth with the
learning required.

--
Best regards
Robert


"David Crow" wrote:

> Your friend was wrong to tell you those data structures are not worth
> studying. True, you may never need them in a program, but that hardly
> means that are not worthy of your time. Once you do know them backward
> and forward, you may end up just using the ones from the STL (which is
> probably what your friend is doing without really knowing how they
> work), but at least you'll have a good understanding of what is
> happening behind the scene. Go forth and learn!
>
> >
> > 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++)
> >
>
>

Re: Can pre-innitialized array be changed? by JustBoo

JustBoo
Sat Feb 11 15:19:30 CST 2006

On Tue, 7 Feb 2006 14:20:29 -0800, "Robby"
<Robby@discussions.microsoft.com> wrote:

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

I'm sorry, but your friend is a fool. Perhaps a seemingly smart fool,
but one nonetheless.

An analogy I have used before is: Say your friend told you not to
learn simple mathematics. "Forget addition, subtraction and division!"
you're told. "Here is a wondrous new tool: an electronic calculator.
Never do those dreary math problems in your head or on paper again. My
aren't we smart..."

Now suppose you're on your way to a new job interview as a
Mathematician and you lose your shiny new calculator. What then? You
don't even have the simple skills to do your job.

Here's one that is more realistic. You arrive at your job interview
and you whip out your calculator. The interviewer is instantly annoyed
and tells you: "Put that calculator away, we don't use them here. We
expect out math people to understand the fundamentals of their
profession and not rely on a calculator for the simple stuff. If you
can't do that then how are you going to handle algebra?"

It's about knowing the fundamentals. How are you going to get anywhere
without them? Sadly I have worked with people who knew a few things
they had learned by rote and that was it. They stunk when it came to
anything outside their realm of "fake knowledge." Of course, they were
then put on the management track.

If anyone, anywhere tells you "Oh, you don't need to know that."
Run, I mean RUN away from that idiot. Now, if they say: "Well at your
current level you need to learn "this" before "that" okay then. We all
have to learn and grow.

Expecting some "tool" - in this case VC++ - to substitute for real
knowledge is just plain stupid. And btw, Petzold's book is *exactly*
the place to start to really understand and program Windows.

Show your "friend" this post, he needs a "math" lesson.

"The only real education is self-education." - Aristotle

Re: Can pre-innitialized array be changed? by Bruno

Bruno
Sat Feb 11 15:45:01 CST 2006

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

in my experience as a professional programmer, there is NO stuff that you
don't need to know.
ALL knowledge is usefull, even if you don't need it directly.
I still try to learn new things every day, because everything that I learn
makes me a better programmer.

you have to start somewhere, and there is nothing wrong with reading books,
even if someone else says that you don't need them.
and things like linked lists, maps and strings are extremely usefull. I
think I use maps and strings in about every native C++ project i ever worked
on, because they are so darn easy to work with.

I have worked with socalled 'guru's' before, and in my experience, the more
they brag and yell, the less they really know. some of them tend to live in
an imaginary world where requirements don't change, legacy code does not
exist, and rewriting from scratch is preferred to reuse.

--

Kind regards,
Bruno.
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"