This example compiles without warnings/errors (gcc 4.8.2 -Wall):  #include &lt;stdio.h&gt; int main()  {      char c;     int i;         printf("%p %02x\n",&amp;i,c[&amp;i]);     printf("%p %02x\n",&amp;c,c[&amp;c]);      // important to note that this line DOESN'T compile:     //printf("%p %02x\n",&amp;i,c[i]);     //  which makes sense as c is NOT an array, it is a char.      return 1; }   Why does the syntax c[&amp;i] compile ? Is it intentional or an accident? Is the syntax c[&amp;i] semantically valid ? (Does it have any useful meaning?)  Example of output for me: (pointers change each time)  0xbfb2577c b7718cea 0xbfb2577b 08   This question originated from a strange bit of code 'ch2[&amp;i]' in question here:   NOTE#0 (updated, upon reflection) on duplicates/similar questions: This question is not a duplicate of the linked questions on c array references. It is related so it is useful to refer to them. The related questions discuss the valid a[b] and b[a] case where one of a or b is a pointer and the other is an int. This question deals with the more weird and maybe should be invalid case where one of a or b is a char. With C arrays, why is it the case that a[5] == 5[a]? 14 answers  String as an array index 3 answers   NOTE #1: This happens with compiler as the type of variable c is char and that can be used as index into array when combined with pointer.  NOTE #2: for some reason, type of c[&lt;ptr&gt;] evaluates to type of &lt;ptr&gt;.  E.g.: the resulting types of c[&amp;pi] and c[&amp;pc] cause warnings in the following code:  int *pi; char *pc; pi=&amp;i; pc=&amp;c; printf("%p %02x\n",&amp;pi,c[&amp;pi]); printf("%p %02x\n",&amp;pc,c[&amp;pc]);   Warnings on type 'int *' or 'char *' instead of 'unsigned int':  c/so_cweirdchar2.c: In function ‘main’: c/so_cweirdchar2.c:13:5: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int *’ [-Wformat=]      printf("pi %p %02x\n",&amp;pi,c[&amp;pi]);      ^ c/so_cweirdchar2.c:14:5: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘char *’ [-Wformat=]      printf("pc %p %02x\n",&amp;pc,c[&amp;pc]);      ^
4478
As Joel points out in , in  (aka: K &amp; R), there is mention of this property of arrays in C: a[5] == 5[a]  Joel says that it's because of pointer arithmetic but I still don't understand. Why does a[5] == 5[a]?
123257
Is it true, that the Asterisk always means "Hey, that is a pointer!" And an Pointer always holds an memory adress?  (Yes I know for the exception that a * is used for math operation)  For Example:   NSString* myString;   or  SomeClass* thatClass;   or  (*somePointerToAStruct).myStructComponent = 5;   I feel that there is more I need to know about the Asterirsk (*) than that I use it when defining an Variable that is a pointer to a class.  Because sometimes I already say in the declaration of an parameter that the Parameter variable is a pointer, and still I have to use the Asterisk in front of the Variable in order to access the value. That recently happened after I wanted to pass a pointer of an struct to a method in a way like [myObj myMethod:&amp;myStruct], I could not access a component value from that structure even though my method declaration already said that there is a parameter (DemoStruct*)myVar which indeed should be already known as a pointer to that demostruct, still I had always to say: "Man, compiler. Listen! It IIISSS a pointer:" and write: (*myVar).myStructComponentX = 5;  I really really really do not understand why I have to say that twice. And only in this case.  When I use the Asterisk in context of an NSString* myString then I can just access myString however I like, without telling the compiler each time that it's a pointer. i.e. like using *myString = @"yep".  It just makes no sense to me.
60978
What compiler warning level do you recommend for different C/C++ compilers?  gcc and g++ will let you get away with a lot on the default level.  I find the best warning level for me is '-Wall'.  And I always try to remove fix the code for the warnings it generates.  (Even the silly ones about using parenthesis for logical precedence rules or to say I really mean 'if (x = y)')  What are your favorite levels for the different compilers, such as Sun CC, aCC (HPUX ?), Visual Studio, intel?  Edit:  I just wanted to point out that I don't use "-Werror" (but I do understand it's utility) on gcc/g++ because, I use:   #warning "this is a note to myself"   in a few places in my code.  Do all the compilers understand the #warning macro?
107627
Is it the same convention as those used in known programming languages such as Java and C? I'm trying to create a lot of variables and I want it to start with a number, but I'm not sure if it's okay.
84232
Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,  void deleteForMe(int* pointer) {      delete[] pointer; }   The pointer could be all sorts of different things, and so performing an unconditional delete[] on it is undefined. However, let's assume that we are indeed passing an array pointer,  int main() {      int* arr = new int[5];      deleteForMe(arr);      return 0; }   My question is, in this case where the pointer is an array, who is it that knows this? I mean, from the language/compiler's point of view, it has no idea whether or not arr is an array pointer versus a pointer to a single int. Heck, it doesn't even know whether arr was dynamically created. Yet, if I do the following instead,  int main() {      int* num = new int(1);      deleteForMe(num);      return 0; }   The OS is smart enough to only delete one int and not go on some type of 'killing spree' by deleting the rest of the memory beyond that point (contrast that with strlen and a non-\0-terminated string -- it will keep going until it hits 0).  So whose job is it to remember these things? Does the OS keep some type of record in the background? (I mean, I realise that I started this post by saying that what happens is undefined, but the fact is, the 'killing spree' scenario doesn't happen, so therefore in the practical world someone is remembering.)
88627
Possible Duplicate:          I'm learning C++ at the moment, and I'm coming across a lot of null-terminated strings. This has got me thinking, what makes more sense when declaring pointers:  char* string   or  char *string   ? To me, the char* format makes more sense, because the type of "string" is a pointer to a char, rather than a char. However, I generally see the latter format. This applies to references as well, obviously.  Could someone tell me if there is a logical reason for the latter format?
113560
I expected A::~A() to be called in this program, but it isn't:  #include &lt;iostream&gt;  struct A {   ~A() { std::cout &lt;&lt; "~A()" &lt;&lt; std::endl; } };  void f() {   A a;   throw "spam"; }  int main() { f(); }   However, if I change last line to   int main() try { f(); } catch (...) { throw; }   then A::~A() is called.  I am compiling with "Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86" from Visual Studio 2005. Command line is cl /EHa my.cpp.  Is compiler right as usual? What does standard say on this matter?
68837
So I'm working on an exceedingly large codebase, and recently upgraded to gcc 4.3, which now triggers this warning:     warning: deprecated conversion from string constant to ‘char*’   Obviously, the correct way to fix this is to find every declaration like  char *s = "constant string";   or function call like:  void foo(char *s); foo("constant string");   and make them const char pointers.  However, that would mean touching 564 files, minimum, which is not a task I wish to perform at this point in time.  The problem right now is that I'm running with -werror, so I need some way to stifle these warnings.  How can I do that?
1982
The following code is generating a stack overflow error for me  int main(int argc, char* argv[]) {     int sieve[2000000];     return 0; }   How do I get around this? I am using Turbo C++ but would like to keep my code in C  EDIT:  Thanks for the advice.  The code above was only for example, I actually declare the array in a function and not in sub main.  Also, I needed the array to be initialized to zeros, so when I googled malloc, I discovered that calloc was perfect for my purposes.  Malloc/calloc also has the advantage over allocating on the stack of allowing me to declare the size using a variable.
64269
My friend produced a small proof-of-concept assembler that worked on x86. I decided to port it for x86_64 as well, but I immediately hit a problem.  I wrote a small piece of program in C, then compiled and objdumped the code. After that I inserted it to my python script, therefore the x86_64 code is correct:  from ctypes import cast, CFUNCTYPE, c_char_p, c_long  buffer = ''.join(map(chr, [ #0000000000000000 &lt;add&gt;:   0x55,                     # push   %rbp   0x48, 0x89, 0xe5,         # mov    %rsp,%rbp   0x48, 0x89, 0x7d, 0xf8,   # mov    %rdi,-0x8(%rbp)   0x48, 0x8b, 0x45, 0xf8,   # mov    -0x8(%rbp),%rax   0x48, 0x83, 0xc0, 0x0a,   # add    $0xa,%rax   0xc9,                     # leaveq    0xc3,                     # retq ]))  fptr = cast(c_char_p(buffer), CFUNCTYPE(c_long, c_long)) print fptr(1234)   Now, why does this script keeps doing segmentation fault whenever I run it?  I have yet a question about mprotect and no execution flag. It is said to protect against most basic security exploits like buffer overruns. But what is the real reason it's in use? You could just keep on writing until you hit the .text, then inject your instructions into a nice, PROT_EXEC -area. Unless, of course, you use a write protection in .text  But then, why have that PROT_EXEC everywhere anyway? Wouldn't it just help tremendously that your .text section is write protected?
81943
How does one parse an integer to string(char* || char[]) in C? Is there an equivalent to the Integer.parseInt(String) method from Java in C?
12888
I'm trying to print types like off_t and size_t.  What is the correct placeholder for printf() that is portable?  Or is there a completely different way to print those variables?
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?
56146
I long thought that in C, all variables had to be declared at the beginning of the function. I know that in C99, the rules are the same as in C++, but what are the variable declaration placement rules for C89/ANSI C?  The following code compiles successfully with gcc -std=c89 and gcc -ansi:  #include &lt;stdio.h&gt; int main() {     int i;     for (i = 0; i &lt; 10; i++) {         char c = (i % 95) + 32;         printf("%i: %c\n", i, c);         char *s;         s = "some string";         puts(s);     }     return 0; }   Shouldn't the declarations of c and s cause an error in C89/ANSI mode?
40847
I can create a literal long by appending an L to the value; why can't I create a literal short or byte in some similar way? Why do I need to use an int literal with a cast?  And if the answer is "Because there was no short literal in C", then why are there no short literals in C?  This doesn't actually affect my life in any meaningful way; it's easy enough to write (short) 0 instead of 0S or something. But the inconsistency makes me curious; it's one of those things that bother you when you're up late at night. Someone at some point made a design decision to make it possible to enter literals for some of the primitive types, but not for all of them. Why?
82619
I was reading the answers to this question  and got this doubt while reading the comments.  programmers frequently find it confusing that &quot;this&quot; is a pointer but not a reference. another confusion is why &quot;hello&quot; is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1:56 That only shows that it doesn't use the same conventions as other (later) languages. – le dorfier Dec 22 '08 at 3:35 I'd call the &quot;this&quot; thing a pretty trivial issue though. And oops, thanks for catching a few errors in my examples of undefined behavior. :) Although I don't understand what info about size has to do with anything in the first one. A pointer is simply not allowed to point outside allocated memory – jalf Dec 22 '08 at 4:18 Is this a constant poiner? – yesraaj Dec 22 '08 at 6:35 this can be constant if the method is const int getFoo() const; &lt;- in the scope of getFoo, &quot;this&quot; is constant, and is therefore readonly. This prevents bugs and provides some level of guarantee to the caller that the object won't change. – Doug T. Dec 22 '08 at 16:42 you can't reassign &quot;this&quot;. i.e you cannot do &quot;this = &amp;other;&quot;, because this is an rvalue. but this is of type T*, not of type T const . i.e it's a non-constant pointer. if you are in a const method, then it's a pointer to const. T const . but the pointer itself is nonconst – Johannes Schaub - litb Dec 22 '08 at 17:53 think of &quot;this&quot; like this: #define this (this_ + 0) where the compiler creates &quot;this_&quot; as a pointer to the object and makes &quot;this&quot; a keyword. you can't assign &quot;this&quot; because (this_ + 0) is an rvalue. of course that's not how it is (there is no such macro), but it can help understand it – Johannes Schaub - litb Dec 22 '08 at 17:55  My question is, why is this a pointer a not a reference? Any particular reason for making it a pointer?  Some further arguments why this being a reference would make sense:  Consider Item 1 from More Effective C++ : use references when it is guaranteed that we have a valid object i.e. not a NULL (my interpretation). Furthermore, references are considered safer than pointers (because we can't screw the memory up with a stray pointer). Thirdly, the syntax for accessing references (.) is a little bit nicer and shorter than accessing pointers (-&gt; or (*)).
13470
I am a little surprised by the following.  Example 1:  char s[100] = "abcd"; // declare and initialize - WORKS   Example 2:  char s[100]; // declare s = "hello"; // initalize - DOESN'T WORK ('lvalue required' error)   I'm wondering why the second approach doesn't work. It seems natural that it should (it works with other data types)? Could someone explain me the logic behind this?