Link to home
Start Free TrialLog in
Avatar of ee_lcpaa
ee_lcpaa

asked on

char* and char array!

Hi,

What is the difference of declaring

Method 1:

char* temp = "1234";

and

Method 2:

char temp[10];
strcpy(temp, "1234");

Is there any difference of the above code?
Avatar of jhance
jhance

Yes, there is a difference.

In the case of:

char *temp = "1234";

the char array is initialized with the "1234" data by the compiler as a part of the initialization code for the application.

In the 2nd case:

char temp[10];
strcpy(....);

there is no initialization done by the compiler and you get only an allocated space of 10 chars for your purpose.  You then fill it with something with your strcpy call.

Note that the fist case CANNOT be changed to hold a different string.  You could not do:

strcpy(temp, "3456");

in the first case.  In the second case you can call strcpy again and again and put different data into it.

ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
There is a big difference - if you use

char* temp = "1234";

'temp' points to a constant string literal that is 5 bytes in size (4 letters + trailing '0'), and one some OSs, writing to it will cause an access violation.

char temp[10];
strcpy(temp, "1234");

Here the compiler allocates a 10-byte mem area for 'temp' you can read from and write to.

There's actually a 3rd method that is even easier and more convenient:

char temp[10] = "1234";

Here the compiler allocates a 10-byte mem area for 'temp' you can read from and write to and initializes it with a string.

jkr,

Thanks for stomping on my comment!!
What

const char* temp = "1234";

does is it defiens a pointer, not a string, not an array, only a pointer.  Now it initializes that pointer to point to an array somewhere that stores "1234", but as far as you are concerned, this line does not define that array that stores "1234", it  only defines a pointer.  this is important because code like

const char *temp1 = "1234";
const char *temp2 = "1234";

defines two pointers and those pointes both point to arrays that contain "1234", but they _could_ point to the same array.  i.e. the two pointers you declare might be initialized to point to the veyr same array.  That is why the arrays they point to must be considered constant.  if in C you did

char *temp1 = "1234";  // C code
char *temp2 = "1234";

*temp1 = 'A';

You might find that temp2 is now changed.  Well to be specific, the pointer stored by temp2 would still be the same, but now it would point to an array that stores "A234".

That is why it is important that these pointers be declared to point to constant arrays, so that these changes do not occur!

and because these poitners are declared to point to contant arrays, you can't change them, that is important.

continues
>>Thanks for stomping on my comment!!

??????????????????

What in all the world are you talking about? Is it illegal now to post a comment just 2 minutes later than you??? Have you been told that typing something may take time?
https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp

and to quote:

"it is common etiquette in some topics areas to always post comments and not answers. "

we are not alone.....

I'd retract my answer, but once again that feature is gone....

Code like

   char temp[10];
   strcpy(temp, "1234");

or as jkr pointed out

   char temp[10] = "1234";

or

   char temp[]  = "1234";

does not define only a pointer.  it actually defines an array.  and array of 10 characters (except one of 5 characters in the 3rd example)  It then initializes that array to store "1234".  The difference is that this array is defined by your code for you purposes, unlike the previous code which defined only a pointer and then _used_ a statically defined and possibly shared array.   The array defined in this way can be changed safely, without affecting other arrays/strings in the program.  i.e.

   char temp1[10] = "1234";
   char temp2[10] = "1234";

   temp1[0] = 'a'; // Safe

this code does not risk changing temp2.  Which is of course good!.

However, this safety comes at a cost.  creating arrays like this are slighly slower and more mmeory intensive.  So if you will not be changing the string, then you are probably better of using the first method.  but if you will be chaning the string, you must use the second method.
>> You could not do:
>> 
>> strcpy(temp, "3456");
Actually you could do that in C or in non-compliant C++ compilers.  However it might cause undesirable results. so really you mean you "should" not... not "could" not.
>> and to quote:
>> 
>> "it is common etiquette in some topics areas to always post comments and not answers. "
Well, its certianly not true in this topic area.  And you often post answers here.
>>"it is common etiquette in some topics areas to always
>>post comments and not answers. "

Where's your point, jhance? When I posted my comment, there was nothing else - do you really think I'd waste my time to write something similar like your comment when I had seen it? Point your browser to https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt and complain that jkr really dared to type a similar comment like yours at the same time...
nietod:
>> Well, its certianly not true in this topic area...

Sorry, but why not?
>> Sorry, but why not?
The experts here just don't.  And I don't think that is a bad thing.  It prevents conflicts.  here you've got 3 different answers that say the same thing.   If jhance had answered, then there would be no room for conflict.  he woudl have been here first and had posted a correct answer.  jkr and I would not have tried to answer and more importantly, I woudl not have succeeded in locking the question with an answer, even thoug I was not the first to answer.   Note that I would not have attenpted to do so if I had know that jhance had posted.  But web pages are not updated in real-time.  That is why locking a question actually prevents these problems.
>>jkr and I would not have tried to answer

Actually, I posted a COMMENT, not an answer - but it seems that jhance considers posting a COMMENT as an infringment to his natural right to claim points - or in which other way should I understand his accusations?
>> or in which other way should I understand his
>> accusations?
It seemed strange to me too unless he mis read and thought that you, not me, had locked the question.  But even so, its not my fault that the question is locked with the 2nd answer, that is the first answerer's responsibility.
>>unless he mis read and thought that you, not me, had
>>locked the question

That's very unlikely, as the notifications clearly state 'XXX has proposed an answer to this question.'. So either he doesn't read the notifications before accusing somebody or he doesn't care. In both cases I think he should rather reflect upon his own behaviour rather than complaining to CS about others or insulting people.
jkr,

My apologies.  I thought that YOU were the one who locked this question but I see that nietoad is the real cuplrit.
So I guess we can just declare "open season" on locking questions from now on, right?  

Forget any manner or "professional courtesy".  Just lock the questions and everyone else be damned!!  The person asking the question can sort it out.....
>> but I see that nietoad is the real cuplrit.
I admit it.  I am not psychic.  When I came to the question, I was not able to predict that you would post to the question before me.   However, if you are psychic, When you foresee this problem coming in the future, please just answer the question and prevent hte problem.

>> So I guess we can just declare "open season" on
>> locking questions from now on, right?  
The same rules apply as always.  Answer if you have an answer that you are reasonably sure is correct.  

>> Forget any manner or "professional courtesy".
Who is being unprofessonal an  uncourtesious?  jkr and I din't post anything hostile.  you were the one that accused jkr an now me of soem wrong doing when it was clearly a problem out of our control.
jhance,
>>So I guess we can just declare "open season" on locking
>>questions from now on, right?  

Have you been on vacation?  Experts on this topic area have been doing this for a long while now.
I've been complaining about this for who know how long.

I finally gave up, and said, "If you can't beat them, then join them!".

>>when it was clearly a problem out of our control.
Actually, this is very much in your control.  If you would post comments instead of answers, this would avoid this very problem.

Other topic areas do this with no problem.  I don't understand why we can't do it in this topic area.  Other then the fact that the top expert doesn't want to do it.
>> Experts on this topic area have been doing this for a long while now.
Since the very start of EE in fact.  

That is the way EE works,  The answer button would not be there id it was nof meant to be used.
I think it would also prevent conflicts if everyone posts comments. The questioner probably wants to have not just the right answer, but the BEST answer, so it is fair to post as much answers as possible and let the questioner judge what is the best answer. That also opens more views to a the questioners problem, the answerer perhaps just didn't think of.

I think it's better to post comments :-)

Greetings
Andi
AndreasF,
I have posted the very same thing you have posted.
But the top expert here, is stubern, and will not budge on this issue.
So the questioners, and other experts are just going to have to suffer.
>>>The answer button would not be there id it was nof meant to be used.

You seem to have missed the difference between CAN BE DONE and SHOULD BE DONE!  

Not everything that is possible to do is a good thing to do.  Sheesh!  
>>  If you would post comments instead of answers, this would
>> avoid this very problem.
Not at all.  Someone else would lock the question and get the points.  It just happend in the last weak.  i the client specifically said he/she didn't want answers so I didn't.  Another client came along and answered with an answer that was 99.9% of 10% of what I said and who gets the points?

Answering avoids problems, it doesn't cause them.  jhance was here first with a correct answer, he shoudl have answered.  In the unlikely event tht the client didn't want jhance's answer, then the client could reject it, but in the meantime it woudl prevent "poaching" and mistakes like this.

>> Other topic areas do this with no problem.  
And they don't have problems where points get awarded unfairly?  Not that I've seen.

>> Other then the fact that the top expert doesn't want to do it.
What?   Do you think I invented this?   No one locked questions before I came to EE?  nope.  Questions were being locked with answers when i came here and ever since.  

Windows topic area:  Answered within 2 hours
https://www.experts-exchange.com/jsp/qShow.jsp?ta=winprog&qid=20129342
VB topic area  Answered in 9 minutes
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20130507
Java Programming
https://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20129191
java Amswered in less than 30 minutes
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20130784

How did I Cause these?  I don't answer in these topic areas?   Do you blame the topic experts in those topic areas too?
Leaving the question open, gives the questioner a better chance of getting a good answer.
For example, you just posted an answer on the following link:
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20130781

This answer was clearly wrong.   If I wasn't the type of expert who checks answered questions, this questioner might not have gotton a correct answer.

Furthermore, if you would have left it open, the questioner might have gotton the correct answer sooner.
>>Answering avoids problems, it doesn't cause them.  
It just caused this problem.  It clearly causes more problems.

