Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


2 questions on C

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
kitesan
Expert Cheater
Reputation: 0

Joined: 01 May 2014
Posts: 124

PostPosted: Sat Jan 28, 2017 9:27 am    Post subject: 2 questions on C Reply with quote

1)Why declaring a pointer to a string (char *Ptr = "literal") makes that string const? and (char str[] = "literal") doesnt?

2) For a C programmer, is it necessary to remember stdlib functions relating string management?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Sat Jan 28, 2017 2:24 pm    Post subject: This post has 2 review(s) Reply with quote

1. You can find the answer within the C standard here:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

Look at section 6.4.5 for string literals. The main point you will want to read would be:
Code:
The contents of the arrays are modifiable. On the other hand, the declaration
char *p = "abc";
defines p with type ‘‘pointer to char’’ and initializes it to point to an object with type ‘‘array of char’’
with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to
modify the contents of the array, the behavior is undefined.


Using a pointed literal is considered undefined behavior which leaves it as an assumed const.

2. I'm not quite sure I understand your question. Are you asking if you need to remember them without having to look them up? There is nothing wrong if referencing the documentation when needed. You are not going to remember every single function in the standard libraries for C or C++. It is better to reference the docs than to guess and write bad code that has issues.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
kitesan
Expert Cheater
Reputation: 0

Joined: 01 May 2014
Posts: 124

PostPosted: Sat Jan 28, 2017 2:47 pm    Post subject: Reply with quote

Yeah, you're totally right , and by the way thanks alot for the answer. I'm going to download that pdf as i'll probably need it again
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites