Timeline for Legitimate usage of char*
Current License: CC BY-SA 3.0
15 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 18, 2015 at 2:43 | history | edited | Yu Hao | CC BY-SA 3.0 |
added 4 characters in body; edited tags
|
Aug 12, 2015 at 7:11 | comment | added | Leonardo |
For 3), s is a variable, as such it needs memory (as any normal int would). It happens that it points to an address, but thats just a detail. No additional space is required, if you lose that address, its lost for good, no one keeps track of it. Then, s is a variable (that consumes 4 bytes), that points to a piece of memory that has 10 bytes. Hope this clarifies a bit.
|
|
Aug 12, 2015 at 1:37 | history | edited | Sam Y. | CC BY-SA 3.0 |
added 1164 characters in body
|
S Aug 11, 2015 at 22:27 | history | edited | Sourav Ghosh | CC BY-SA 3.0 |
improved formatting
|
S Aug 11, 2015 at 22:27 | history | suggested | Gabriel | CC BY-SA 3.0 |
improved formatting
|
Aug 11, 2015 at 22:25 | comment | added | chux - Reinstate Monica | @user3629249 "placed in a readonly memory segment" and "The char string cannot be modified," are not specified by C. Attempting to modify it is undefined behavior. We agree code should not attempt/expect modifying to work. | |
Aug 11, 2015 at 22:22 | comment | added | chux - Reinstate Monica |
1) Could use char s[] = "my string"; and s is right-sized to 10. 2) &s is the address of a pointer to an array of 10 char - not the same type as char * .
|
|
Aug 11, 2015 at 22:19 | comment | added | user3629249 | in C, an array degrades to the address of the array. In the statement: 'char* s = "my String";' the char string 'my String' +'\0' is placed in a readonly memory segment. and 's' receives a pointer to that char string (I.E. the address of that string) The char string cannot be modified, but can be read. | |
Aug 11, 2015 at 21:45 | review | Suggested edits | |||
S Aug 11, 2015 at 22:27 | |||||
Aug 11, 2015 at 21:39 | comment | added | too honest for this site |
Try printf("%c", 1["Hello"]) . You might be surprised. The swap 1 and the string literal and try again. Note: That is just to make you think - do not use this in your code. You will find the answer here already, so please do not add a new one.
|
|
Aug 11, 2015 at 21:37 | answer | added | David K | timeline score: 5 | |
Aug 11, 2015 at 21:32 | review | First posts | |||
Aug 11, 2015 at 21:34 | |||||
Aug 11, 2015 at 21:31 | answer | added | Sourav Ghosh | timeline score: 2 | |
Aug 11, 2015 at 21:31 | answer | added | Matt Olson | timeline score: 3 | |
Aug 11, 2015 at 21:28 | history | asked | Sam Y. | CC BY-SA 3.0 |