I haven't seen it avoid any problems yet, and I use other topic areas that use the proposed policy.
>>And they don't have problems where points get awarded
>>unfairly?  Not that I've seen.

I have not seen this happen at all in any of the other topic areas.

I do see it happen here, only because of the current policy in this topic area.
>> I think it would also prevent conflicts if everyone posts comments. The
>> questioner probably wants to have not just the right answer, but the
>> BEST answer,
Unfotunately that is exactly right.  The client wants the best answer and usually doesn't care about anything else.  And I mean anything else.  So what happens is that you get 3 different versions of the answer and the points get awarded at random.  or no points get awarded and the question sits.   Worse  And this happens all the time, including this topic area.  One expert provides 995 of the info, then the client asks for some tiny little clarification and a 2nd expert posts that.  The 2nd expert gets all the points having provided nearly none of the effort.   When that happens here, I usually post a message to the client to pointing out that they coudl have been a little more fair.

>>That also opens more views to a the questioners
>>problem, the answerer perhaps just didn't think of.
Answering doesn't prevent that.   But it tends to encourage the points to go to the right expert.  And it prevents plaugerism.  Every once in a while someone goes through the unlocked quesitons and copies the existing comments into an answer and locks the question.  (And often they really _copy_ them, word for word)

>> But the top expert here, is stubern, and will not budge on this issue.
And I have a supernatural effect on the other topic areas?  I've never even been to the java topic area until today?   I think a retraction is in order....

>> You seem to have missed the difference between
>> CAN BE DONE and SHOULD BE DONE!  
Apparently so have you.  You lock questions all the time!  Sometimes you lock homework questions without providing any answer.
>>The client wants the best answer and usually doesn't
>>care about anything else.  
That's clearly wrong.  Most client want to hear different points of view for thier question.  And I seen many of them tell you so.
I've seen many questioners reject your answer, and tell you that they want to wait and hear from other experts.

You usally verbally bash them, and tell them that yours is the only correct answer.

So this clearly proves that clients want to hear more then just one correct answer or A correct answer.
>>Answering doesn't prevent that.  
Clients feel that it does, and as previosly stated, they have told you so.

>>But it tends to encourage the points to go to the right expert.  

Well why don't you just look at the question that you just answered, and got awarded the points for.
The answer was wrong, and I posted the correct answer.
Because you locked the question, the questioner gave you the points.
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20130781

That proves that locking the question, only encourages unknowledgable questioners to award points to the first expert who locks the question.  Even when the answer is clearly wrong.
>>And I have a supernatural effect on the other topic
>>areas?  I've never even been to the java topic area
>>until today?   I think a retraction is in order....

What did Java have to do with the price of beans?
No pun intended :-)

I think a retraction to your requested retraction is in order....
>>Because you locked the question, the questioner gave you the points.

This is what I call "bottom feeding".  Too bad there are too many experts who feel the need to do this.


>>You lock questions all the time!  Sometimes you lock homework

So which is it?  All the time or sometimes?

How often do I lock a Q that ALREADY HAS A CORRECT OR MOSTLY CORRECT (since you pick apart my comment here) POSTED ON IT?  I don't!

Lock a homework Q with no answer?  No.  Lock one pointing out the EE policy violation?  Yes.  Your point?
>>  have not seen this happen at all in any of the other topic areas.
Then Isn't it strange that it happens in this topic area!


>> https://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20127047
I got the points becuase i commented on an follow up answer.  I didn't deseve them and I notified the client of this.  Woudln't have happened if the expert had answered.

 https://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20127529
points when to follow-up answer, not the real answer.  I'd like to know how this is my fault?  I didn't participate at the time, in fact I was out of town.

So which is it?  All the time or sometimes?
You usually lock questions.  I'm surprised you dind't in this case.

So which is it?  All the time or sometimes?

>> How often do I lock a Q that ALREADY HAS A CORRECT OR MOSTLY CORRECT
Intentionally?  To the best o my knowledge never.

How often do I intentionally lock one that has a correct answer?  never.

There was no comments on this question when i posted.  

I would have retracted my answer if I could.  I even stated that long before this debate started.  There is no retract button anymore.  Is that my fault too?

>>   Lock one pointing out the EE policy violation?  Yes.  Your point?
That you lock questions too.  Why are you allowed to lock them and not me?
Axter, you win.   I've had it with you.

Good bye.

>>This is what I call "bottom feeding".  Too bad there are
>>too many experts who feel the need to do this.

Yes, that's one of the major reasons his points are so much higher then the next expert in the list.

I didn't enter the top 15 list until I started using hist method.  It only took me 6 weeks to get into the top 15 list after I started to lock up questions like nietod does.

