This code is giving me a segmentation fault at run time.  char *str = "HELLO"; str[0] = str[2];   Please can anyone tell me why?
924
The following code receives seg fault on line 2:  char *str = "string"; str[0] = 'z';  // could be also written as *str = 'z' printf("%s\n", str);   While this works perfectly well:  char str[] = "string"; str[0] = 'z'; printf("%s\n", str);   Tested with MSVC and GCC.
109558
Are these two equivalent?  char * aString = "This is a string.";  char aString[] = "This is a string.";  From my understanding, C++ stores strings by their addresses, so the pointer is getting a valid address. I assume the strings are also stored as type char? But how does it know how much memory to allocate for the string? Normally, char arrays need their array size preset. I believe the [] type arrays are counted by the compiler before compilation. Is this what happens for the string literal pointer as well?  On to my second question, what is the difference between these two:  int someNumber;  int * someNumber = new int;  The second is supposed to be dynamic allocation. If I understand the concept right, it allocates the memory as soon as it reads the declaration. But what happens in the standard declaration? Basically, I need to understand how dynamic allocation is advantageous over standard declarations.
6195
Using PHP, what's the fastest way to convert a string like this: "123" to an integer?  Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array?
100697
char *strtok(char *s1, const char *s2)      repeated calls to this function break string s1 into "tokens"--that is   the string is broken into substrings,   each terminating with a '\0', where   the '\0' replaces any characters   contained in string s2. The first call   uses the string to be tokenized as s1;   subsequent calls use NULL as the first   argument. A pointer to the beginning   of the current token is returned; NULL   is returned if there are no more   tokens.   Hi,   I have been trying to use strtok just now and found out that if I pass in a char* into s1, I get a segmentation fault. If I pass in a char[], strtok works fine.   Why is this?  I googled around and the reason seems to be something about how char* is read only and char[] is writeable. A more thorough explanation would be much appreciated.
5014
I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages.   Basically I have three questions:   Why use pointers over normal variables? When and where should I use pointers? How do you use pointers with arrays?
34071
Consider this:  public class TestClass {      private String a;     private String b;      public TestClass()     {     a = "initialized";     }      public void doSomething()     {     String c;          a.notify(); // This is fine     b.notify(); // This is fine - but will end in an exception     c.notify(); // "Local variable c may not have been initialised"     }  }   I don't get it. "b" is never initialized but will give the same run-time error as "c", which is a compile-time error. Why the difference between local variables and members?  Edit: making the members private was my initial intention, and the question still stands...
91467
Is there a way to convert a character to an integer in C?   For example, from '5' to 5?
30627
In C# the Main class has string[] args parameter.  What is that for and where does it get used?
14717
The problem is: Find $C_n$, the number of all ternary strings (length $n≥0$) that don't have consecutive 1's and 2's, using combinatorics.   Any tips or advices?
55455
Is it safe to assume that NULL always translates to false in C?  void *somePtr = NULL;  if (!somePtr) {   /* This will always be executed? */ }   Or should an explicit check against the value of NULL be made?
9790
If you have a statically allocated array, the Visual Studio debugger can easily display all of the array elements.  However, if you have an array allocated dynamically and pointed to by a pointer, it will only display the first element of the array when you click the + to expand it.  Is there an easy way to tell the debugger, show me this data as an array of type Foo and size X?
63138
I have a string that I would like to tokenize. But the C strtok() function requires my string to be a char*. How can I do this simply?  I tried:  token = strtok(str.c_str(), " ");    which fails because it turns it into a const char*, not a char*
93317
I'm learning how to write programs in C using the k&amp;r book (The C Programming Language) and I have a problem with one of the exercises. It's asking me to detect and remove a character in string s1, which matches any characters in the string s2.  So, say s1 = "A";  And s2 = "AABAACAADAAE"  I want it to return "BCDE"  I know I'm on the right path towards it, i just don't know how to design programs very well, could you give me any additional tips. I tried to read about the binary search tree algorithm, but felt it was a little too advanced for this mundane task.  Thanks everyone!  /* An alternate version of squeeze(s1, s2) that deletes each character in  * s1 that matches any character in the string s2  *  * Angie@odfx.org  */  #include &lt;stdio.h&gt; #include &lt;string.h&gt;  void squeeze(char s[], char t[]);  char string[] = "BAD"; char sstring[] = "ABC";  int main(void) {     squeeze(string, sstring);     return 0; }  void squeeze(char s[], char t[]) {     int i, j, d;      d = 0;     if(strstr(s, t) == NULL)         printf("%c", s[i]);     s[j] = '\0'; }
19865
Can someone explain what this means?  int (*data[2])[2];
7953
Can a string be used as array index in C?  Ex: String      Corresponding value "ONE"       1 "TWO"       2 "FIVE"      5 "TEN"       10  When a string in the above list is passed to the function, the function must return the corresponding value indicated above. Can this be achieved by declaring a constant array with string as index  int *x; x["ONE"]  = 1; x["TWO"]  = 2; x["FIVE"] = 5; x["TEN"]  = 5;  return x["string received by the function"];   The above logic does not work as expected; is there a workaround to implement the above logic in order to have a string-indexed array?
3226
class String {      private:         char* rep;      public:         String (const char*);         void toUpper() const; };   String :: String (const char* s) {     rep = new char [strlen(s)+1];     strcpy (rep, s); }   void String :: toUpper () const {     for (int i = 0; rep [i]; i++)     rep[i] = toupper(rep[i]); }   int main () {     const String lower ("lower");     lower.toUpper();      cout &lt;&lt; lower &lt;&lt; endl;     return 0; }
26696
In Program 1 Hello world gets printed just once, but when I remove  \n and run it (Program 2), the output gets printed 8 times. Can someone please explain me the significance of \n here and how it affects the fork()?  Program 1   #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt;  int main() {     printf("hello world...\n");     fork();     fork();     fork(); }   Output 1:  hello world...    Program  2  #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt;  int main() {     printf("hello world...");     fork();     fork();     fork(); }   Output 2:  hello world... hello world...hello world...hello world...hello world...hello world...hello world...hello world...