Hated to resort to his method, but I got tired of seen the wrong expert get the points so many time.
>>Axter, you win.   I've had it with you.

Well of course.  I would say goodby to if I made the above statements while simultaneously posting an incorrect answer, and getting awarded points for it.
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20130781

After that, I think I would immedietly take myself out of the conversation.

I notice how you haven't made any comments to the above link.

Things that make you go Hmmmm.
Guys/gals,

I did not follow this conversation as I don't receive any notifications at the moment (not EE's fault), but things seem to get out of control.

Just ONE THING from what I've noticed so far:

I will *definitely* report *everyone* to CS who locks questions with obvious non-answers stating sth. like "Oh, I'm sorry, I'm told that we need to post all comments as ANSWERS to avoid unecessary confusion."

If you all want to be childish and play around, feel free to do so, but *not* on the expense of people asking questions here.

My opinon on that: Lock a question if you think you have the answer and it's hardly disputable (well, you might be wrong - nobody is perfect). Don't lock it if there's any doubt or room for discussion.

That's theway I handle it...

Oh, and for this particular one: Please keep in mind that all this unnecessary discussion hadn't taken place if there still was this 'withdraw answer' button...

PS: jhance - apologies accepted :o)
>>Lock a question if you think you have the answer and it's hardly disputable

I don't agree with this either.  I'm sure when nietod posted his answer on the following link, that he though he was right.

https://www.experts-exchange.com/jsp/qShow.jsp?qid=20130781

Yet his answer was wrong, and in locking the question with an incorrect answer, it resulted in the questioner awarding him the points for a question that I answered correctly.

I find this happens too often.  Questioners who are afraid to reject an answer, or who don't know how to reject an answer, or who just don't care, will often give the points to the first expert to lock the question.  Even if they got the correct answer from another expert.

Do you think what happen in the above link is fair?
The questioner did NOT accept nietod's answer until after I posted the correct answer.
>>Do you think what happen in the above link is fair?

No - but should I post the links to all the Qs where something 'unfair' like this happened to me? It happens all the time, to every expert here. IMO that particular one wasn't nietod's fault, it was the fault of the person asking that question. I actually think that nietod hardly would mind to be proven that he's wrong. Things like that happen, so report them to CS - that's what they're here for...
>>I actually think that nietod hardly would mind to be
>>proven that he's wrong.

This has happen many times before, but I never said anything.
And I wouldn't have said anything this time either, except that nietod is trying to say the his locking question method encourage the points to go to the right expert.
See follow quote.
>>"Answering doesn't prevent that.   But it tends to encourage the points to go to the right expert."

Don't you think you would be a little P if an expert stated the above, and then a few minutes later gets awarded points for someone else's answer.
>>IMO that particular one wasn't nietod's fault.

In my opinion it is his fault, because he locked the question with an incorrect answer.  If he didn't lock the question, it wouldn't have happen.
>>In my opinion it is his fault, because he locked the
>>question with an incorrect answer

I don't get it - if an incorrect answer is being accepted by a customer, this is not the expert's fault. The customer should have been aware of that, reject nietod's incorrect answer and accept a correct one. That's why you *can* reject answers. You cannot blame the owner of a gas station when a customer chooses the wrong fuel...
>>You cannot blame the owner of a gas station when a
>>customer chooses the wrong fuel...

You can if he told you that it's the right fuel....
>>You cannot blame the owner of a gas station when a
>>customer chooses the wrong fuel...

You can if the owner told the customer that it's the right fuel....

If the customer ask what type of fuel does he need in his car, and the gas station owner told him the wrong one, you can blame the owner.  Even if the customer is to dumb to know better.
The customer is depending on the gas station owner to be the expert.
I think we've beat this dead hourse to the ground.
You're not going to change my mind, and I'm not going to change yours.
So it's pointless to continue.
Lets just agree to disagree.
jhance,
AndreasF,
jkr,
Please check the following link covering "Posting Questions as Answer or Comment"
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20132087
Hello ee_lcpaa:

I want to apologize for the behavior of the experts in your question. I don't know why they felt the need to share their differences with you.

At this point, however, I'm going to delete this question, which returns your points to you. You can choose whether you want to post the question again. If the answer was here, then consider it a gift from the experts in this question.

Experts, feel free to discuss this at <bad link, please see below>. I will be removing Axter's question from the C++ topic area also. It is no place to decide procedure. Axter, I'll be commenting there as well to give you direction.

Thanks everyone.
amp
Community Support Moderator
Experts Exchange
amp,
The link you just posted is a bad link.
amp,
This is the message I get when I try your link:

This area is reserved for Experts Exchange Moderators only.
Please disregard my last comment.
ee_lcpaa,
I like to appologize for my behavior on your question.
I hope this does not give you the impression that this is a typical response to a question on EE.  It is not.